Mysql约束控制

主键约束

primary key(字段名);//表级完整性约束

字段名 数据类型[其他约束条件] primary key;//列级完整性约束

 

 修改表stu2的主键,删除原来的主键,添加sno、cno为主键

alter table stu2 drop primary key,add primary key(sno,cno);

 

外键约束

alter table stu2 add [外键名]foreign key [id];

 

非空约束

alter table 表名 字段名 数据类型 not null;

 

唯一性约束

字段名 数据类型 unique;

 

默认约束

字段名 数据类型[其他约束条件]default 默认值;

 

自增约束

create table 表名(
属性名 数据类型 AUTO_INCREMENT,
....
);

 

检查约束

create table 表名(
属性名 数据类型 CHECK(sage>=15,//将sage设置为15以上检查约束
..
);

 

删除约束

alter table 表名 drop foreign key 约束名;

 

posted @ 2020-12-06 17:09  简笺  阅读(170)  评论(0编辑  收藏  举报