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;