一.表基本操作
1.当前数据库内创建表
2.查看表
3.删除表
4.修改表结构
5.复制表(结构)
二.表约束创建
1.约束的作用
2.约束的类型
3.演示
一.表基本操作
1.当前数据库内创建表
CREATE TABLE 表名(
列名 列数据类型,
列名 列数据类型);
mysql> create table stable(
-> name char(10),
-> id int);
Query OK, 0 rows affected (0.02 sec)
2.查看表
(1)查看数据库中的表
mysql> show tables from SLB;
+---------------+
| Tables_in_SLB |
+---------------+
| stable |
| t1 |
| t2 |
| t3 |
| t4 |
+---------------+
5 rows in set (0.00 sec)
(2)查看表结构
#describe 表名
#show columns from 表名
mysql> describe SLB.stable;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| id | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> show columns from SLB.stable;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(10) | YES | | NULL | |
| id | int | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
(3)垂直显示表中各列信息
mysql> show create table SLB.stable\G;
*************************** 1. row ***************************
Table: stable
Create Table: CREATE TABLE `stable` (
`name` char(10) DEFAULT NULL,
`id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
3.删除表
drop table 表名
mysql> drop table stable;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_SLB |
+---------------+
| t1 |
| t2 |
| t3 |
| t4 |
+---------------+
4 rows in set (0.01 sec)
4.修改表结构
(1)修改列类型:ALTER table 表名 modify 列名 列数据类型
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> ALTER table t1 modify id char(20);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | char(20) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
(2)修改列名:ALTER table t1 change 原列名 新列名 列数据类型
mysql> ALTER table t1 change id stuid char(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| stuid | char(20) | YES | | NULL | |
| name | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
5.复制表(结构)
create table 新表名 like 被复制表名(源表)
mysql> desc stable;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> create table t1 like stable;
Query OK, 0 rows affected (0.06 sec)
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
create table 新表名 select * from 被复制表名(源表)
mysql> create table t2 select * from stable;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> desc t2;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
二.表约束创建
1.约束的作用
约束主要用于保证数据库的完整性,表中数据有相互依赖时,可以保护相关的数据不被删除。
2.约束的类型
(1)根据约束数据列的限制:单列约束(每个约束只能约束一列),多列约束(每个约束可以约束多列数据)
(2)根据约束的作用范围:列级约束(作用在一个列上,直接跟在列的后面 ),表级约束(作用在多个列上,不与列写一起,需要单独定义 )
(3)常见约束
NOT NULL:非空约束,规定某个字段不能为空
UNIQUE:唯一约束,规定某个字段在整个表中是唯一的,不允许数据重复
注意:唯一约束是为了确保所在的字段或者字段组合不出现重复值;唯一约束的字段允许出现多个NULL;同一张表内可建多个唯一约束;唯一约束可由多列组合而成;新建唯一约束时MySQL会为之建立对应的索引;如果不设置唯一约束名,该唯一约束默认与列名相同;删除唯一约束时使用“aletr table 表名 drop index 唯一约束名;”来删除。
PRIMARY KEY:主键约束,是非空且唯一的
注意:一个表只允许一个主键,创建主键时也会创建相应的索引。删除主键约束时使用“alter table 表名 drop primary key;”来删除
FOREIGN KEY:外键约束,建立主表与从表的约束关系
注意:从表外键列的值必须在主表参照列值的范围内,或者为空(也可以加非空约束,强制不允许为空);当主表的记录被从表参照时,主表记录不允许被删除; 外键参照的只能是主表主键或者唯一键,保证子表记录可以准确定位到被参照的记录;删除外键时使用“aletr table 从表名 drop foreign key CONSTRAINT显示的名称;”
CHECK:检查约束
DEFAULT:默认值约束
3.演示
(1)非空约束
注意:取消约束时使用alter命令更改列时不指定约束即可
mysql> create table t3( id int, name char(20) not null);
Query OK, 0 rows affected (0.02 sec)
mysql> show create table SLB.t3\G;
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int DEFAULT NULL,
`name` char(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
(2)唯一约束
mysql> create table t4(
-> id int unique,
-> name char(20));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table SLB.t4\G;
*************************** 1. row ***************************
Table: t4
Create Table: CREATE TABLE `t4` (
`id` int DEFAULT NULL,
`name` char(20) DEFAULT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
(3)主键约束
mysql> create table t5(
-> id int primary key,
-> name char(10) unique);
Query OK, 0 rows affected (0.01 sec)
mysql> show create table SLB.t5\G;
*************************** 1. row ***************************
Table: t5
Create Table: CREATE TABLE `t5` (
`id` int NOT NULL,
`name` char(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
(4)外键约束
foreign key(外键列名) references (主表参照列)
mysql> create table t7(
-> t7id int primary key,
-> name char(20),
-> tid int,
-> foreign key(tid) references t6(tid));
Query OK, 0 rows affected (0.02 sec)
mysql> show create table SLB.t7\G;
*************************** 1. row ***************************
Table: t7
Create Table: CREATE TABLE `t7` (
`t7id` int NOT NULL,
`name` char(20) DEFAULT NULL,
`tid` int DEFAULT NULL,
PRIMARY KEY (`t7id`),
KEY `tid` (`tid`),
CONSTRAINT `t7_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `t6` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
#删除外键约束
mysql> alter table t7 drop foreign key t7_ibfk_1;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
t6主表中的id为1
t7从表中不允许修改主表设定的id
从表删除外键后即可修改
(5)检查约束
这列数据只允许等于设定的内容
mysql> create table t8(
-> gender char(10) check(gender in ('男','女')));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table SLB.t8\G;
*************************** 1. row ***************************
Table: t8
Create Table: CREATE TABLE `t8` (
`gender` char(10) DEFAULT NULL,
CONSTRAINT `t8_chk_1` CHECK ((`gender` in (_utf8mb4'男',_utf8mb4'女')))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
(6)默认值约束
mysql> create table t9(
-> id int unique default 1);
Query OK, 0 rows affected (0.01 sec)
mysql> show create table SLB.t9\G;
*************************** 1. row ***************************
Table: t9
Create Table: CREATE TABLE `t9` (
`id` int DEFAULT '1',
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)