一、创建数据表
1、创建表的语法形式
2、使用主键约束
3、使用外键约束
4、使用非空约束
5、使用唯一性约束
6、使用默认约束
7、设置表的属性值自动增加
二、查看数据表结构
1、查看表的结构语句 describe
2、查看表详细结构语句 show create table
三、修改数据表
1、修改表名
2、修改字段的数据类型
3、修改字段名
4、添加字段
5、删除字段
6、修改字段的排列位置
7、更改表的存储引擎
8、删除表的外键约束
四、删除数据表
1、删除没有被关联的表
2、删除被其他表关联的主表
五、MySQL8.0的新特性
1、默认字符集改为 utf8mb4
2、自增变量的持久化
一、创建数据表
1、创建表的语法形式
create table <表名>
(
字段名1 数据类型 [列级别约束条件] [默认值],
字段名1 数据类型 [列级别约束条件] [默认值],
......
[表级别约束条件]
);
#案例创建表
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)
mysql> use test_db;
Database changed
mysql> create table tb_emp1
-> (
-> id int(11),
-> name varchar(25),
-> deptId int(11),
-> salary float
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_emp1 |
+-------------------+
1 row in set (0.00 sec)
mysql>
2、使用主键约束
主键约束:①数据唯一,不能重复;②不允许为空 ;主键可以唯一识别一条记录,类似于身份证和人之间的对应关系。
1)单字段主键
(1)在定义列的同时指定主键
字段名 数据类型 primary key [默认值]
#案例
mysql> create table tb_emp2
-> (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
(2)在定义所有列之后指定主键
[constraint <约束名>] primary key [字段名]
#案例
mysql> create table tb_emp3
-> (
-> id int(11),
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> primary key(id)
-> );
2)多字段主键
primary key [字段1,字段2,... ,字段n]
#案例
mysql> create table tb_emp4
-> (
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> primary key(name,deptId)
-> );
3、使用外键约束
外键是用在两个表的数据之间建立连接,可以是一列或者多列。一个表可以有一个或多一个外键,一个表的外键可以为空值,如果不为空值,则每一个外键值必须邓毅另一个表中主键的某个值。外键的主要作用是保证数据引用的完整性,定义外键后,不允许删除在另一个表中具有关联关系的行。
[constraint <外键名>] foreign key 字段1 [,字段2,... ] refernces <主表名> 主键列1 [,主键列2,... ]
#案例
mysql> create table tb_dept1
-> (
-> id int(11) primary key,
-> name varchar(22) not null,
-> location varchar(50)
-> );
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> create table tb_emp5
-> (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id)
-> );
4、使用非空约束
字段名 数据类型 not null
#案例
mysql> create table tb_emp6
-> (
-> id int(11) primary key,
-> name varchar(25) not null,
-> deptId int(11),
-> salary float
-> );
5、使用唯一性约束
不允许有空值,但是允许值为NULL
1)在定义完列之后直接指定唯一约束
字段名 数据类型 unique
#案例
mysql> create table tb_dept2
-> (
-> id int(11) primary key,
-> name varchar(22) unique,
-> location varchar(50)
-> );
2)在定义完所有列之后指定唯一约束
[constraint <约束名> ] unique(<字段名>)
#案例
mysql> create table tb_dept3
-> (
-> id int(11) primary key,
-> name varchar(22),
-> location varchar(50),
-> constraint sth unique(name)
-> );
6、使用默认约束
字段名 数据类型 default 默认值
#案例
mysql> create table tb_emp7
-> (
-> id int(11) primary key,
-> name varchar(25) not null,
-> deptId int(11) default 1111,
-> salary float
-> );
7、设置表的属性值自动增加
在插入数据时,默认自增字段id的值从1开始,每次添加一条新纪录,该值自动加1
字段名 数据类型 auto_increment
#案例
mysql> create table tb_emp8
-> (
-> id int(11) primary key auto_increment,
-> name varchar(25) not null,
-> deptId int(11),
-> salary float
-> );
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> insert into tb_emp8(name,salary) values('lucy',100),('lura',1200),('kevin',1500);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from tb_emp8;
+----+-------+--------+--------+
| id | name | deptId | salary |
+----+-------+--------+--------+
| 1 | lucy | NULL | 100 |
| 2 | lura | NULL | 1200 |
| 3 | kevin | NULL | 1500 |
+----+-------+--------+--------+
3 rows in set (0.00 sec)
二、查看数据表结构
1、查看表的结构语句 describe
describe 表名;
或者
desc 表名;
#案例
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
- NULL:表示该列是够可以存储NULL值。
- Key:表示该列是否已编制索引。PRI表示该列是表主键的一部分;UNI表示该列是unique索引的一部分;MUL表示在列中某个值允许出现次数。
- Default:表示该列是否有默认值,有的话指定是多少。
- Extra:表示可以化取的与给定列有关的附加信息,列如 AUTO_INCREMENTD等。
2、查看表详细结构语句 show create table
show create table <表名\G>;
还可以查看存储引擎和字符编码。
mysql> show create table tb_emp1\G;
*************************** 1. row ***************************
Table: tb_emp1
Create Table: CREATE TABLE `tb_emp1` (
`id` int DEFAULT NULL,
`name` varchar(25) DEFAULT NULL,
`deptId` int DEFAULT NULL,
`salary` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
三、修改数据表
1、修改表名
alter table <旧表名> rename [to] <新表名>;
#案例
mysql> alter table tb_dept3 rename tb_deptment3;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept1 |
| tb_dept2 |
| tb_deptment3 |
+-------------------+
11 rows in set (0.00 sec)
2、修改字段的数据类型
alter table <表名> modify <字段名> <数据类型>
#案例
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table tb_dept1 modify name varchar(30);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
3、修改字段名
alter talbe <表名> change <旧字段名> <新字段名> <新数据类型>;
#案例
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table tb_dept1 change location loc varchar(50);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| loc | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table tb_dept1 change loc location varchar(60);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4、添加字段
alter talbe <表名> add <新字段名> <数据类型> [约束条件] [first | after 已经存在的字段名]
- first 为可选参数,其作用是将新添加的字段设置为表的第一个字段;
- after 为可选参数,其作用是将新添加的字段添加到指定的“已存在字段名”的后面
- 默认将新添加的字段设置为数据表的最后一列。
1)添加无完整性约束条件的字段
mysql> alter table tb_dept1 add managerId int(10);
Query OK, 0 rows affected, 1 warning (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
2)添加有完整性约束条件的字段
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table tb_dept1 add column1 varchar(12) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
3)在表的第一列添加一个字段
mysql> alter table tb_dept1 add column2 int(11) first;
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column2 | int | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
4)在表的指定列后添加一个字段
mysql> alter table tb_dept1 add column3 int(11) after name;
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column2 | int | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
5、删除字段
alter table <表名> drop <字段名>;
#案例
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column2 | int | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table tb_dept1 drop column2;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
| column1 | varchar(12) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
6、修改字段的排列位置
alter table <表名> modify <字段1> <数据类型> first | after <字段2>
1)修改字段为表的第一个字段
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column1 | varchar(12) | YES | | NULL | |
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| managerId | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
2)修改字段到表的指定列之后
mysql> alter table tb_dept1 modify column1 varchar(12) after location;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int | YES | | NULL | |
| location | varchar(60) | YES | | NULL | |
| column1 | varchar(12) | YES | | NULL | |
| managerId | int | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
7、更改表的存储引擎
MySQL支持的存储引擎
| 引擎名 | 是否支持 |
|---|---|
| FEDERATED | 否 |
| MRG_MYISAM | 是 |
| MyISAM | 是 |
| BLACKHOLE | 是 |
| CSV | 是 |
| MEMORY | 是 |
| ARCHIVE | 是 |
| InnoDB | 是,默认的存储引擎 |
| PERFORMANCE_SCHEMA | 是 |
alter talbe <表名> engine=<更改后的存储引擎>;
#案例
mysql> show create table tb_deptment3 \G;
*************************** 1. row ***************************
Table: tb_deptment3
Create Table: CREATE TABLE `tb_deptment3` (
`id` int NOT NULL,
`name` varchar(22) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `sth` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> alter table tb_deptment3 engine=MyISAM;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table tb_deptment3 \G;
*************************** 1. row ***************************
Table: tb_deptment3
Create Table: CREATE TABLE `tb_deptment3` (
`id` int NOT NULL,
`name` varchar(22) DEFAULT NULL,
`location` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `sth` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
ERROR:
No query specified
8、删除表的外键约束
alter table <表名> drop foreign key <外键约束名>;
#案例
-> (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dept foreign key (deptId) references tb_dept1(id)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> show create table tb_emp9 \G;
*************************** 1. row ***************************
Table: tb_emp9
Create Table: CREATE TABLE `tb_emp9` (
`id` int NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptId` int DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dept` (`deptId`),
CONSTRAINT `fk_emp_dept` FOREIGN KEY (`deptId`) REFERENCES `tb_dept1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> alter table tb_emp9 drop foreign key fk_emp_dept;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table tb_emp9 \G;
*************************** 1. row ***************************
Table: tb_emp9
Create Table: CREATE TABLE `tb_emp9` (
`id` int NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptId` int DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dept` (`deptId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
四、删除数据表
1、删除没有被关联的表
drop table [if exists] 表1,表2,...,表n;
#案例
mysql> drop table if exists tb_dept2;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept1 |
| tb_deptment3 |
| tb_emp1 |
+-------------------+
2、删除被其他表关联的主表
创建两个表
mysql> create table tb_dept2
-> (
-> id int(11) primary key,
-> name varchar(22),
-> location varchar(50)
-> );
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> create table tb_emp
-> (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dep foreign key (deptId) references tb_dept2(id)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> show create table tb_emp\G;
*************************** 1. row ***************************
Table: tb_emp
Create Table: CREATE TABLE `tb_emp` (
`id` int NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptId` int DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_emp_dep` (`deptId`),
CONSTRAINT `fk_emp_dep` FOREIGN KEY (`deptId`) REFERENCES `tb_dept2` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
主动删除tb_dept2表
mysql> drop table tb_dept2;
ERROR 3730 (HY000): Cannot drop table 'tb_dept2' referenced by a foreign key constraint 'fk_emp_dep' on table 'tb_emp'.
先删除外键,在删除tb_dept2表
mysql> alter table tb_emp drop foreign key fk_emp_dep;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> drop table tb_dept2;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept1 |
| tb_deptment3 |
| tb_emp |
| tb_emp1 |
| tb_emp2 |
| tb_emp3 |
| tb_emp4 |
| tb_emp5 |
| tb_emp6 |
| tb_emp7 |
| tb_emp8 |
| tb_emp9 |
+-------------------+
12 rows in set (0.01 sec)
五、MySQL8.0的新特性
1、默认字符集改为 utf8mb4
2、自增变量的持久化
在mysql8.0之前,自增主键auto_increment 的值如果大于max(primary key)+1,在mysql重启后,会重置auto_increment=max(primary key)+1,这种现象在某些情况下会导致业务主键冲突或者其他难以发现的问题。

被折叠的 条评论
为什么被折叠?



