- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用触发器时如何从表中获取新/旧值?我尝试将以前的人口放入另一个表中,但出现语法错误。我已经编辑了表格并放置了城市/国家架构,以便其他人可以查看并尝试帮助我。
DELIMITER $$
USE `world`$$
DROP TRIGGER /*!50032 IF EXISTS */ `previous_pop`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `previous_pop` AFTER UPDATE ON city
FOR country
BEGIN
previous.city_previous_pop = country.Old.Population;
END;
$$
DELIMITER ;
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'country
Begin
国家架构;
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME COLUMN_TYPE COLUMN_KEY EXTRA PRIVILEGES COLUMN_COMMENT
def sakila country country_id 1 \N NO smallint \N \N 5 0 \N \N \N smallint(5) unsigned PRI auto_increment select,insert,update,references
def sakila country country 2 \N NO varchar 50 150 \N \N \N utf8 utf8_general_ci varchar(50) select,insert,update,references
def sakila country last_update 3 CURRENT_TIMESTAMP NO timestamp \N \N \N \N 0 \N \N timestamp on update CURRENT_TIMESTAMP select,insert,update,references
def world country Code 1 NO char 3 3 \N \N \N latin1 latin1_swedish_ci char(3) PRI select,insert,update,references
def world country Name 2 NO char 52 52 \N \N \N latin1 latin1_swedish_ci char(52) select,insert,update,references
def world country Continent 3 Asia NO enum 13 13 \N \N \N latin1 latin1_swedish_ci enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') select,insert,update,references
def world country Region 4 NO char 26 26 \N \N \N latin1 latin1_swedish_ci char(26) select,insert,update,references
def world country SurfaceArea 5 0.00 NO float \N \N 10 2 \N \N \N float(10,2) select,insert,update,references
def world country IndepYear 6 \N YES smallint \N \N 5 0 \N \N \N smallint(6) select,insert,update,references
def world country Population 7 0 NO int \N \N 10 0 \N \N \N int(11) select,insert,update,references
def world country LifeExpectancy 8 \N YES float \N \N 3 1 \N \N \N float(3,1) select,insert,update,references
def world country GNP 9 \N YES float \N \N 10 2 \N \N \N float(10,2) select,insert,update,references
def world country GNPOld 10 \N YES float \N \N 10 2 \N \N \N float(10,2) select,insert,update,references
def world country LocalName 11 NO char 45 45 \N \N \N latin1 latin1_swedish_ci char(45) select,insert,update,references
def world country GovernmentForm 12 NO char 45 45 \N \N \N latin1 latin1_swedish_ci char(45) select,insert,update,references
def world country HeadOfState 13 \N YES char 60 60 \N \N \N latin1 latin1_swedish_ci char(60) select,insert,update,references
def world country Capital 14 \N YES int \N \N 10 0 \N \N \N int(11) select,insert,update,references
def world country Code2 15 NO char 2 2 \N \N \N latin1 latin1_swedish_ci char(2) select,insert,update,references
城市架构:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME COLUMN_TYPE COLUMN_KEY EXTRA PRIVILEGES COLUMN_COMMENT
def sakila city city_id 1 \N NO smallint \N \N 5 0 \N \N \N smallint(5) unsigned PRI auto_increment select,insert,update,references
def sakila city city 2 \N NO varchar 50 150 \N \N \N utf8 utf8_general_ci varchar(50) select,insert,update,references
def sakila city country_id 3 \N NO smallint \N \N 5 0 \N \N \N smallint(5) unsigned MUL select,insert,update,references
def sakila city last_update 4 CURRENT_TIMESTAMP NO timestamp \N \N \N \N 0 \N \N timestamp on update CURRENT_TIMESTAMP select,insert,update,references
def world city ID 1 \N NO int \N \N 10 0 \N \N \N int(11) PRI auto_increment select,insert,update,references
def world city Name 2 NO char 35 35 \N \N \N latin1 latin1_swedish_ci char(35) select,insert,update,references
def world city CountryCode 3 NO char 3 3 \N \N \N latin1 latin1_swedish_ci char(3) select,insert,update,references
def world city District 4 NO char 20 20 \N \N \N latin1 latin1_swedish_ci char(20) select,insert,update,references
def world city Population 5 0 NO int \N \N 10 0 \N \N \N int(11) select,insert,update,references
创建触发器后,我运行了以下命令:
UPDATE city
SET Population = 1
WHERE CountryCode = 'AFG';
SELECT Population, CountryCode FROM city;
Population CountryCode
1 AFG
1 AFG
1 AFG
1 AFG
国家人口没有变化:
SELECT Population FROM country WHERE CODE = 'AFG';
Population
20387904
最佳答案
当城市人口有更新时,这是更新国家/地区的触发器。假设首尔市人口从1000万更新为1100万;因此该国 KOR 人口也应增加 100 万。触发器上的问题是“FOR Country”的语法,它应该是“FOR EACH ROW”。也就是说,对于城市表中更新的每一行,对国家/地区表进行更新。老城区人口与新城区人口的差额将计入乡村人口。如果城市减少人口,那么乡村人口也会减少。
CREATE TRIGGER `previous_pop` AFTER UPDATE ON city
FOR EACH ROW
BEGIN
UPDATE country
SET Population = Population + (NEW.Population - OLD.Population)
WHERE Code = OLD.CountryCode;
END;
关于MYSQL 触发器新旧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49682464/
在什么情况下我们应该在 JDK 8 中使用旧的 foreach 循环遍历新的 collection.forEach() 还是最好的做法是转换 every foreach 循环?是否存在任何重要的性能差
我想使用新的 CoraData 模型创建应用程序的新版本(由其他人创建)。我的疑问是:核心数据模型通过更新是永久的。我想确保以前的 CoreData 模型不会对新版本的应用程序产生问题。 我必须遵循一
我正在阅读“Dennis Ritchie 的 C 编程语言”,并且在一开始它演示了一个代码,即以下 #include main() { printf("Hello, World"); } 根据本书,这
首先,我知道这个问题在过去几年中在 Stack Overflow 上被提出过几次。问题是有时我会收到新的 PayPal 结帐页面,有时会收到旧页面。 我在沙盒中创建了两个测试帐户。第一个采用新设计,第
我在同一 Google Analytics(分析)帐户中有多个配置文件,但我发现新创建的配置文件的跟踪代码不同。它们会影响跟踪功能吗? 旧代码: var _gaq = _gaq || [];
我是一名优秀的程序员,十分优秀!