disable/enable validate/novalidate 的區別 (ORACLE)

來源:互聯網
上載者:User
啟用約束:
enable( validate) :啟用約束,建立索引,對已有及新加入的資料執行約束.
enable novalidate :啟用約束,建立索引,僅對新加入的資料強制執行約束,而不管表中的現有資料.

禁用約束:
disable( novalidate):關閉約束,刪除索引,可以對約束列的資料進行修改等操作.
disable validate :關閉約束,刪除索引,不能對錶進行 插入/更新/刪除等操作.

環境:oracle 9i 9.0.1.0 for win,以上結論均測試通過.
例:disable validate約束後,執行update...操作提示:
ORA-25128: 不能對帶有禁用和驗證約束條件 (SYS.PK_EMP_01) 的表進行插入/更新/刪除

=============================================================


首先對constraint的開啟和關閉共有四種:enable validate;enable novalidate;disable validate;disable novalidate;
1,3沒有什麼好說的,2表示開啟時不檢查已存在資料,4表示不檢查今後的資料.
為此做如下測試:
1.首先建立測試表
create table a (no number(5),name varchar2(10));
insert into a values(1,'ss');
alter table a add constraint pk_a primary key(no);

create table b (no number(5),name varchar2(10));
insert into a values(1,'ss');
ALTER TABLE b ADD CONSTRAINT fk_a_b
FOREIGN KEY (no)
REFERENCES a (no);

2.測試fk
SQL> insert into b values(2,'sd');
insert into b values(2,'sd')
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.FK_A_B) violated - parent key not found
為此使用
SQL> alter table b disable novalidate constraint fk_a_b;

Table altered.

insert into b values(2,'sdd')
SQL> /

1 row created.
SQL> alter table b enable novalidate constraint fk_a_b;

Table altered.
從上面測試結果可見enable novalidate和disable novalidate在fk等約束是好用的.
3.測試pk
SQL> alter table a disable novalidate primary key;

Table altered.
SQL> insert into a values(1,'sd');

1 row created.

SQL> commit;

Commit complete.

SQL> alter table a enable novalidate primary key;
alter table a enable novalidate primary key
*
ERROR at line 1:
ORA-02437: cannot validate (SYSTEM.SYS_C001578) - primary key violated
在pk測試中enable novalidate不能使用了.
看到kamus的解釋說是enable novalidate主鍵必須是唯一.
查了下相關資料,沒有發現上述說明,但是找到一個下面的說法:
Primary and unique keys must use nonunique indexes
從新做測試
4.測試pk(2)
SQL> alter table a disable novalidate primary key;

Table altered.
這時我去檢查表a,發現主鍵對應的索引pk_a不在了.
手工建立索引
SQL> create index i_a on a(no);

Index created.

SQL> alter table a enable novalidate primary key;

Table altered.
成功了.

結論:
從上面的測試結果看出,novalidate在非pk;un時可以正常工作.
在對pk;un使用時需要先建立相關索引,再使用novalidate.

Validated Constraints
You have seen how to enable and disable a constraint. ENABLE and DISABLE
affect only future data that will be added/modified in the table. In contrast,
the VALIDATE and NOVALIDATE keywords in the ALTER TABLE command act
upon the existing data. Therefore, a constraint can have four states:

ENABLE VALIDATE This is the default for the ENABLE clause. The existing
data in the table is validated to verify that it conforms to the constraint.

ENABLE NOVALIDATE Does not validate the existing data, but enables
the constraint for future constraint checking.

DISABLE VALIDATE The constraint is disabled (any index used to
enforce the constraint is also dropped), but the constraint is kept valid.
No DML operation is allowed on the table because future changes cannot
be verified.

DISABLE NOVALIDATE This is the default for the DISABLE clause. The
constraint is disabled, and no checks are done on future or existing data.

eg:
ALTER TABLE WH01 MODIFY CONSTRAINT PK_WH01
DISABLE NOVALIDATE;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.