對數據表中數據的限制條件叫表的約束,目的是爲了保證表中記錄的完整和有效。例如非空、惟一等。mysql
1 經過查看建表語句 查看錶中的約束sql
show create table tb_name;
2 經過檢查約束表 查看約束數據庫
select * from information_schema.table_constraints where table_name='usr';
Tips:在MySQL數據庫中 全部的約束都存放在information_schema.table_constraints表中~~~~參看~~~~~information_schema數據庫介紹django
在MySQL中主鍵是一種約束,也是一種索引。被設爲主鍵的字段 要求 不重複 不能爲空 不能缺省。主鍵的做用爲惟一的肯定表中的一條記錄。須要注意的是 當主鍵是複合主鍵時,不重複的含義是 組合起來不重複便可,單列值容許重複,但不容許同時重複code
create table if not exists user( id int(11) auto_increment primary key, name char(64) not null default 'No Name' );
create table if not exists user( id int(11) auto_increment, name char(64) not null default 'No Name', primary key(id,name) ); -- 注意 聲明主鍵的完整格式應該是這樣的 -- constraint 約束名 primary key key_name
上邊的兩種方式都是在建立表時候指定主鍵約束,也能夠在表建成後添加主鍵約束orm
alter table tb_name add primary key(key_name) -- tb_name 表名 -- primary key() 括號裏填寫字段名,能夠是一個 也能夠是多個(複合主鍵)
-- 若是主鍵字段中包含有自增 auto_increment,須要先刪除自增屬性 在刪除主鍵 -- 若是沒有自增直接刪除主鍵便可 -- 刪除自增類型。 !!!注意 自增是類型 因此 可使用 修改字段的類型就ok --1 修改字段的名 修改後須要從新指定類型 alter table tb_name change col_name col_name int; --2 簡單的直接修改類型 alter table tb_name modify col_name int; -- 刪除掉 auto_increment屬性後能夠刪除主鍵 alter table tb_name drop primary key;
建立 1 中 建立的 id 就是單一的主鍵,須要注意,定義字段時聲明主鍵是沒法建立複合主鍵的。索引
建立 2 中的建立主鍵的方式 容許建立單一主鍵也容許建立複合主鍵,推薦使用這種方法進行主鍵的建立。ip
當處理多對多的關係時,須要建立中間表來進行多對多的關聯,此時中間表的主鍵成爲聯合主鍵,他能惟一的肯定多對多的若干實體間的對應關係。聯合主鍵也能夠是複合主鍵即聯合主鍵由第三表的多個列複合而成。保持惟一。rem
外鍵依託於具備惟一約束的字段,通常來說主鍵會多一些,可是也能夠不是主鍵。前提是它們具備惟一約束。外鍵依賴的字段能夠來自本(自參照表)也能夠是其餘表,通常的是其餘表的狀況多一些。此時要求 主表必須已經存在於數據庫中,或者是當前正在建立的表。若是被依賴字段在本表中,那麼這樣的約束結構稱爲 自參照完整性。文檔
Tips: 雖然主鍵不容許爲空不容許重複,可是外鍵能夠爲空,也能夠重複。外鍵約束要求每個值都來自於主鍵,特別的插入空值時,MySQL不支持缺省,必須顯式表達出來,例如
insert into user(name,leader_id) values('monkey',null); -- 顯示的寫出 null 不然 會報錯 insert into user(name,leader_id) values('monkey'); -- 是不合法的語句
[constraint <fk_name>] foreign key(fk_col_name [,col_name2,…]) references <tb_name> (pk_col_name1 [,pk_col_name2,…]) -- fk_name 外鍵名 -- fk_col_name 設置外鍵字段的字段名 -- tb_name 外鍵關聯的主表表名 -- pk_col_name 主表被依賴的字段名
alter table tb_name add [constriant <fk_name>] foreign key(fk_name) references tb_name(pk_col_name) -- fk_name 外鍵名 -- fk_col_name 設置外鍵字段的字段名 -- tb_name 外鍵關聯的主表表名 -- pk_col_name 主表被依賴的字段名
例子
-- 建立主表 leader create table leader( id int(11) primary key auto_increment, name char(64) not null ); -- 建立外鍵所在表 employee create table employee( id int(11) primary key auto_increment, name char(64) not null, leader_id int(11), constraint fk_leader_employee foreign key(leader_id) references leader(id) ); create table employee( id int(11) primary key auto_increment, tid char(11), name char(64) not null, leader_id int(11), constraint fk_leader_employee foreign key(name) references leader(name) );
同刪除主鍵相似,不一樣的是,每張表只有一個主鍵,所以不須要指定主鍵名,可是外鍵 外鍵名是不虛指定的。
alter table tb_name drop foreign key fk_name; -- fk_name 外鍵約束名 也就是 constrain 後面的名字
惟一約束就是要求字段中的值是惟一,特別的它容許爲空,但只能出現一個空值。惟一約束能夠確保一列不出現重複值,或者若干列不出現重複的組合。
create table leader( id int(11) primary key auto_increment, name char(64) unique, ); -- name 字段惟一 ,不容許有重名的出現
create table leader( id int(11) primary key auto_increment, name char(64), constraint uq_name unique key(name) -- 在這裏寫惟一約束 與主鍵相似 );
alter table tb_name add [constaint uq_name] unique key(uq_col_name) -- tb_name 要修改的表名 -- uq_name 約束名<索引名> -- uq_col_name 聲明惟一的字段名 -- 約束名 能夠不寫
alter table tb_name drop index fk_name; -- tb_name 表名 -- fk_name 約束名<索引名>
default 應該被稱爲默認值約束,他也是一種約束,做用和簡單,指定某字段的默認值
create table user( id int(11) auto_increment primary key, age int(4) default 18, name char(64) not null );
alter table tb_name change col_name col_type[size] default <默認值> -- 就是簡單的修改表的結構語句 alter table user change col_name col_type[size] {default 默認值 | not null | null }
-- 修改表結構 將默認值 設置爲null alter table tb_name change col_name col_type[size] default null; -- 或者 -- 修改表結構 修改字段的類型 alter table tb_name modify col_name col_type[size];
MySQL不支持檢查約束,官方文檔上說,即便設置了檢查約束,可是mysql不會強制的要求這樣作。建立以後在information_schema.table_constraints 找不到相關的記錄。甚至他可能自己就不存在。
檢查約束通常來講是爲了知足用戶實際的實體完整性而存在的。例如 身高應該 high > 10 and high < 280
check <約束條件> -- 例子 create table userinfo( id int(11) auto_increment primary key, high int(4) default 160, weight int(4) default 50, check(heigh > 10 and heigh < 280) );
alter table userinfo add constraint check_name check(約束條件) -- 例如 alter table userinfo add constraint check_weight check(weight > 10 and weight < 300);
alter table tb_name drop constraint check_name; -- 例如 alter table userinfo drop constraint check_weight; -- 值得注意的是,mysql 雖說建立成功了,可是在information_schema 數據庫的 table_constraints 表中並不能找到相關的信息。MySQL不支持檢查約束。