mysql 触发器

时间:2022-05-27 22:50:48   收藏:0   阅读:16

题目

构造一个触发器audit_log,在向employees_test表中插入一条数据的时候,触发插入相关的数据到audit中。

环境sql

drop table if exists employees_test;
drop table if exists audit;

CREATE TABLE employees_test(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);
CREATE TABLE audit(
    emp_no INT NOT NULL,
    NAME TEXT NOT NULL
);

INSERT INTO employees_test (ID,NAME,AGE,ADDRESS,SALARY)VALUES (1, ‘Paul‘, 32, ‘California‘, 20000.00 );

触发器创建sql

CREATE TRIGGER audit_log 
AFTER
INSERT
	ON
	employees_test
FOR EACH ROW 
BEGIN 
	INSERT INTO audit(emp_no, NAME) 
	VALUE (NEW.id, NEW.name);
END;

原文:https://www.cnblogs.com/jiangdewen/p/15344764.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!