简体   繁体   English

Mysql触发器不起作用

[英]Mysql triggers dont work

I have been searching all around about TRIGGERS since I am in dire needs for some of them, but it doesn't matter how much I try Mysql workbench 6.3 CE won't allow me to create it. 我一直在寻找有关TRIGGERS的东西,因为我对其中一些人的需求很大,但是我尝试了多少Mysql workbench 6.3 CE无法让我创建它。 It seems like my server is 5.7 and I have read the manual for references but I still can't make them to work. 看起来我的服务器是5.7并且我已经阅读了参考手册,但我仍然无法使它们工作。

DELIMITER $$
CREATE TRIGGER updateproductostock
BEFORE INSERT ON producto
FOR EACH ROW
BEGIN
NEW.prod_points = prod_price / 2;
END$$
DELIMITER ;

I have a table named producto which has id, description, price, stock and points. 我有一个名为producto的表,其中包含id,description,price,stock和points。 What I want to do is to have the points value to be the half of the price value. 我想要做的是让积分值为价格值的一半。 So if the price of a product is $100 then the points value will be 50. 因此,如果产品的价格是100美元,则积分值将为50。

I cant create the trigger, mysql gives me syntax error. 我无法创建触发器,mysql给我语法错误。

I also would like to make a trigger in which the stock value of producto depends on the table order . 我还想制作一个触发器,其中producto的股票价值取决于表格顺序 Order has an order id, product id, description and quantity. 订单包含订单ID,产品ID,描述和数量。 So when I insert a new order I want to have the value of stock in producto to change. 因此,当我插入新订单时,我希望在producto中更改库存的值。 If an order is made with the same id of the product then I want the quantity of the order to be added to the stock of the product . 如果订单的产品ID与产品相同,那么我希望将订单数量添加到产品库存中。

Again, mysql gives me syntax error. 同样,mysql给了我语法错误。

I have searched all over the place and tried different kinds of syntax but I get error. 我搜遍了整个地方,尝试了不同的语法,但是我得到了错误。 My mysql does have the option for trigger. 我的mysql确实有触发器的选项。

Or perhaps is there an easier way to do what i want to do? 或者是否有更简单的方法来做我想做的事情? Please help 请帮忙

For the sql you've shown, I would guess prod_price / 2 should be NEW.prod_price / 2 . 对于你展示的sql,我猜prod_price / 2应该是NEW.prod_price / 2

For the latter issue, the trigger needs to be on the order table; 对于后一个问题,触发器需要在订单表上; but it is generally frowned on maintaining such values when they should be easily calculable unless there is a pressing performance reason to do so. 但是,除非出现紧迫的性能原因,否则在容易计算它们时,通常不赞成保留这些值。 My guess with your problems with that would be that your order table is named with a keyword and you are forgetting to delimit it properly with `. 我对你的问题的猜测是你的订单表是用关键字命名的,你忘了用`来正确地划分它。

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

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