【发布时间】:2011-02-18 00:16:36
【问题描述】:
嗨,我对 mysql 很陌生,我正在尝试弄清楚如何使用触发器。
我正在尝试做的事情: 我有 2 个表,max 和 sub_max,当我向 sub_max 插入新行时,我想检查与新行具有相同外键的值的 SUM 是否小于 max 表中的值。我认为这听起来令人困惑,所以这是我的表格:
CREATE TABLE max(
number INT ,
MaxAmount integer NOT NULL)
CREATE TABLE sub_max(
sub_number INT ,
sub_MaxAmount integer NOT NULL,
number INT,
FOREIGN KEY ( number ) REFERENCES max( number ))
这是我的触发器代码,我知道语法已关闭,但这是我通过查找教程所能做的最好的事情。
CREATE TRIGGER maxallowed
after insert on sub_max
FOR EACH ROW
BEGIN
DECLARE submax integer;
DECLARE maxmax integer;
submax = select sum(sub_MaxAmount) from sub_max where sub_number = new.sub_number;
submax = submax + new. sub_MaxAmount;
maxmax = select MaxAmount from max where number = new.number ;
if max>maxmax
rollback?
END
我想知道我是否正确地远程执行此操作。提前致谢。
【问题讨论】:
标签: mysql sql triggers phpmyadmin