1
我有以下DDLDB2外鍵空不工作的兩列引用父表的同一列
CREATE TABLE ABC (
COL1 INTEGER NOT NULL,
COL2 VARCHAR(100) NOT NULL,
COL3 VARCHAR(100) NOT NULL,
COL4 INTEGER NOT NULL
);
ALTER TABLE ABC
ADD CONSTRAINT ABC_PK PRIMARY KEY
(COL1,
COL4);
CREATE TABLE XYZ (
COL1 INTEGER NOT NULL,
COL2 INTEGER,
COL3 INTEGER,
COL4 INTEGER
);
ALTER TABLE XYZ
ADD CONSTRAINT COL_2_FK FOREIGN KEY
(COL2,
COL4)
REFERENCES ABC
(COL1,
COL4)
ON DELETE SET NULL
ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
ALTER TABLE XYZ
ADD CONSTRAINT COL_3_FK FOREIGN KEY
(COL3,
COL4)
REFERENCES ABC
(COL1,
COL4)
ON DELETE SET NULL
ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION;
最後修改表失敗,以下錯誤:
FOREIGN KEY "COL_3_FK" is not valid because it would cause a descendent table "DB2ADMIN.X" to be delete-connected to its ancestor table "DB2ADMIN.A" through multiple relationships with conflicting delete rules. The conflict is between the delete rules of constraints "DB2ADMIN.XYZ.CO" and "DB2ADMIN.XYZ.COL_3_FK" on the descendent table. Reason code = "3".. SQLCODE=-20255, SQLSTATE=42915, DRIVER=3.68.61
我在IBM DB2文檔中找到: 當通過多個關係(其中這些關係具有重疊的外鍵)將表刪除連接到另一個表時,這些關係必須具有相同的刪除規則,並且這些關係都不能爲SET NULL。
但我不明白背後的理由嗎? 我該如何克服這個問題?
謝謝!但它對Oracle有用。 – raag
我不會稱之爲「工作」。 Oracle可能會讓你創建這些約束,但是你的數據違反了這些約束。 – mustaccio