约束:多个约束之间用空格分隔
创建用户表:
create table user(
id int primary key auto_increment comment '主键',
name varchar(10) not null unique comment '姓名',
age int check ( age > 0 && age < 120 ) comment '年龄',
status char(1) default '1' comment '状态',
gender char(1) comment '性别'
) comment '用户表'
在emp表insert数据时,如果dept_id的值为6,
插入时由于外键的数据一致性要求会去检测dept表里是否存在id为6的部门,
因为不存在所以会insert失败。
alter table emp add constraint fk_emp_dept_id foreign key(dept_id)
references dept(id) on update cascade on delete cascade
上面指定了update和delete 主表dept时是cascade行为,那么把dept的财务部的id改为8的话则在emp中所有的引用财务部的dept_id都变为8;如果删除研发部,那么emp中所有dept_id为1的数据都会被删除。主表影响子表的数据。