我必须在postgresql中创建一个触发器(没有任何函数调用)。我有两张桌子LivesIn(pid,name,province)和Places(姓名,省,人口,mayorid)
我有以下代码:
CREATE TRIGGER updatePopulation
AFTER INSERT ON LivesIn
FOR EACH ROW
UPDATE Places
SET NEW.population = OLD.Population + 1
WHERE LivesIn.name = Places.name AND LiveIn.province = Places.province;
我收到以下错误: psql:/home/2008/uehtes/Desktop/Comp421/comp421_a2_q1.sql:111:错误:“UPDATE”或其附近的语法错误 第5行:更新Places.population ^
任何帮助都会非常感激。
答案 0 :(得分:1)
@Glenn,显然这不是办法。您必须创建一个函数并在触发器中调用它。像这样:
CREATE FUNCTION updatePop() RETURNS TRIGGER AS $updatePopulation$
DECLARE
name1 varchar(30);
name2 varchar(30);
BEGIN
IF (TG_OP = 'INSERT') THEN
name1 = NEW.name;
name2 = NEW.province;
UPDATE Place
SET population = population + 1
WHERE name1 = Place.name AND name2 = Place.province;
END IF;
RETURN NULL;
END;
$updatePopulation$ LANGUAGE plpgsql;
CREATE TRIGGER updatePopulation
AFTER INSERT ON LivesIn
FOR EACH ROW
EXECUTE PROCEDURE updatePop();