原文地址:http://www.cnblogs.com/chenkai/archive/2010/02/22/1671442.html。點擊開啟連結
Check約束
代碼
--建立表create table order_status ( id integer constraint order_status_pk primary key , status varchar(120) , last_modified date default sysdate ); --添加Check約束 alter table order_status add constraint order_status_ck check (status in('BeiJing','ShangHai','TaiWan')); alter table order_status add constraint order_status_id_ck check(id>10); --測試資料 insert into order_status(id,status) values(132,'ShangHai');
注意:
(1)在建立Check約束時,表中所有的行必須滿足添加約束條件, 如果在添加約束已經存在資料 條件不滿足會提示一個ORA-02293錯誤:如下:
一般處理在添加約束前最好清空原表中資料,上面提示錯誤有一個例外:在添加約束將其禁用. 這就涉及到"啟用約束"和"禁用約束"操作.後面詳解.還可以指定Enable Novalidate選項.從而使約束只適用於新添加資料.而不必管約束添加前資料.在插入資料時如果資料不符Check約束會提示一個ORA-02290錯誤.修改插入資料即可.
B:Not Null約束
代碼:
--添加上述建立表 not null約束 alter table order_status modify status constraint order_status_newref not null; alert table order_status modify last_modified not null; 格式:注意使用Modify 而非以前使用的Add Constraint 格式如下: alert table [表名] modify [表中列名] constraint [約束唯一標示] not null;
注意:
A1:在為Last_modified添加約束時 並沒有使用約束命名 是允許的 系統會自動分配一個名稱來標識. 但建議使用手動使用一個有意義的命名 出現錯誤容易判斷出處
C:Foreign Key外鍵約束.
代碼:
--建立一個外鍵約束測試表 create global temporary table test_orderstatus ( id integer constraint order_statustest_pk primary key, status_id number(15), status varchar(120), last_modifieddate date default sysdate ); --添加一列 先刪除 後添加 alter table test_orderstatus drop column status_id; alter table test_orderstatus add constraint order_status_modify_fk status_id reference order_status(id);
對Foreign key 使用On Delete Cascade子句.即指定當父表中刪除一行記錄時 回自動刪除子表中於其外部索引鍵關聯的行記錄.
--附帶自動串聯更新 alter table test_orderstatus drop column status_id; alter table test_orderstatus add constraint order_status_modify_fk status_id reference order_status(id) on delete cascade;
當在刪除了父表一條記錄時可以不刪除子表記錄. 全部設定字表中於外部索引鍵關聯所有記錄為Null 則使用On Delete Cascade Null子句. 同樣加在 外鍵約束後位置.當刪除一條父表記錄時.字表所有通過當前外鍵同父表關聯的資料都設定為空白值.
D:Unique約束
--添加Unique約束 alter table order_status add constraint order_status_uq unique(status);
E:刪除約束(Delete References)
--刪除剛剛設定的Unique約束
alter table order_status
drop constraint order_status_uq;
當需要手動編碼刪除約束時. 找到約束唯一命名是關鍵. 所以推薦建立使用有意義的約束命名.容易識別
禁用約束(Disable References)
--在建立時就禁用約束alter table order_statusadd constraint order_status_uq unique(status) disable;
添加約束預設是在建立後自動啟用的. 建立時可以直接禁用 直接尾部加一個Disable子句 如果在建立後 在來禁用約束則使用Alter table .....Disable Constraint子句
--使用中禁用約束
alert table order_status
disable constraint order_status_uq;
E:啟用約束(ReStart References)
--啟用已經建立約束 alert table order_status enable constraint order_status_uq;
約束預設在建立後自動啟用.要啟用約束注意當前表中所有資料必須滿足約束條件. 不然會包ORA-02293錯誤.當然有時為了需要可以只對新插入資料進行約束.原始依然存在,使用Alter Table.......Enable Novalidate. 預設是Enable Constraint.
--只對新插入的資料才執行當前約束alert table order_statusenable novalidate constraint order_status_uq;
shangmian我們在用到上面第一個Check約束時也談到這個問題.當我們建立Check約束,因為表中在建立約束前原來還存在資料 其中這些資料有些不符合當前約束的 但是我們不想刪掉. 那麼使用Enable Novalidate子句就是一個很好選擇, 它子對新插入的資料其約束. 未經處理資料進行保留.
G:延遲約束(Deferred Constraint)
延遲約束是在事務被提交時強制執行的約束.添加約束時可以通過Deferrable子句來指定約束為延遲約束. 約束一但建立以後, 就不能修改為Deferrable延遲約束.唯一辦法: 刪除該約束,只能在建立指定為延遲約束即可.
--當前來指定上述建立的唯一約束為延遲約束 先刪除已經建立唯一約束 --在建立中再次指定為延遲 alter table order_status drop constraint order_status_uq alert table order_status add constraint order_status_uq unique (status) deferrable initially deferred;
注意上面再添加約束時. 可以將其標識為Initially Immediate 或Initially Deferred.
其中Initially Immediate意思是每次向表中添加資料,修改資料或是從表中刪除資料時.都要檢查這個約束.(這與約束預設行為相同). 而Initially Deferred.意思是只有事務被提交時才檢查這個約束. 上述我們指定的Deferred . 即在只有事務提交時才檢查該約束.
H:擷取關於約束系統資訊
可以通過查詢User_Constraints表獲得當前所有關於約束的系統資訊.下面關於約束User_Constraints表常用資訊欄位說明:
Owner——約束所有者
Constraint_name——約束名稱
Constraint_Type——約束類型:C:代表Check或Not Null約束. P:主鍵約束. R:外鍵約束. U:唯一約束. V:Check option約束. O:Readonly 唯讀約束
Table——name——約束定義針對錶名
Status_約束的狀態 Enable或Disable 可用或不可用
Deferrable:是否為延遲約束 值為:Deferrable或Not Deferred.
Deferred:是否採用延遲 值為:IMMEDIate或Deferred.
--查看全部的關於order_status'資料表條件約束資訊 select * from user_constraints where table_name='order_status'
shangshu是關於表的約束. 對於表中特定的列只需查詢User_cons_columns表即可 不在贅述.