使用SQLFiddle,PostgreSQL 9.3.1。Postgres觸發器語法
我學習PostgreSQL中定義的觸發器,並做了一些研究之後,我發現了以下工作:
觸發的Postgres是從MySQL不同。在Postgres中你必須創建一個函數RETURNS TRIGGER
,在MySQL中你可以創建一個觸發器。所以這就是我想出的:
對於員工插入,我們要更新部門總工資。
CREATE FUNCTION update_sal() RETURNS TRIGGER AS $$
BEGIN
IF NEW.dno IS NOT NULL THEN
UPDATE Department SET Total_sal = total_sal + NEW.salary
WHERE department.dno = NEW.dno;
END IF;
RETURN NULL;
END;
$$ Language plpgsql;
CREATE TRIGGER updateInsert
AFTER INSERT ON Employee
FOR EACH ROW
EXECUTE PROCEDURE update_sal();
而且我發現了以下錯誤:
Schema Creation Failed: ERROR: unterminated dollar-quoted string at or near "$$
BEGIN IF NEW.dno IS NOT NULL THEN UPDATE Department
SET Total_sal = total_sal +NEW.salary WHERE department.dno = NEW.dno":
[相關問題更多的細節和演示](http://stackoverflow.com/questions/22747225/exceptions-when-creating-a-trigger-in-postgresql-9-1/22748778#22748778)。 –