0

我希望能够跟踪表中相关记录的历史记录。在 Oracle 系统上,我通过创建一个插入触发器来完成此操作,该触发器将使用原始记录的 PK(如果已知)填充字段,或者如果未指定原始 PK,则使用新 PK 填充字段。

换句话说,给定以下代码,INS_Nomination 触发器将使用传入的 parent_nomination_id 填充新的 Nomination.parent_nomination_id 字段,或者如果 parent_nomination_id 为 NULL,则使用为表的 PK (nomination_id) 生成的值:

/*create nomination tables*/
    CREATE TABLE Nomination (
        nomination_id NUMBER(12,0) NOT NULL ENABLE, 
        parent_nomination_id NUMBER(12,0) NOT NULL, 
        whatever VARCHAR2(400 BYTE), 
        created_by NUMBER(12,0) NOT NULL ENABLE, 
        created_date DATE DEFAULT SYSDATE NOT NULL ENABLE, 
        active_ind NUMBER(1,0) DEFAULT 1 NOT NULL ENABLE, 
        CONSTRAINT Nomination_PK PRIMARY KEY (nomination_id) ENABLE
    );

    CREATE INDEX Nomination_INDEX1 ON Nomination (parent_nomination_id, active_ind DESC) ;

    CREATE UNIQUE INDEX Nomination_PK ON Nomination (nomination_id);

    CREATE SEQUENCE Nomination_SEQ;

    CREATE OR REPLACE TRIGGER INS_Nomination BEFORE INSERT ON Nomination FOR EACH ROW 
    BEGIN   
        SELECT Nomination_SEQ.nextval
        INTO :new.nomination_id 
        FROM Dual;

        SELECT NVL(:NEW.parent_nomination_id, :NEW.nomination_id)
        INTO :NEW.parent_nomination_id
        FROM Dual;
    END;

    /
    ALTER TRIGGER INS_Nomination ENABLE;

如何使用 Postgres 完成相同的功能?

4

1 回答 1

2
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;

CREATE TABLE nomination 
      ( nomination_id BIGSERIAL NOT NULL PRIMARY KEY
      , parent_nomination_id BIGINT NOT NULL
         REFERENCES nomination(nomination_id)
      , whatever VARCHAR
      , created_by INTEGER NOT NULL DEFAULT 0
      , created_date DATE NOT NULL DEFAULT CURRENT_DATE
      , active_ind boolean NOT NULL DEFAULT true
    );

CREATE OR REPLACE FUNCTION nomination_fix_parent() RETURNS TRIGGER
AS
$func$
        BEGIN
         NEW.parent_nomination_id := NEW.nomination_id ;
         RETURN NEW;
        END
$func$ LANGUAGE plpgsql ; 

CREATE TRIGGER nomination_fix_parent
    BEFORE INSERT ON nomination
    FOR EACH ROW
    WHEN (NEW.parent_nomination_id IS NULL)
    EXECUTE PROCEDURE nomination_fix_parent()
        ; 

-- Test it ...
INSERT INTO nomination(whatever) VALUES('First?' );
INSERT INTO nomination(parent_nomination_id, whatever) VALUES(1,'Second?' );
INSERT INTO nomination(nomination_id, parent_nomination_id, whatever) VALUES(5,1,'Five?' );
SELECT * FROM nomination;

-- after entering the third record with id=5, the sequence is out of sequence ...
SELECT currval('nomination_nomination_id_seq');

-- Re-adjust the value for the sequence
SELECT setval('nomination_nomination_id_seq', (SELECT MAX(nomination_id) FROM nomination) );

INSERT INTO nomination(whatever) VALUES('Fourth?' );
SELECT * FROM nomination;

笔记:

  • PRIMARY KEYREFERENCES子句自动为您创建索引
  • SERIALBIGSERIAL自动创建一个序列,并默认使用其 nextval()
  • 所以上面片段中的触发器只需要为parent_nomination_id设置一个DEFAULT,它不是基于序列,而是基于同一行的nomination_id(可能是基于序列)。
  • 手动将值插入 SERIAL/bigserial 列不会影响序列;所以你必须在之后明确地重新同步(使用setval('nomination_id_seq', select max() from ...))。
于 2013-09-13T11:16:10.543 回答