Oracle生产版本19.17.0.0.0
我有一个表,其中包含一个实体化视图的外键,该表是从父表填充的。
在父表上删除并重新插入相同的记录(这不会在实体化视图中造成任何差异)会导致外键中断。
CREATE TABLE TEMP_20230509_PARENT
(
ID INT PRIMARY KEY
);
INSERT INTO TEMP_20230509_PARENT (ID)
VALUES (1);
CREATE MATERIALIZED VIEW LOG ON TEMP_20230509_PARENT WITH ROWID, PRIMARY KEY;
-- MV pointing to the Parent
CREATE MATERIALIZED VIEW TEMP_20230509_PARENT_MV AS
SELECT *
FROM TEMP_20230509_PARENT;
-- child table that points to the MV
CREATE TABLE TEMP_20230509_CHILD
(
ID INT PRIMARY KEY,
PARENT_ID INT,
CONSTRAINT TO_PARENT_FK
FOREIGN KEY (PARENT_ID)
REFERENCES TEMP_20230509_PARENT_MV (ID)
);
-- child record pointing to the MV
INSERT INTO TEMP_20230509_CHILD
VALUES (1, 1);
-- insert new record in parent, should not affect the child
INSERT INTO TEMP_20230509_PARENT (ID)
VALUES (2);
-- 🟩 full refresh works
BEGIN
DBMS_MVIEW.REFRESH('TEMP_20230509_PARENT_MV');
END;
-- delete and reinsert the same record on the parent table
DELETE
FROM TEMP_20230509_PARENT A
WHERE A.ID = 1;
INSERT INTO TEMP_20230509_PARENT (ID)
VALUES (1);
COMMIT;
-- 🟥 full refresh fails
-- ORA-02292: integrity constraint (IMPACT_NG.TO_PARENT_FK) violated - child record found
BEGIN
DBMS_MVIEW.REFRESH('TEMP_20230509_PARENT_MV');
END;
1条答案
按热度按时间blmhpbnm1#
对我来说,看起来好像MV刷新首先 * 删除 * 行(这就是引发错误的原因,因为它确实删除了父键),然后 * 插入 * 它们。
这里有一个解决方案,看看是否有帮助:创建外键为
INITIALLY DEFERRED DEFERRABLE
。父表:
MV日志:
MV本身:
子表-注意第8行:
新建父行:
刷新:
删除/插入到父项中:
这是以前失败的,但现在有效:
就是这样