討論關於Constraint states
討論關於Constraint states:
<
最佳實踐:
Integrity Constraint States: Procedures and Benefits
Using integrity constraint states in the following order can ensure the best benefits:
1. Disable state.
2. Perform. the operation (load, export, import).
3. Enable NOVALIDATE state.
4. Enable state.
Some benefits of using constraints in this order are:
No locks are held.
All constraints can go to enable state concurrently.
Constraint enabling is done in parallel.
Concurrent activity on table is permitted.
Constraint states include:
ENABLE ensures that all incoming data conforms to the constraint
DISABLE allows incoming data, regardless of whether it conforms to the constraint
VALIDATE ensures that existing data conforms to the constraint
NOVALIDATE means that some existing data may not conform. to the constraint
In addition:
ENABLE VALIDATE is the same as ENABLE. The constraint is checked and is guaranteed to hold for all rows.
ENABLE NOVALIDATE means that the constraint is checked, but it does not have to be true for all rows. This allows existing rows to violate the constraint, while ensuring that all new or modified rows are valid.
In an ALTER TABLE statement, ENABLE NOVALIDATE resumes constraint checking on disabled constraints without first validating all data in the table.
DISABLE NOVALIDATE is the same as DISABLE. The constraint is not checked and is not necessarily true.
DISABLE VALIDATE disables the constraint, drops the index on the constraint, and disallows any modification of the constrained columns.
Transitions between these states are governed by the following rules:
l ENABLE implies VALIDATE, unless NOVALIDATE is specified.
l DISABLE implies NOVALIDATE, unless VALIDATE is specified.
l VALIDATE and NOVALIDATE do not have any default implications for the ENABLE and DISABLE states.
When a unique or primary key moves from the DISABLE state to the ENABLE state, if there is no existing index, a unique index is automatically created. Similarly, when a unique or primary key move from ENABLE to DISABLE and it is enabled with a unique index, the unique index is dropped.
When any constraint is moved from the NOVALIDATE state to the VALIDATE state, all data must be checked. (This can be very slow.) However, moving from VALIDATE to NOVALIDATE simply forgets that the data was ever checked.
Moving a single constraint from the ENABLE NOVALIDATE state to the ENABLE VALIDATE state does not block reads, writes, or other DDL statements. It can be done in parallel.
test example:
create table t1(id number primary key );
col OWNER format a20;
col CONSTRAINT_NAME format a20;
col Table_NAME format a20;
idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';
T1 SYS_C002055 ENABLED VALIDATED
insert into t1 values(1);
insert into t1 values(2);
commit;
idle> insert into t1 values(1);
insert into t1 values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (TEST.SYS_C002055) violated
alter table t1 rename constraint SYS_C002055 to t1_pk;
idle> alter table t1 rename constraint SYS_C002055 to t1_pk;
Table altered.
alter table t1 disable constraint t1_pk;
idle> alter table t1 disable constraint t1_pk;
Table altered.
idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';
T1 T1_PK DISABLED NOT VALIDATED
一旦disable, 隨之也NOVALITDATED,除非Validate is specifed.
insert into t1 values(1);
commit;
idle> ALTER TABLE t1 ENABLE CONSTRAINT t1_pk;
ALTER TABLE t1 ENABLE CONSTRAINT t1_pk
*
ERROR at line 1:
ORA-02437: cannot validate (TEST.T1_PK) - primary key violated
因為t1中已存在有重複的key.所以不能enable validate
idle> ALTER TABLE t1 ENABLE NOVALIDATE CONSTRAINT t1_pk;
ALTER TABLE t1 ENABLE NOVALIDATE CONSTRAINT t1_pk
*
ERROR at line 1:
ORA-02437: cannot validate (TEST.T1_PK) - primary key violated
idle> ALTER TABLE t1 NOVALIDATE CONSTRAINT t1_pk;
ALTER TABLE t1 NOVALIDATE CONSTRAINT t1_pk
*
ERROR at line 1:
ORA-01735: invalid ALTER TABLE option
idle> drop table t1;
Table dropped.
idle> create table t1(id number primary key disable );
Table created.
idle> select TABLE_NAME, constraint_name, status, validated from user_constraints where table_name='T1';
T1 SYS_C002056 DISABLED NOT VALIDATED
此時的狀態為 Disabled NOVALIDATED
insert into t1 values(1);
insert into t1 values(2);
commit;
idle> insert into t1 values(1);
1 row created.
再次插入
idle> insert into t1 values(1);
1 row created.
這時並沒有報錯。因為狀態為Disabled NOVALIDATED
idle> ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056;
ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056
*
ERROR at line 1:
ORA-02437: cannot validate (TEST.SYS_C002056) - primary key violated
不能變為Validate,因為existing rows viloate the primary rule.
idle> select * from t1;
1
1
idle> select rowid from t1;
AAAHCLAAGAAAAAMAAA
AAAHCLAAGAAAAAMAAB
idle> delete t1 where rowid='AAAHCLAAGAAAAAMAAA';
1 row deleted.
idle> commit;
Commit complete.
刪除重複行
idle> ALTER TABLE T1 ENABLE CONSTRAINT SYS_C002056;
Table altered.
恢復正常。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10248702/viewspace-617235/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於oracle SCN 的討論Oracle
- [技術討論]關於低耦合開發的討論
- 關於部落格評論外掛的討論
- 關於神經網路的討論神經網路
- 關於aio的設定的討論AI
- 討論:關於The REBIND utility and the FLUSH PACKAGE CACHEPackage
- 關於rails和Grails的效能討論AI
- 關於HTTP中文翻譯的討論HTTP
- 關於業務元件相關架構的討論元件架構
- 關於一個建立型模式的討論:模式
- 關於string.Empty & "" & null 的討論Null
- 關於專案經理的討論 (轉)
- 關於UI的一次討論——來自專案管理群的討論UI專案管理
- 關於分類的線性模型的討論模型
- 關於檔案寫入的原子性討論
- 關於HTTP中文翻譯的討論之二HTTP
- 關於程式設計風格的討論 (轉)程式設計
- 關於網站設計的一點點討論網站
- 《快速排序》引發關於演算法的討論排序演算法
- K君關於“IT 新人就業方向問題“討論就業
- 關於動態配置表檢查工具 (討論帖)
- 關於 Angular 應用 Module 的 forRoot 方法的討論Angular
- 關於大資料和資料庫的討論大資料資料庫
- 關於按鍵掃描程式的終極討論
- oracle 關於例項恢復的一個討論Oracle
- 關於拉幕程式的討論和原始碼 (轉)原始碼
- 關於ora_pz程式的一些討論
- 關於資料庫作業系統的討論資料庫作業系統
- 關於 Service Worker 和 Web 應用對應關係的討論Web
- [提問交流]關於onethink模型這塊的討論模型
- 關於BSS資料化轉型的幾點討論
- 一個關於月球車的筆試題--求討論筆試
- 關於 appium 獲取不到 toast 訊息的討論APPAST
- 關於如何節約資料庫連線的討論?資料庫
- 關於軟體事務記憶體(STM)的討論記憶體
- 關於“斯金納箱”及相關理論在遊戲設計中應用的討論遊戲設計
- 玩家是否需要“自由”——關於“遊戲策略自由度”的討論遊戲
- 我的理解——關於“ERP過時論”的探討(轉)