2014-02-14 42 views
0

我在想,我想有一個Postgres的「遞延檢查約束」,但這顯然是not supported at this time(Postgres的9.3)在Oracle中是否可以有「延期檢查約束」?

然後我看到甲骨文似乎已經廣泛「延期」的約束,記錄在here。因此,Oracle 10g +是否支持「延遲檢查約束」?

我可能錯過了相反的進一步文檔,所以我想在這裏問一個複選框,相信有人會主動使用Oracle,他們會知道答案 - 從而避免反覆試驗,浪費幾個小時與Oracle服務器有關。

回答

2

是,儘管我「M不知道你爲什麼會想:

create table t42 (id number, 
    constraint check_id check (id > 0) initially deferred deferrable); 

table T42 created. 

insert into t42 (id) values (-1); 

1 rows inserted. 

commit; 

Error report - 
SQL Error: ORA-02091: transaction rolled back 
ORA-02290: check constraint (STACKOVERFLOW.CHECK_ID) violated 
02091. 00000 - "transaction rolled back" 
*Cause: Also see error 2092. If the transaction is aborted at a remote 
      site then you will only see 2091; if aborted at host then you will 
      see 2092 and 2091. 
*Action: Add rollback segment and retry the transaction. 

當然,你可以在提交之前更新:

insert into t42 (id) values (-1); 

1 rows inserted. 

update t42 set id = 1 where id = -1; 

1 rows updated. 

commit; 

committed. 

...但我不知道你爲什麼會放無效值如果您計劃更新它,請在表格中排在第一位。據推測,有一些情況下這是有用的。

更多關於約束延期in the documentation

+0

當兩個表需要FK到另一個時,我使用延遲約束。這是一個非常嚴格的fk約束。我用過幾次...幾乎沒有。 –

+0

@BrianMcGinity - 這是使用延遲*檢查*約束,我感到困惑,而不是一般推遲。我想我只見過FK被推遲。 –

+0

哦......這很奇怪......我想不出你所概述的另一個原因。如果你在插入時不知道一個值,那麼做一些事情來找到值,然後在提交之前更新表......嗯......不能想到另一個原因...... –

1

是的,你可以定義約束爲

"DEFERRABLE" or "NOT DEFERRABLE" 

然後

"INITIALLY DEFERRED" or "INITIALLY IMMEDIATE" 

例如:

ALTER TABLE T 
ADD CONSTRAINT ck_t CHECK (COL_1 > 0) 
DEFERRABLE INITIALLY DEFERRED; 

詳情請查看Oracle文檔...