Decode360's Blog

          業(yè)精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            302 隨筆 :: 26 文章 :: 82 評論 :: 0 Trackbacks
          ??? 首先來看一下《SQL Reference》中對于Constraint的說明:
          ?
          ??? constraint-1.jpg
          ?
          ??? 下面說一下我的認識:
          ?
          ??? 1、Constraints的目的:
          ?
          ????? 設立Constraint就是為了讓數(shù)據(jù)滿足某些規(guī)則。

          ??? 2、Constraint的類型:
          ?
          ????? not null??? (不能為空)
          ??????unique????? (值必須唯一)
          ??????primary?key (not?null + unique)
          ????? goreign?key?(該表值必須在外鍵表中存在)
          ??????check????? ?(自己加的條件)
          ??????ref???????? (不熟)

          ??? 注:Constraints不但可以建立在Table上,也可以建立在View上。

          ??? 3、Constraint的狀態(tài):
          ?
          ????? ① Deferrable
          ????? 該參數(shù)用于指定是否可以是同set語句來進行臨時控制constraint,時約束在commit時才生效
          ????? DEFERRABLE:可以使用set constraint字句
          ????? NOT DEFERRABLE:不可以使用set constraint字句(默認)
          ?
          ????? ② Initially
          ????? 該參數(shù)用于建立默認的DEFERRABLE類型約束
          ????? INITIALLY一般都要和IMMEDIATE、DEFERRED一起使用
          ????? INITIALLY IMMEDIATE:在執(zhí)行SQL時違反約束即報錯(默認)
          ????? INITIALLY DEFERRED:在提交時才報錯
          ?
          ????? ③ Validate?| NoValidate
          ????? 該參數(shù)一般與Enabled和Disabled屬性搭配使用
          ?
          ????? ④ Enable
          ????? 該參數(shù)確認約束應用于數(shù)據(jù)
          ????? ENABLE VALIDATE:將驗證已經(jīng)存在的和之后的操作是否符合約束(默認)
          ????? ENABLE NOVALIDATE:不驗證已經(jīng)存在的數(shù)據(jù),但對之后進行的操作有效
          ?
          ????? ⑤ Disable
          ????? 該參數(shù)使約束失效
          ????? DISABLE VALIDATE:約束失效標注,可用于暫時導入大量數(shù)據(jù)時,不進行索引更新
          ????? DISABLE NOVALIDATE:約束失效,并不保證約束是否正確,即不保證已有數(shù)據(jù)滿足約束(默認)
          ?
          ????? ⑥ Rely
          ????? Rely和Norely只能用在 ALTER TABLE MODIFY constraint 語句中
          ????? Rely:告訴Oracle,不必對NOVALIDATE模式的約束的數(shù)據(jù)進行信任,即需要檢驗以前的數(shù)據(jù)
          ????? (這個沒用過,實在搞不準確切含義,還是把文檔的內容直接放上來)
          ????? constraint-2.jpg
          ?
          ??? 4、set語句
          ?
          ????? constraint-3.jpg
          ?
          ?
          ?
          ?
          ----------------------------------------------------------------------------------------------------
          轉一篇Constraint的文章
          ----------------------------------------------------------------------------------------------------
          ?
          http://sunmoonking.spaces.live.com/blog/cns!E3BD9CBED01777CA!278.entry
          ?
          constraints 三個需要注意的地方
          ?
          1. deferrable
          ?
          一個constraint如果被定義成deferrable那么這個constraints可以在deferred和imediate兩種狀態(tài)相互轉換。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兩種狀態(tài)相互轉換
          ?
          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兩種狀態(tài)相互轉換
          ?
          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的計劃,并且正常情況下沒有應用的必要,所以建議不要修改,而用系統(tǒng)默認的non deferrable
          ?

          2. enable/disable validate/novalidate
          ?
          enable/disable對未來的數(shù)據(jù)有約束/無約束。
          ?
          validate/novalidate對已有的數(shù)據(jù)有約束/無約束。
          ?
          如果加約束到一個大表,那么ORACLE會LOCK這個表,然后SCAN所有數(shù)據(jù),來判斷是否符合CONSTRAINT的要求,在繁忙的系統(tǒng)里顯然是不合適的。所以用enable novalidate比較合適,因為ORACLE僅僅會LOCK表一小段時間來建立CONSTRAINT,當CONSTRAINT建立后再VALIDATE,這時檢驗數(shù)據(jù)是不會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了
          ?




          -The End-

          posted on 2008-12-22 22:45 decode360-3 閱讀(419) 評論(0)  編輯  收藏 所屬分類: Oracle
          主站蜘蛛池模板: 南昌市| 禹州市| 文山县| 兴安县| 定兴县| 彭泽县| 桂林市| 恩施市| 厦门市| 柯坪县| 梁平县| 齐齐哈尔市| 宜川县| 庆城县| 景德镇市| 郎溪县| 威宁| 新泰市| 平罗县| 台前县| 淮阳县| 新河县| 繁昌县| 华蓥市| 尼勒克县| 罗平县| 陵水| 图片| 司法| 武邑县| 曲阳县| 白河县| 弥勒县| 富锦市| 静乐县| 西畴县| 邵阳市| 崇明县| 娱乐| 上思县| 阿尔山市|