不为空和默认值约束
不能重复 unique
插入两个空,mysql可以吗?可以
联合唯一 unique
例如ip可以重复,port可以重复,但是ip+port是不能重复的
主键约束 第一个定义为非空+唯一的那一列会成为这张表的主键,一张表只能定义一个主键。
如果指定指定的那个会成为primary key 用关键字primary key定义
联合主键
自增auto_increment:自对数字有效,自带非空约束,至少是unique的约束之后才能使用自增
外键约束
要删除班级为1的删不了,因为里边有人
更新也有约束
on update cascade on delete cascade
create table stu2(
id int primary key auto_increment,
name char(12) not null,
gender enum('male','female') default 'male',
class_id int,
foreign key(class_id) references class(cid) on update cascade on delete cascade
)
foreign key(自己的字段) references 外表(外表字段)
外表字段必须是唯一的