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