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;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM