使用world数据库我必须创建一个触发器,当更新总体的country
(也表)时将更新city
(表)的总体,并将旧结果拉入新表ancien_population
:
CREATE TABLE ancien_population (Name VARCHAR(100), population int(11));
DELIMITER $$
CREATE TRIGGER city_pop
AFTER UPDATE ON city
FOR EACH ROW BEGIN
INSERT INTO ancien_population (Name, population) VALUES ('City', old.population);
UPDATE ancien_population set Name = (select Name from city where Population = new.population) where population = old.population;
UPDATE country SET population = (population + (new.population - old.population)) WHERE country.code in (select CountryCode from city where city.population = new.population);
END $$
DELIMITER ;
子查询返回多行,因此它给我们一个错误。我们需要触发器来拉动城市的名称以及旧的人口。