简体   繁体   English

在SQL中插入触发器后如何使用

[英]how to use after insertion triggers in sql

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| _date  | varchar(20) | NO   | PRI | NULL    |       |
| petrol | int(11)     | NO   |     | NULL    |       |
| diesel | int(11)     | NO   |     | NULL    |       |
| gas    | int(11)     | NO   |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| _date1  | varchar(20) | NO   | PRI | NULL    |       |
| petrol1 | int(11)     | NO   |     | NULL    |       |
| diesel1 | int(11)     | NO   |     | NULL    |       |
| gas1    | int(11)     | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

I am new at triggers and SQL overall and wanted to use after insert trigger such that once i enter values in the table "buy" mentioned above. 我是触发器和SQL方面的新手,并且想在插入触发器后使用,以便一旦在上述表“购买”中输入值后就可以使用。 The same value inserted must be inserted into the "Sell" table(also mentioned above) but after a mathematical operation : 必须在数学运算之后,将插入的相同值插入“ Sell”表(也已在上文中提及):

petrol+petrol*0.3, diesel+diesel*0.15, gas+gas*0.25

I tried to use this: 我试图用这个:

CREATE TRIGGER t1
AFTER INSERT
ON buy
FOR EACH ROW
BEGIN
(
    INSERT INTO Sell (_date1, petrol1, diesel1, gas1)
    SELECT _date, petrol+petrol*0.3, diesel+diesel*0.15, gas+gas*0.25
    FROM inserted
)
END

But then, it showed this error below: 但随后,它在下面显示了此错误:

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'after insert on buy for each row begin (insert into Sell (_date1, petrol1, diese' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“在购买时插入每一行之后开始(在第一行中插入Sell(_date1,petrol1,diese)”之后)附近使用

Your Referenced MySQL 8 Document page 您引用的MySQL 8文档页面

Read down you will find the following two statements: 向下阅读,您会发现以下两个语句:

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. 在触发器主体内,使用OLD和NEW关键字可以访问受触发器影响的行中的列。 OLD and NEW are MySQL extensions to triggers; OLD和NEW是MySQL对触发器的扩展; they are not case-sensitive. 它们不区分大小写。

In an INSERT trigger, only NEW.col_name can be used; 在INSERT触发器中,只能使用NEW.col_name。 there is no old row. 没有旧的行。 In a DELETE trigger, only OLD.col_name can be used; 在DELETE触发器中,只能使用OLD.col_name。 there is no new row. 没有新行。 In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated. 在UPDATE触发器中,可以使用OLD.col_name引用更新前的行的列,并使用NEW.col_name引用更新后的行的列。

So with that I entirely agree with @Gordon Linoff 因此,我完全同意@Gordon Linoff

What you're looking for is: 您正在寻找的是:

DELIMITER $$
CREATE TRIGGER t1 AFTER INSERT ON buy FOR EACH ROW
BEGIN
    INSERT INTO Sell (_date1, petrol1, diesel1, gas1)
    VALUES (NEW._date, NEW.petrol * 1.3, NEW.diesel * 1.15, NEW.gas * 1.25);
END $$
DELIMITER ;

You seem to want: 您似乎想要:

delimiter $$
create trigger t1 after insert on buy for each row
begin
    insert into Sell (_date1, petrol1, diesel1, gas1)
        values (new._date, new.petrol * 1.3, new.diesel * 1.15, new.gas * 1.25)
end;$$

delimiter ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM