我想創建一個觸發器,以便每當我進行更改(更新或刪除)時,都應該將舊數據複製到新表(使用相同的模板)。postgresql-使用觸發器觸發
我試過這段代碼:
create table restrictions(ID int,name text);
insert into restrictions values(122,'suresh');
select * from restrictions;
create table restrictions_deleted(ID int,name text);// this is my duplicate table for keeping information of all updations.
CREATE OR REPLACE FUNCTION moveDeleted() RETURNS trigger AS $$
BEGIN
INSERT INTO restrictions_deleted VALUES(OLD.ID, OLD.name);
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER moveDeleted
BEFORE DELETE ON restrictions
FOR EACH ROW
EXECUTE PROCEDURE moveDeleted();
delete from restrictions where ID=122;
select * from restrictions_deleted;
這個代碼能夠全部被刪除的數據記錄到重複表的。但是我也想爲更新做同樣的事情。
任何建議,任何想法?
你肯定要_replicate上的更新操作的整體table_?您是否考慮過使用數據模型來記錄您需要知道的歷史信息? – 2013-03-12 09:35:07
@ClassStacker當前的觸發器不會複製整個**表。它只會應付當前行。 – 2013-03-12 10:00:02
@IgorRomanchenko絕對。我的錯。 – 2013-03-12 10:06:09