## mysql 常用约束
目录
- 主键约束
- 默认约束
- 非空约束
- 外键约束
内容
主键约束:
字段后加 primary key 或者使用 primary key (字段) 来设定
默认约束:
字段后加 default 'string' 或 default number (也可默认设置时间戳 default current_timestamp())
非空约束:
字段后加 not null
外键约束:
外键约束需要满足三个条件 : 引擎 innodb , 外键字段必须为索引类型,外键字段与关联字段类型必须一致。
①constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)
②constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)+ on delete cascade
③constraint 外键名 foreign key (外键字段) preferences 表名(关联字段)+ on delete set null
注:delete 处也可使用 update 实现更新时的效果