一、主键约束(Primary Key)

每个数据表只能设置一个主键约束,通常用来标记数据值唯一存在,且不能为空,不能重复。

  1. alter table 数据表 add [constraint 约束名] primary key (列);
  1. alter table 数据表名 modify 列名 数据类型 auto_increment;
  2. alter table 数据表名 change 旧列名 列名 数据类型 auto_increment;
  3. alter table 数据表名 auto_increment = ;
  1. alter table 数据表 drop primary key
  2. # 删除主键约束后,不重复特性取消了,但是非空属性还存在,可以做以下修改
  3. alter table 数据表名 modify 列名 数据类型 null;

二、唯一约束(Unique Key)

列的值不能重复,可以为空,可以设置多个唯一约束

  1. alter table 数据表 add [constraint 约束名] unique key (列);
  1. alter table 数据表 drop index 约束名(默认为列名);

三、非空约束

列的值不能为空

  1. alter table 数据表名 modify 列名 数据类型 not null default '';

四、外键约束(foreign key)

可以设置多个外键约束,可以为空,可以重复,但是当前列的值不能随意填写,需从另一个数据表内寻找(当前列的值受到另一张表列的影响)

  1. alter table 数据表 add [constraint 约束名(fk_当前表_关联表)] foreign key (列) references 约束表 (列);
  1. alter table 数据表 drop foreign key 约束名;
  2. alter table 数据表 drop key 约束名;

五、检查约束(check)

在存值过程中,进行检查

  1. alter table 数据表 add [constraint 约束名] check(判断条件)