Mysql数据库的触发程序

时间:2022-01-22 17:22:00
 /**
**创建表
*/
CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
b4 INT DEFAULT 0
);
/**
**创建触发程序
*/
DROP TRIGGER IF EXISTS testref;
DELIMITER //
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = new.a1;
DELETE FROM test3 WHERE a3 = new.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = new.a1;
END //
DELIMITER ; /**
** 插入初始数据
*/
INSERT INTO test3(a3) VALUES (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL); INSERT INTO test4(a4) VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0); /**
** 测试触发过程
*/
INSERT INTO test1 VALUES (1),(3),(1),(7),(1),(8),(4),(4);