需要帮助编写SQL触发器

时间:2012-04-10 21:41:17

标签: sql postgresql triggers

试图写下这个触发器,但我无法解决这个问题..

我需要做什么:

从项目表中删除项目,但同时删除与该项目相关的任何问题以及与该问题相关的任何问题更新。然后,这些删除的记录需要存储在存档表中,删除时间以及删除它们的运算符的ID。

问题可能会有多个与之相关的更新,因为某个项目可能有很多与之相关的问题。

我已将所有架构放在SQL小提琴中,因为它在那里工作起来要容易得多,但如果需要,我会把它放在这里。

SQL小提琴的链接:

http://sqlfiddle.com/#!1/1bb25

编辑:我想我也可以把它放在这里..

Tables:


CREATE TABLE Operator
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(40) NOT NULL
);

CREATE TABLE Item
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(40) NOT NULL
);

CREATE TABLE Faq
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Question    VARCHAR(150) NOT NULL,
    Answer  VARCHAR(2500) NOT NULL,
    ItemID  INTEGER,
    FOREIGN KEY (ItemID) REFERENCES Item(ID)
);

CREATE TABLE Customer
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(20) NOT NULL,
    Email       VARCHAR(20) NOT NULL
);

CREATE TABLE Question
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Problem VARCHAR(1000),
    AskedTime   TIMESTAMP NOT NULL,
    CustomerID  INTEGER NOT NULL,
    ItemID  INTEGER NOT NULL,
    FOREIGN KEY (ItemID) REFERENCES Item(ID),
    FOREIGN KEY (CustomerID) REFERENCES Customer(ID)
);

CREATE TABLE qUpdate
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Message VARCHAR(1000) NOT NULL,
    UpdateTime  TIMESTAMP NOT NULL,
    QuestionID  INTEGER NOT NULL,
    OperatorID INTEGER,
    FOREIGN KEY (OperatorID) REFERENCES Operator(ID),
    FOREIGN KEY (QuestionID) REFERENCES Question(ID)
);


-- Archive Tables

CREATE TABLE DeletedQuestion
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Problem VARCHAR(1000),
    AskedTime   TIMESTAMP NOT NULL,
    CustomerID  INTEGER NOT NULL,
    ItemID  INTEGER NOT NULL
    );

CREATE TABLE DeletedqUpdate
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Message VARCHAR(1000) NOT NULL,
    UpdateTime  TIMESTAMP NOT NULL,
    Question    INTEGER NOT NULL
);

    CREATE TABLE DeletedItem
(
    ID      INTEGER NOT NULL PRIMARY KEY,
    Name        VARCHAR(40) NOT NULL,
    OperatorDeleteID INTEGER NOT NULL,
    FOREIGN KEY (OperatorDeleteID) REFERENCES Operator(ID)
);

一些样品插入物用于测试

--Product Inserts
INSERT INTO Item (ID, Name) VALUES (1, 'testitem1');
INSERT INTO Item (ID, Name) VALUES (2, 'testitem2');

--Operator Inserts
INSERT INTO Operator (ID, Name) VALUES (1, 'testname1');
INSERT INTO Operator (ID, Name) VALUES (2, 'testname2');

--Faq Inserts

INSERT INTO Faq (ID, Question, Answer, ItemID) VALUES (1, 'testq1', 'testa1', 1);
INSERT INTO Faq (ID, Question, Answer, ItemID) VALUES (2, 'testq2', 'testa2', 2);

-- Customer Inserts

INSERT INTO Customer (ID, Name, Email) VALUES (1, 'testcust1', 'testemail1');
INSERT INTO Customer (ID, Name, Email) VALUES (2, 'testcust2', 'testemail2');

-- Question Inserts

INSERT INTO Question (ID, Problem, AskedTime, CustomerID, ItemID) VALUES (1,'testproblem1','2012-03-14 09:30',1,1);

INSERT INTO Question (ID, Problem, AskedTime, CustomerID, ItemID) VALUES (2,'testproblem1','2012-07-14 09:30',2,1);

INSERT INTO qUpdate (ID, Message, UpdateTime, OperatorID, QuestionID) VALUES (1, 'test1','2012-05-14 09:30', 1, 1);

INSERT INTO qUpdate (ID, Message, UpdateTime, OperatorID, QuestionID) VALUES (2, 'test2','2012-08-14 09:30', 2, 1);

1 个答案:

答案 0 :(得分:1)

要做的第一件事是要理解在PostgreSQL中,CREATE TRIGGER语句将触发器 function 绑定到表上的一个或多个操作,所以让我们从语法开始功能。您可以使用各种脚本语言编写触发器函数,但最常见的是plpgsql。一个简单的函数可能如下所示:

CREATE OR REPLACE FUNCTION Question_delete_trig_func()
  RETURNS TRIGGER
  LANGUAGE plpgsql
AS $$
BEGIN
  INSERT INTO DeletedQuestion
    SELECT OLD.*;
  RETURN OLD;
END;
$$;

删除后运行:

CREATE TRIGGER Question_delete_trig
  AFTER DELETE ON Question
  FOR EACH ROW EXECUTE PROCEDURE Question_delete_trig_func();

这应该足以让你开始。

对于应保存已删除行的每个表,您应该有这样的触发器。然后,您需要确定如何进行删除操作。您可以将适当的外键定义为ON DELETE CASCADE,让PostgreSQL为您完成。