约束整理
// 约束
// 非空约束
not null;
// 唯一约束
unique;
// 主键约束 = 非空 + 唯一,最好只能作用于主键
primary key;
// 默认约束,若为空则为默认值
default;
// 检查约束,保证字段满足某些条件
check;
// 自增
auto_increment;
// 外键约束,让两张表之间建立链接,保证一致性以及完整性
foreign key;
// 创建时建立约束
// create table 表名 (
// 字段名 书记类型,
// ...
// [constrant] [外键名称] foreign key (外键字段名) references 主表(主表列名)
// );
// 创建后修改约束
// alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表(主表列名);
// 删除约束
// alter table 表名 drop foreign key 外键名称;
// -----------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------
// 外键的删除与更新行为
// 删除/更新对应记录时,检查是否有对应外键,如果有则不允许继续操作
// 一下两个操作等价
no action;
restrict;
// 删除/更新对应记录时,检查是否有对应外键,如果有则同步更新在子表中的记录
cascade;
// 删除对应记录时,检查是否有对应外加按,如果有,则将子表中的对应记录设置为null
set null;
// 父表有变更时,子表将外键列设置成一个默认值
set default;
// 语法示例
// alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update cascade on delete cascade;

浙公网安备 33010602011771号