一:添加約束
1.添加唯一約束:alter table student add constraint uk_name unique(name);
2.添加外鍵約束:
alter table 表名
add constraint fk_字段名
foreign key (字段名) references 關聯的表名(關聯的字段名)
3.添加check約束:
alter table 表名
add constraint CK_字段名
check (條件表達式)
4.添加默認值約束:
alter table 表名
add constraint DF_字段名
default '默認值' for 字段名
5.刪除約束:
alter table 表名
drop constraint 約束名
二:查詢約束和觸發器
1.查詢約束:SELECT * FROM information_schema.`TABLE_CONSTRAINTS`;(可以通過where table_name='student'來約定只查詢某個表的約束)
其實每次添加約束都是將添加的約束的信息存儲到了information_schema這個schema的table_constraints表里;(mysql里``和bash的很像,也是執行``內的表達式而不是將里面作為字符串使用)
2.查看所有的觸發器:select* from information_schema.triggers;