目录
一.常见的约束
非空约束
唯一约束
主键约束
外键约束
二.非空约束
1.添加非空约束
创建表时添加
create table
修改表数据类型时添加
alter table 表名 modify 字段名 新数据类型 not null;
2.删除非空约束
alter table 表名 change column 字段名 字段名 新数据类型;
alter table student modify age int(10);
三.唯一性约束
1.添加唯一性约束
1.1.直接add
1.1.1表级约束
alter table student add unique(name);
1.1.2联合唯一性约束
alter table 表名 add unique(字段名1,字段名2);
字段名1与字段名2的组合具有唯一性
比如代码中:同一个班级里不能有同一个id;
2.删除
alter table 表名 drop index 字段名;
四.主键约束
一个表中,只能有一个主键。一个主键可以由一个或多个列构成。
主键约束相当于唯一性与非空约束的组合
对一个字段既添加唯一性约束,又添加非空约束