MySQL修改约束

1. 添加列级约束

alter table 表名 modify column 字段名 字段类型 新约束

例如:alter table student modify column age int not null;

添加字段唯一:alter table student modify column stu_nu varchar(20) unique;

2. 添加表级约束

alter table 表名 add [constraint 约束名] 约束类型(字段名) [外键的引用]

例如添加外键:alter table student add constraint fk_stu_class foreign key(class_id) references class(id);

添加主键:alter table student add constraint pk_stu primary key(id);

添加字段唯一:alter table student add unique(stu_nu);

3.删除列级约束

例如删除非空约束

alter table student modify column age int null;

删除主键:

alter table student drop primary key;

删除唯一键(可以使用show index from student查看唯一)

alter table student drop index index_name;

删除外键:

alter table student drop foreign key fk_name;

 

posted @ 2019-08-09 09:20  风来与你安  阅读(6481)  评论(0编辑  收藏  举报