创建触发器更新sql表

fnatzsnv  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(274)

我想创建一个触发器来更新表 khohang 和table Dothan .
以下是我的sql:

CREATE TRIGGER trigger_update on dathang
AFTER UPDATE AS
BEGIN
 UPDATE khohang INNER JOIN dathang on khohang.mahang =dathang.mahang
    SET khohang.soluong = khohang.soluong - (SELECT soluong FROM dathang where khohang.mahang = dathang.mahang)
END
vltsax25

vltsax251#

在表上执行(插入、更新、删除之前/之后)操作时,会发生触发器,因此会对该操作触发触发器,并在相同或不同的表上执行某些操作。这是触发器的格式

DELIMITER $$

USE `database_name`$$

DROP TRIGGER  `Tigger_name`$$

CREATE 
    TRIGGER `Tigger_name` BEFORE/AFTER INSERT /UPDATE/DELETE ON `Table_1` /* trigger fire from this table*/
    FOR EACH ROW BEGIN
        /* (query) action perform */ 
        update table_2 set field_1="value" where table_2.id=New.field_1; /*New.field_1 is refrance Table_1.field_1. which record effect by action*/
    END;
$$

DELIMITER ;

参考文献:https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

相关问题