MySQL刪除約束
將t_student
刪除外鍵約束:alter table 表名 drop foreign key 外鍵(區分大小寫);
alter table t_student drop foreign key fk_classes_id;
刪除主鍵約束:alter table 表名 drop primary key ;
alter table t_student drop primary key;
刪除約束約束:alter table 表名 drop key 約束名稱 ;
alter table t drop key uk;
MySQL添加約束
將t_student中的約束
添加外鍵約束:alter table 從表 add constraint???約束名稱?foreign key 從表(外鍵字段) references 主表(主鍵字段);
alter table t_student add constraint fk_classes_id_1 foreign key(classes_id) references t_classes(classes_id);
添加主鍵約束:alter table 表 add constraint ?約束名稱 ?primary key ?表(主鍵字段);?
alter table t_student add constraint pk primary key(student_id);
添加唯一性約束:alter table 表 add constraint ?約束名稱 unique ?表(字段);?
alter table t_student add constraint uk unique(email);
MySQL修改約束,其實就是修改字段
alter table t_student modify student_name varchar(30) unique;
mysql對有些約束的修改時不支持,所以我們可以先刪除,再添加