1
嗨我很新的MySQL,我想弄清楚如何使用觸發器。幫助與MySQL觸發器(檢查插入前的值)
我在做什麼: 當我向sub_max插入新行時,我有2個表max和sub_max我想檢查與新行相同foreign_key的值的SUM是否少於比最大表中的值。我覺得這聽起來有點混亂所以這裏是我的表:
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
我想知道我是否正確地遠程進行此操作。提前致謝。