一、约束概述
2、外键约束检查
mysql> show variables like "foreign%"; -- 查看外键约束检查状态
mysql> SET foreign_key_checks=0; -- 关闭外键约束检查
mysql> SET foreign_key_checks=1; -- 开启外键约束检查,以保持表结构的完整性
二、唯一约束:unique应用
三、主键约束:primary key应用
1、在创建表时设置主键约束
2、在修改表时添加主键约束
mysql> alter table [表名] add primary key (字段); -- 添加主键
mysql> alter table [表名] drop primary key; -- 删除主键
mysql> alter table student add primary key (id);
mysql> alter table student drop primary key;
--参考:https://www.cnblogs.com/lpxblog/p/7773084.html
四、自动增长:auto_increment应用
约束字段为自动增长,被约束的字段必须同时被key约束。
--添加:alter table [表名] modify 列名 int auto_increment
mysql> alter table student add primary key (id); --必须先设置主键,才能自增长
mysql> alter table student modify id int(12) auto_increment;
mysql> alter table student auto_increment=10000;
--删除:alter table [表名] modify 列名 int
mysql> alter table student modify id int --默认大小11
mysql> alter table student modify id int(13); --修改大小了
五、检查约束