oracle 約束(constraint)的幾個引數的小研究
首先搞清下幾個概念:
ORACLE中,約束分deferred 跟 immediate 2種:
deferred:如果 Oracle 在事務提交(commit)時才對約束執行檢查,則稱此約束是延遲的(deferred)。如果資料違反了延遲約束,提交操作將導致事務被回滾(undo)。
immediate:如果約束是即時的(immediate)(非延遲的),則此約束將在語句執行結束後進行檢查。如果資料違反了延遲約束,語句將被立即回滾。
一般情況下,我們用的約束初始都是immediate型的(預設),而且不好轉為deferred型。但是如果初始是deferrable(需要手動指定),那deferred跟immediate 2種狀態可以隨意轉換。
此外,約束有以下4種狀態:
ENABLE(啟用)確保所有輸入的資料都遵從約束(constraint)
DISABLE(禁用)總是允許輸入資料,無論資料是否遵從約束
VALIDATE(驗證)確保已存在的資料遵從約束
NOVALIDATE(無驗證)允許已存在的資料不遵從約束
ENABLE VALIDATE 與 ENABLE 相同。Oracle 將檢查約束,並保證所有資料均遵從約束。
ENABLE NOVALIDATE 表示所有新插入或被修改的資料都必須遵從約束,但允許已存在的資料不遵從約束。
DISABLE NOVALIDATE 與 DISABLE 相同。Oracle 不會檢查約束.
DISABLE VALIDATE 將禁用約束,移除約束使用的索引,並禁止修改約束鍵的資料。
約束不論哪種型別,要能夠生效,必須狀態是enable才行。
SQL> drop table aa purge;
Table dropped.
SQL> create table aa ( id number,name varchar2(20),constraint pk primary key(id));
Table created.
SQL> col constraint_name for a11
SQL> select CONSTRAINT_NAME ,CONSTRAINT_TYPE,TABLE_NAME,STATUS,DEFERRABLE,DEFERRED,validated from u
ser_constraints where table_name='AA';
CONSTRAINT_ C TABLE STATUS DEFERRABLE DEFERRED VALIDATED
----------- - ----- -------- -------------- --------- -------------
PK P AA ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
--可以看到,預設的是NOT DEFERRABLE,下面我們將它轉為immediate試試
SQL> set constraint pk immediate;
set constraint pk immediate
*
ERROR at line 1:
ORA-02447: cannot defer a constraint that is not deferrable
--增加一個uk,指定deferrable initially immidiate|deferred
SQL> alter table aa add constraint uk unique (name) deferrable initially immediate;
Table altered.
SQL> select CONSTRAINT_NAME ,CONSTRAINT_TYPE,TABLE_NAME,STATUS,DEFERRABLE,DEFERRED,validated from u
ser_constraints where table_name='AA';
CONSTRAINT_ C TABLE STATUS DEFERRABLE DEFERRED VALIDATED
----------- - ----- -------- -------------- --------- -------------
PK P AA ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
UK U AA ENABLED DEFERRABLE IMMEDIATE VALIDATED
SQL> select * from aa;
no rows selected
SQL> insert into aa values(1,'SDF');
1 row created.
SQL> insert into aa values(2,'SDF');
insert into aa values(2,'SDF')
*
ERROR at line 1:
ORA-00001: unique constraint (LYN.UK) violated
SQL> set constraints uk deferred;
Constraint set.
SQL> select * from aa;
no rows selected
SQL> insert into aa values(1,'SDF');
1 row created.
SQL> insert into aa values(2,'SDF');
1 row created.
SQL> commit;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-00001: unique constraint (LYN.UK) violated
--此外,可以在SESSION裡設定所有約束是立即檢查(immediate)還是延遲檢查(deferred),當然,這隻影響初始時指定可延遲的(deferrable)約束。
SQL> alter session set constraint=immediate;
Session altered.
SQL> alter session set constraints=deferred;
Session altered.
--4種狀態對比-----------------
SQL> drop table aa purge;
Table dropped.
SQL> create table aa(id number primary key);
Table created.
--enable validate(預設)
SQL> select CONSTRAINT_NAME ,CONSTRAINT_TYPE,TABLE_NAME,STATUS,DEFERRABLE,DEFERRED,validated from
user_constraints where table_name='AA';
CONSTRAINT_ C TABLE STATUS DEFERRABLE DEFERRED VALIDATED
----------- - ----- -------- -------------- --------- -------------
SYS_C002829 P AA ENABLED NOT DEFERRABLE IMMEDIATE VALIDATED
SQL> insert into aa select 2010 from dual;
1 row created.
SQL> insert into aa select 2010 from dual;
insert into aa select 2010 from dual
*
ERROR at line 1:
ORA-00001: unique constraint (LYN.SYS_C002829) violated
--disable novalidate
SQL> alter table aa modify constraints SYS_C002829 disable;
Table altered.
SQL> select CONSTRAINT_NAME ,CONSTRAINT_TYPE,TABLE_NAME,STATUS,DEFERRABLE,DEFERRED,validated from
user_constraints where table_name='AA';
CONSTRAINT_ C TABLE STATUS DEFERRABLE DEFERRED VALIDATED
----------- - ----- -------- -------------- --------- -------------
SYS_C002829 P AA DISABLED NOT DEFERRABLE IMMEDIATE NOT VALIDATED
SQL> select * from aa;
ID
----------
2010
SQL> insert into aa select 2010 from dual;
1 row created.
SQL> insert into aa select 2010 from dual;
1 row created.
SQL> commit;
Commit complete.
--disable validate
SQL> alter table aa modify constraints SYS_C002829 disable validate;
alter table aa modify constraints SYS_C002829 disable validate
*
ERROR at line 1:
ORA-02437: cannot validate (LYN.SYS_C002829) - primary key violated
SQL> select * from aa;
ID
----------
2010
2010
2010
SQL> delete aa where rownum<3;
2 rows deleted.
SQL> select rownum,id from aa;
ROWNUM ID
---------- ----------
1 2010
SQL> alter table aa modify constraints SYS_C002829 disable validate;
Table altered.
SQL> update aa set id=2012;
update aa set id=2012
*
ERROR at line 1:
ORA-25128: No insert/update/delete on table with constraint (LYN.SYS_C002829) disabledand validated
SQL> insert into aa select 2012 from dual;
insert into aa select 2012 from dual
*
ERROR at line 1:
ORA-25128: No insert/update/delete on table with constraint (LYN.SYS_C002829) disabledand validated
SQL> delete from aa;
delete from aa
*
ERROR at line 1:
ORA-25128: No insert/update/delete on table with constraint (LYN.SYS_C002829) disabledand validated
SQL> truncate table aa;
truncate table aa
*
ERROR at line 1:
ORA-25128: No insert/update/delete on table with constraint (LYN.SYS_C002829) disabledand validated
--enable novalidate
SQL> alter table aa enable novalidate constraint SYS_C002829;
Table altered.
SQL> insert into aa select 2010 from dual;
insert into aa select 2010 from dual
*
ERROR at line 1:
ORA-00001: unique constraint (LYN.SYS_C002829) violated
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/21584437/viewspace-715985/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 10、Oracle中的約 束constraintOracleAI
- Oracle約束constraint的三個屬性應用OracleAI
- 約束CONSTRAINTAI
- constraint約束AI
- Oracle約束Constraint對於CBO優化器的作用OracleAI優化
- Oracle資料庫中Constraint約束的四對屬性Oracle資料庫AI
- Oracle SQL細節總結之constraint約束OracleSQLAI
- MySQL——約束(constraint)詳解MySqlAI
- oracle中的約束Oracle
- 聊聊Oracle Optimizer相關的幾個引數(中)Oracle
- 聊聊Oracle Optimizer相關的幾個引數(下)Oracle
- 聊聊Oracle外來鍵約束(Foreign Key)的幾個操作選項Oracle
- Oracle幾個初始化引數Oracle
- oracle的延遲約束Oracle
- oracle impdp的skip_constraint_errors選項跳過唯一約束錯誤OracleAIError
- 建約束(Constraint)時隱式建立索引(Index)及先建立索引後建立約束的區別AI索引Index
- Oracle undo保留時間的幾個相關引數Oracle
- Oracle約束Oracle
- oracle 約束Oracle
- REST 的 6 個約束REST
- 修改oracle的約束欄位Oracle
- 並行相關的幾個引數並行
- Oracle定義約束 外來鍵約束Oracle
- oracle 約束的novalidate 應用Oracle
- ConstraintLayout 約束佈局的幾個新特性筆記整理AI筆記
- 【CONSTRAINT】具有唯一性約束的列是否可以插入空值AI
- innodb的幾個記憶體引數記憶體
- EBS密碼安全的幾個引數密碼
- 幾個引數配置的計算公式公式
- oracle 約束詳解Oracle
- Oracle約束簡介Oracle
- oracle鍵約束控制Oracle
- Oracle Sequence不設定cache引數的幾個潛在問題Oracle
- Oracle主鍵約束、唯一鍵約束、唯一索引的區別(轉)Oracle索引
- oracle外來鍵約束的總結Oracle
- Oracle約束的學習與測試Oracle
- 伺服器中的幾個重要引數伺服器
- 幾個重要的 ASM Disk Groups 引數ASM