Mysql数据库
数据库
- 数据库[根据数据结构组织、存储和管理数据的仓库]。它是有组织的、可共享的、统一管理的大量数据的集合,这些数据长期存储在计算机中。
[En]
Database [a warehouse that organizes, stores and manages data according to the data structure]. It is a collection of organized, shareable and uniformly managed large amounts of data stored in a computer for a long time.*
- 数据是一家公司最宝贵的资产。程序员的工作是管理数据,包括操作、流通、存储、显示等。数据库最重要的功能是[存储数据]并长期保存数据。
[En]
data is the most valuable asset for a company. The programmer’s job is to manage the data, including operation, circulation, storage, display, etc. The most important function of the database is to [store data] and keep the data for a long time.*
Mysql
- MySQL是一个【关系型数据库管理系统】,瑞典的公司研发,被【Oracle】收购。
- MySQL使用了一种语言【SQL语言】。
- MySQL分为社区版和商业版,体积小、速度快、成本低,开源。
登录mysql :
mysql -h 127.0.0.1 -p3306 -uroot -p
当是在本地登录时 sql
mysql -uroot -p
创建一个数据库:
create database 数据库名;
create schema 数据库名;
查看所有的数据库
show databases
使用数据库
use 数据库名;
用于存储数据的对象是结构化数据的集合。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>The object used to store data is a collection of structured data.</font>*</details>
* 行:一行是一条数据,数据库里有多少条数据其实就是几行数据。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>* Row: a row is a piece of data, and how many pieces of data there are in the database is actually several rows of data.</font>*</details>
* 列:列是一个字段。数据库中有多少个字段?实际上有几列数据。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>* column: a column is a field. How many fields are there in the database? there are actually several columns of data.</font>*</details>
## SQL语言
Structured Query Language:结构化查询语言
SQL是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存储数据以及查询、更新和管理关系型数据库系统。
- SQL 语句可以单行或多行书写,以分号结尾。
- 可使用空格和缩进来增强语句的可读性。
- MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写。
- 3 种注释
- 单行注释: — 注释内容 或 # 注释内容(mysql 特有)
- 多行注释: / 注释 /
## DDL:操作数据库、表
create table 表名(
字段名1 类型(长度)约束条件;
字段名2 类型(长度)约束条件;
字段名3 类型(长度)约束条件;
.........
);
因为一个表有多列,并且数据库中有多个表,所以建表约束是我们应该如何对表中的数据和表之间的关系进行标准化。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Because a table has multiple columns, and there is more than one table in the database, the table-building constraint is how we should standardize the data in the table and the relationship between the tables.</font>*</details>
| **MySQL约束类型:**
约束名称 描述 NOT NULL 非空约束 UNIQUE 唯一约束,取值不允许重复 PRIMARY KEY 主键约束(主关键字),自带非空,唯一、索引 DEFAULT 默认值 FOREIGH KEY 外键约束,表和表之间的约束
(1)NOT NULL约束
sql
CREATE TABLE
(
stu_id int,
stu_name VARCHAR(50) NOT NULL,
char(1) DEFAULT '男',
datetime,
PRIMARY KEY(stu_id)
);
</code></pre>
<p>(2)UNIQUE</p>
<pre><code class="language-sql">create table
(
int PRIMARY KEY auto_increment,
varchar(50) not null,
bar_code VARCHAR(30) not null,
aut_id int not null,
UNIQUE(bar_code)
);
</code></pre>
<p>(3)主键约束 用多个列来共同当主键</p>
<pre><code class="language-sql">create table
(
aut_id int,
aut_name varchar(50) not null,
char(1) default '男',
varchar(50),
datetime,
PRIMARY KEY(aut_id,aut_name)
);
</code></pre>
<p>(4)外键约束 推荐配合主键去使用。有了这个约束,我们在向表中插入数据时,来源于另外一张表的主键
外键会产生的效果:</p>
<pre><code class="language-sql">create table
(
aut_id int,
aut_name varchar(50) not null,
char(1) default '男',
varchar(50),
datetime,
PRIMARY KEY(aut_id)
);
</code></pre>
<pre><code class="language-sql">create table
(
int PRIMARY KEY auto_increment,
varchar(50) not null,
bar_code VARCHAR(30) not null UNIQUE,
aut_id int not null,
FOREIGN KEY(aut_id) REFERENCES author(aut_id)
);
</code></pre>
<p>查看当前数据库中的所有表:<details><summary><em><font color='gray'>[En]</font></em></summary><em><font color='gray'>View all tables in the current database:</font></em></details></p>
<pre><code class="language-sql">show tables;
</code></pre>
<p>查看表结构:</p>
<pre><code>desc 表名;
</code></pre>
<p>修改表的五个操作
前缀都是 alter table 表名 -----</p>
<p>添加列 alter table 表名 add(字段名1 类型 , 字段名2 类型2 ····· ) ;</p>
<p>修改列数据类型:</p>
<pre><code class="language-sql">
alter table author MODIFY address varchar(100);
</code></pre>
<p>修改列名称和数据类型 将address 改为 addr</p>
<pre><code class="language-sql">alter table author change address addr VARCHAR(60);
</code></pre>
<p>删除列 作者表 addr字段</p>
<pre><code class="language-sql">alter table author drop addr;
</code></pre>
<p>修改表名</p>
<pre><code class="language-sql">ALTER TABLE author RENAME
;
</code></pre>
<p>删除表</p>
<pre><code class="language-sql">drop table if EXISTS
;
</code></pre>
<h2>DML(数据操作语言)</h2>
<p>操作表记录的语言(添加、删除、更改)不包括查询。<details><summary><em><font color='gray'>[En]</font></em></summary><em><font color='gray'>The language to operate on table records (add, delete, change), does not include queries.</font></em></details></p>
<p>插入数据:
intsert into 表名 (字段1,字段2 ,字段3)values( 1,2,3 );</p>
<pre><code class="language-sql">INSERT INTO
( aut_id, aut_name, gender, country, birthday, hobby ) VALUES (4,'罗曼罗兰','女','漂亮国','1945-8-15','写字');
</code></pre>
<p>如果插入完整的字段,则可以省略字段名。<details><summary><em><font color='gray'>[En]</font></em></summary><em><font color='gray'>If a full field is inserted, the field name can be omitted.</font></em></details></p>
<pre><code class="language-sql">INSERT INTO
VALUES (5,'韩寒','男','中国','1984-8-15','赛车');
</code></pre>
<p>批量插入:</p>
<pre><code class="language-sql">INSERT INTO
VALUES
(7,"李诞",'男','中国','1985-8-15','脱口秀'),
(8,"史铁生",'男','中国','1967-8-15','绘画');
</code></pre>
<p>修改数据
update 表名 set 字段1 = 修改的值 ,字段1 = 修改的值 where 条件</p>
<pre><code class="language-sql"> update
set aut_name = '金庸',country='中国' where aut_id = 1;
</code></pre>
<p>删除数据</p>
<pre><code class="language-sql">delete from
` where aut_id = 8;
截断(清空表)
TRUNCATE student;
说明:
truncate实际上应该属于DDL语言,操作立即生效,不能撤回。
- truncate和delete都是删除数据,drop删除整个表。
- truncate速度快,效率高,可以理解为直接删除整个表,再重新建立。
- truncate和delete都不会是表结构及其列、约束、索引的发生改变。
Original: https://www.cnblogs.com/ychptz/p/16589323.html
Author: 阿萨德菩提子
Title: 2022-8-15 数据库 mysql 第一天
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/505028/
转载文章受原作者版权保护。转载请注明原作者出处!