【问题标题】:help with mysql triggers (checking values before insert)帮助 mysql 触发器(插入前检查值)
【发布时间】: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


    【解决方案1】:

    警告 - 我也在学习触发器。

    对于部分:

    if max>maxmax 
    rollback? 
    

    语法会是这样的吗?:

    IF max > maxmax THEN
        DELETE the id of the new record?
    ELSE
        do nothing?
    END IF;   
    

    【讨论】:

      猜你喜欢
      • 2015-01-04
      • 2016-03-31
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多