用序列生成的pk更新父子表块

时间:2018-10-18 17:36:37

标签: oracle plsql oracle11g database-trigger database-sequence

CREATE TABLE t1 (
     t1pk NUMBER PRIMARY KEY NOT NULL
    ,t1val NUMBER
);
CREATE TABLE t2 (
     t2pk NUMBER PRIMARY KEY NOT NULL
    ,t2fk NUMBER
    ,t2val NUMBER
    ,CONSTRAINT t2fk FOREIGN KEY (t2fk)
        REFERENCES t1 (t1pk) ON DELETE CASCADE
);

INSERT INTO t1 (t1pk, t1val)
VALUES (1, 1);

INSERT INTO t2 (t2pk, t2fk, t2val)
VALUES (1, 1, 1);

COMMIT;

CREATE SEQUENCE seq1
    MINVALUE 1
    MAXVALUE 999999999999999999999999999;

CREATE OR REPLACE TRIGGER trg1
BEFORE
INSERT -- Problematic code.
OR UPDATE
OR DELETE ON t1
FOR EACH ROW
BEGIN
    IF (INSERTING)
    THEN
        -- Problematic code.
        :NEW.t1pk := seq1.NEXTVAL;
    END IF;
END trg1;
/

会议1:

UPDATE t2 -- Table 2!
SET t2val = t2val;

会议2:

UPDATE t1 -- Table 1!
SET t1val = t1val;

在会话2中,更新不会返回,而是等待直到会话1通过提交或回滚关闭事务。这不是我所期望的。原因似乎是从序列生成pk的触发代码。如果删除该序列代码,则会话2中的更新不会等待,而在会话1的事务仍处于打开状态时又会回来。怎么了?

Oracle Database 11g企业版11.2.0.3.0版-64位生产

0 个答案:

没有答案