2

我想创建一个触发器,以便:

  1. 如果 AMOUNT 字段 < 100,AMOUNT 字段会自动插入 100。
  2. 如果 AMOUNT > 100,则应在 AMOUNT 字段中插入输入的值。

桌子:

CREATE TABLE DB_triggers.acc(
    C_ID INT(3) UNIQUE NOT NULL AUTO_INCREMENT ,
    customer_name CHAR(30),amount FLOAT(10,2) UNSIGNED, 
    date_created DATE,PRIMARY KEY(C_ID,customer_name)
) AUTO_INCREMENT=100;   

扳机:

DELIMITER $$ 
    CREATE TRIGGER trig_acc
    BEFORE INSERT ON acc

    FOR EACH ROW 

    BEGIN
    IF amount <100 THEN

    INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
    ELSE
    INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());

    END IF;

END $$
DELIMITER ;

但是,当我插入一个值时:

INSERT INTO 
    acc(c_id,customer_name,amount,date_created) 
VALUES
    (DEFAULT,'ABC',1000,NOW()); 

给出错误 1054

4

1 回答 1

0

将IF amount <100 THEN更改为IF new.amount <100 THEN那么它将起作用。

尝试这个:

DELIMITER $$ 
 CREATE TRIGGER trig_acc
 BEFORE INSERT ON acc
 FOR EACH ROW 

BEGIN
IF new.amount <100 THEN

INSERT INTO acc VALUES(new.c_id,new.customer_name,100,new.now());
ELSE
     INSERT INTO acc VALUES(new.c_id,new.customer_name,new.amount,new.now());

END IF;

END $$
DELIMITER ;
于 2013-01-09T13:57:16.417 回答