1.主键约束
(1).建表时添加带自增的主键约束
create table 表名(列名 类型 primary key auto_increment,列名 类型);
(2).修表时添加带自增的主键约束
alter table 表名 add primary key(列名);
alter table 表名 modify 主键列名 类型 auto_increment;
(3).删除主键约束
主键约束如果有自增长则要先删除自增长。
alter table 表名 modify 主键列名 类型;
alter table 表名 drop primary key;
2.外键约束
(1).建表时添加外键约束
create table 表名(列名 类型,列名 类型,constraint 外键名 foreign key(列名)
references 表名(列名));
(2).修表时添加外键约束
alter table 表名 add constraint 约束名 foreign key(列名) references 表名(列名);
(3).删除外键约束
alter table 表名 drop foreign key(列名);
3.唯一约束
(1).建表时添加唯一约束
create table 表名(列名 类型,列名 类型 unique );
(2).修表时添加唯一约束
alter table 表名 add constraint 约束名 unique(name);
(3).删除唯一约束
alter table 表名 drop key 约束名;
4.非空约束
(1).建表时添加非空约束
create table 表名(列名 类型,列名 类型 not null);
(2).修表时添加非空约束
alter table 表名 modify 列名 类型名 not null;
(3).删除非空约束
alter table 表名 modify 列名 类型名 null;
5.检查约束
MySQL不具备哦!!