Constraint基礎概念
?
??? 一直對constraint的概念比較模糊,沒有系統得學習過一次。這次專門來學習一下這方面的內容。其實如果不用到REF constraint的話,這部分還是比較簡單明晰的,關鍵是要記住創建和修改constraint的幾個語法,這很重要。
首先來看一下《SQL Reference》中對于Constraint的說明:
?
???
?
??? 下面說一下我的認識:
?
1、Constraints的目的:
?
????? 設立Constraint就是為了讓數據滿足某些規則。
2、Constraint的類型:
?
????? not null??? (不能為空)
????? unique????? (值必須唯一)
????? primary key (not null + unique)
????? goreign key (該表值必須在外鍵表中存在)
????? check?????? (自己加的條件)
????? ref???????? (不熟)
??? 注:Constraints不但可以建立在Table上,也可以建立在View上。
3、Constraint的狀態:
?
????? ① Deferrable
????? 該參數用于指定是否可以是同set語句來進行臨時控制constraint,時約束在commit時才生效
????? DEFERRABLE:可以使用set constraint字句
????? NOT DEFERRABLE:不可以使用set constraint字句(默認)
?
????? ② Initially
????? 該參數用于建立默認的DEFERRABLE類型約束
????? INITIALLY一般都要和IMMEDIATE、DEFERRED一起使用
????? INITIALLY IMMEDIATE:在執行SQL時違反約束即報錯(默認)
????? INITIALLY DEFERRED:在提交時才報錯
?
????? ③ Validate | NoValidate
????? 該參數一般與Enabled和Disabled屬性搭配使用
?
????? ④ Enable
????? 該參數確認約束應用于數據
????? ENABLE VALIDATE:將驗證已經存在的和之后的操作是否符合約束(默認)
????? ENABLE NOVALIDATE:不驗證已經存在的數據,但對之后進行的操作有效
?
????? ⑤ Disable
????? 該參數使約束失效
????? DISABLE VALIDATE:約束失效標注,可用于暫時導入大量數據時,不進行索引更新
????? DISABLE NOVALIDATE:約束失效,并不保證約束是否正確,即不保證已有數據滿足約束(默認)
?
????? ⑥ Rely
????? Rely和Norely只能用在 ALTER TABLE MODIFY constraint 語句中
????? Rely:告訴Oracle,不必對NOVALIDATE模式的約束的數據進行信任,即需要檢驗以前的數據
????? (這個沒用過,實在搞不準確切含義,還是把文檔的內容直接放上來)
?
?????
?
4、set語句
?
?????
?
?
?
?
----------------------------------------------------------------------------------------------------
轉一篇Constraint的文章
----------------------------------------------------------------------------------------------------
?
?
constraints 三個需要注意的地方
?
1. deferrable
?
一個constraint如果被定義成deferrable那么這個constraints可以在deferred和imediate兩種狀態相互轉換。deferred只在transaction中有效,也就是只可以在transaction過程中使constraint失效,但如果transaction commit的話,transaction會變成immediate。
?
SQL> create table cons_parent (id number(10),name varchar2(10));
Table created.
?
SQL> create table cons_child (id number(10),name varchar2(10));
Table created.
?
SQL> alter table cons_parent add primary key (id);
Table altered.
?
SQL>alter table cons_child add constraints chi_fk_par foreign key (id)
2?? references cons_parent(id);
Table altered.
?
SQL> alter table cons_child add constraints chi_fk_par foreign key (id)
2 references cons_parent(id);
Table altered.
?
一個constraint默認是NOT DEFERRABLE的
?
SQL> select constraint_name||' '||deferrable from all_constraints
2??? where constraint_name='CHI_FK_PAR';
?
CONSTRAINT_NAME||''||DEFERRABLE
---------------------------------------------
CHI_FK_PAR NOT DEFERRABLE
?
NOT DEFERRABLE的不能在deferred和imediate兩種狀態相互轉換
?
SQL> set constraints chi_fk_par deferred;
SET constraints chi_fk_par deferred
*
ERROR at line 1:
ORA-02447: cannot defer a constraint that is not deferrable
?
SQL> alter table cons_child drop constraints chi_fk_par;
Table altered.
?
SQL> alter table cons_child add constraints chi_fk_par foreign key (id)
2??? references cons_parent(id) deferrable;
Table altered.
?
SQL> select constraint_name||' '||deferrable from all_constraints
2??? where constraint_name='CHI_FK_PAR';
?
CONSTRAINT_NAME||''||DEFERRABLE
---------------------------------------------
CHI_FK_PAR DEFERRABLE
?
一個constraint如果被定義成deferrable那么這個constraints可以在deferred和imediate兩種狀態相互轉換
?
SQL> set constraints chi_fk_par immediate;
Constraint set.
?
SQL> insert into cons_child values (2,'llll')
insert into cons_child values (2,'llll')
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.CHI_FK_PAR) violated - parent key not found
?
SQL> set constraints chi_fk_par deferred;
Constraint set.
?
SQL> insert into cons_child values (2,'llll');
1 row created.
?
SQL> set constraints chi_fk_par immediate;
SET constraints chi_fk_par immediate
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.CHI_FK_PAR) violated - parent key not found
?
deferred只在transaction中有效,也就是只可以在transaction過程中使constraint失效,但如果transaction commit的話,transaction會變成immediate。
?
SQL> commit;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-02291: integrity constraint (SYSTEM.CHI_FK_PAR) violated - parent key not found
deferrable會影響CBO的計劃,并且正常情況下沒有應用的必要,所以建議不要修改,而用系統默認的non deferrable
?
2. enable/disable validate/novalidate
?
enable/disable對未來的數據有約束/無約束。
?
validate/novalidate對已有的數據有約束/無約束。
?
如果加約束到一個大表,那么ORACLE會LOCK這個表,然后SCAN所有數據,來判斷是否符合CONSTRAINT的要求,在繁忙的系統里顯然是不合適的。所以用enable novalidate比較合適,因為ORACLE僅僅會LOCK表一小段時間來建立CONSTRAINT,當CONSTRAINT建立后再VALIDATE,這時檢驗數據是不會LOCK表的。
?
這方面很多書上都有例子,就不在這里累述了
?
3.REFERENCE 讓人疑惑的地方
?
SQL>? create table wwm_father (id number,name varchar2(10),primary key (id,name))
Table created.
?
SQL> create table wwm_child (id number,name varchar2(10),
2??? foreign key (id,name) references wwm_father on delete set null);
Table created.
?
SQL> insert into wwm_father values (6,'wwm');
1 row created.
?
SQL> insret into wwm_child values (6,'fff');
SP2-0734: unknown command beginning "insret int..." - rest of line ignored.
?
可以看出,REFERENCE是起作用的。但下面就有點讓人疑惑了,似乎ORACLE不用該用這種策略來做,
?
SQL> insert into wwm_child values (6,null);
1 row created.
?
SQL> insert into wwm_child values(null,'lll');
1 row created.
?
SQL> insert into wwm_child values (null,null);
1 row created.
?
SQL> select * from wwm_father;
?
ID NAME
---------- --------------------
6 wwm
?
SQL> select * from wwm_child;
?
ID NAME
---------- --------------------
6
lll
?
SQL> select count(*) from wwm_child;
?
COUNT(*)
----------
3
?
可見,如果向CHILD表插入NULL的話,ORACLE默認認為NULL是匹配FATHER表里相關的REFERENCE的字段內容的。因此FOREIGN KEY的COLUMN大家就需要認真考慮是否要設置成NOT NULL了
?