摘要:本文学习了如何使用DDL语句使用约束。
环境
Windows 10 企业版 LTSC 21H2
MySQL 5.7.40
1 简介
1.1 定义
约束是在创建表时,为某些列添加特定的规则,保证数据满足用户的要求。
1.2 作用
约束添加后,在向表中添加和更新数据时,必须要满足约束,否则不能执行操作。
2 操作
2.1 主键约束
2.1.1 定义
主键约束是一个列或者多个列,其值能唯一地标识表中的每一行,这样的一列或多列称为表的主键。
主键约束相当于唯一约束和非空约束的组合,主键约束列不允许重复,也不允许出现空值。
当创建主键约束时,系统默认会建立对应的唯一索引。
2.1.2 自增
数据库提供了一个自增的数字,专门用来自动生成主键值,不需要用户维护,自增数从1开始,以1递增。
2.1.3 使用
使用primary key
添加主键约束,使用auto_increment
设置主键自增。
在创建表时添加单列主键约束,主键自增:
sql1 2 3 4 5 6 7
| mysql> create table test ( -> id int(11) primary key auto_increment comment '编号', -> name varchar(50) comment '姓名' -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在创建表时添加联合主键约束:
sql1 2 3 4 5 6 7 8
| mysql> create table test ( -> id int(11) comment '编号', -> name varchar(50) comment '姓名', -> primary key(id, name) -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在修改表时添加主键约束:
sql1 2 3 4 5
| mysql> alter table test add primary key(id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
在修改表时删除主键约束:
sql1 2 3 4 5
| mysql> alter table test drop primary key; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
2.2 外键约束
2.2.1 定义
外键约束用来在两个表的数据之间建立链接,它可以是一列或者多列,一个表可以有一个或多个外键。
外键可以为空值,若不为空值,则每一个外键的值必须等于另一个表中主键的某个值。
主表和子表:
- 主表:对于两个具有关联关系的表而言,存储主要信息的表就是主表。
- 子表:对于两个具有关联关系的表而言,存储详细信息的表就是子表。
比如一个订单主表对应多个订单子表,以及一个信息主表对应一个信息详情表。
2.2.2 使用
使用foreign key
添加外键约束。
创建主表并定义主键约束:
sql1 2 3 4 5 6 7
| mysql> create table man ( -> id int(11) primary key comment '编号', -> name varchar(50) comment '姓名' -> ) engine=innodb default charset=utf8mb4 comment='主表'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在创建子表时添加外键约束:
sql1 2 3 4 5 6 7 8 9
| mysql> create table child ( -> id int(11) primary key comment '编号', -> m_id int(11) comment '主表编号', -> name varchar(50) comment '姓名', -> constraint fk_m_id foreign key(m_id) references man(id) -> ) engine=innodb default charset=utf8mb4 comment='子表'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在修改子表时添加外键约束:
sql1 2 3 4 5
| mysql> alter table child add constraint fk_m_id foreign key(m_id) references man(id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
在修改子表时删除外键约束:
sql1 2 3 4 5
| mysql> alter table child drop foreign key fk_m_id; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
2.3 唯一约束
2.3.1 定义
唯一约束要求该列唯一,允许为空,但只能出现一个空值。
唯一约束可以确保一列或者几列不出现重复值。
2.3.2 使用
使用unique
添加唯一约束。
在创建表时添加单列唯一约束:
sql1 2 3 4 5 6 7
| mysql> create table test ( -> id int(11) primary key comment '编号', -> name varchar(50) unique comment '姓名' -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在创建表时添加联合唯一约束:
sql1 2 3 4 5 6 7 8
| mysql> create table test ( -> id int(11) primary key comment '编号', -> name varchar(50) comment '姓名', -> constraint uk_id_name unique(id, name) -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在修改表时添加唯一约束:
sql1 2 3 4 5
| mysql> alter table test add unique uk_name(name); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
在修改表时删除唯一约束:
sql1 2 3 4 5
| mysql> alter table test drop index uk_name; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
2.4 非空约束
2.4.1 定义
非空约束可以用来约束该列的取值不能为空,对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统就会报错。
2.4.2 使用
使用not null
添加非空约束。
在创建表时添加非空约束:
sql1 2 3 4 5 6 7
| mysql> create table test ( -> id int(11) primary key comment '编号', -> name varchar(50) not null comment '姓名' -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在修改表时添加非空约束:
sql1 2 3 4 5
| mysql> alter table test change column name name varchar(50) not null; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
在修改表时删除非空约束:
sql1 2 3 4 5
| mysql> alter table test change column name name varchar(50) default null; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql>
|
2.5 默认值约束
2.5.1 定义
默认值约束用来指定某列的默认值。
2.5.2 使用
使用default
添加默认值约束。
在创建表时添加默认值约束:
sql1 2 3 4 5 6 7
| mysql> create table test ( -> id int(11) primary key comment '编号', -> name varchar(50) default 'none' comment '姓名' -> ) engine=innodb default charset=utf8mb4 comment='测试'; Query OK, 0 rows affected (0.01 sec)
mysql>
|
在修改表时添加默认值约束:
sql1 2 3 4 5
| mysql> alter table test change column name name varchar(50) default 'none'; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql>
|
在修改表时删除默认值约束:
sql1 2 3 4 5
| mysql> alter table test change column name name varchar(50) default null; Query OK, 1 row affected (0.01 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql>
|
条