<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
    Constraint基礎(chǔ)概念
    ?
    ??? 一直對constraint的概念比較模糊,沒有系統(tǒng)得學(xué)習(xí)過一次。這次專門來學(xué)習(xí)一下這方面的內(nèi)容。其實如果不用到REF constraint的話,這部分還是比較簡單明晰的,關(guān)鍵是要記住創(chuàng)建和修改constraint的幾個語法,這很重要。 首先來看一下《SQL Reference》中對于Constraint的說明:
    ?
    ??? constraint01
    ?
    ??? 下面說一下我的認識:
    ?
    1、Constraints的目的:
    ?
    ????? 設(shè)立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ù)確認約束應(yīng)用于數(shù)據(jù)
    ????? ENABLE VALIDATE:將驗證已經(jīng)存在的和之后的操作是否符合約束(默認)
    ????? ENABLE NOVALIDATE:不驗證已經(jīng)存在的數(shù)據(jù),但對之后進行的操作有效
    ?
    ????? ⑤ Disable
    ????? 該參數(shù)使約束失效
    ????? DISABLE VALIDATE:約束失效標注,可用于暫時導(dǎo)入大量數(shù)據(jù)時,不進行索引更新
    ????? DISABLE NOVALIDATE:約束失效,并不保證約束是否正確,即不保證已有數(shù)據(jù)滿足約束(默認)
    ?
    ????? ⑥ Rely
    ????? Rely和Norely只能用在 ALTER TABLE MODIFY constraint 語句中
    ????? Rely:告訴Oracle,不必對NOVALIDATE模式的約束的數(shù)據(jù)進行信任,即需要檢驗以前的數(shù)據(jù)
    ????? (這個沒用過,實在搞不準確切含義,還是把文檔的內(nèi)容直接放上來)
    ?
    ????? constraint02
    ?
    4、set語句
    ?
    ????? constraint03
    ?
    ?
    ?
    ?
    ----------------------------------------------------------------------------------------------------
    轉(zhuǎn)一篇Constraint的文章
    ----------------------------------------------------------------------------------------------------
    ?
    http://sunmoonking.spaces.live.com/blog/cns!E3BD9CBED01777CA!278.entry
    ?
    constraints 三個需要注意的地方
    ?
    1. deferrable
    ?
    一個constraint如果被定義成deferrable那么這個constraints可以在deferred和imediate兩種狀態(tài)相互轉(zhuǎn)換。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)相互轉(zhuǎn)換
    ?
    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)相互轉(zhuǎn)換
    ?
    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的計劃,并且正常情況下沒有應(yīng)用的必要,所以建議不要修改,而用系統(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,當(dāng)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表里相關(guān)的REFERENCE的字段內(nèi)容的。因此FOREIGN KEY的COLUMN大家就需要認真考慮是否要設(shè)置成NOT NULL了
    ?
    posted on 2008-12-22 22:45 decode360 閱讀(358) 評論(0)  編輯  收藏 所屬分類: 08.DBA
    主站蜘蛛池模板: 亚洲午夜电影在线观看| 亚洲精品成人区在线观看| 亚洲av无码一区二区三区网站| 亚洲国产精品嫩草影院| 久久久久国色AV免费看图片| 亚洲精品视频在线免费| 4455永久在线观免费看| 亚洲av无码专区在线| 国产成人精品久久免费动漫| 亚洲国产成人精品无码区在线秒播 | 大地资源在线资源免费观看| 中文字幕亚洲激情| a级日本高清免费看| 亚洲成av人片天堂网| 久久免费的精品国产V∧| 亚洲精品不卡视频| 人妻视频一区二区三区免费| 亚洲欧美第一成人网站7777| 四虎永久免费观看| 你懂的免费在线观看| 久久精品蜜芽亚洲国产AV| 免费在线看v网址| WWW国产亚洲精品久久麻豆| 亚洲成人影院在线观看| 91视频免费观看| 亚洲国产精品成人精品小说| 在线播放免费播放av片| 羞羞视频免费观看| 亚洲国产精品无码专区| 69堂人成无码免费视频果冻传媒| 亚洲欧美日韩自偷自拍| 伊人亚洲综合青草青草久热| 无码国产精品一区二区免费式芒果 | yellow免费网站| 亚洲黄色在线视频| 午夜免费福利在线| 精品国产免费人成网站| 亚洲国产综合第一精品小说| 宅男666在线永久免费观看| 大地资源在线资源免费观看| 亚洲午夜成人精品无码色欲|