學習資源: Codedata - MySQL 超新手入門(16)Triggers
CREATE TRIGGER 名稱 時機 事件
ON 表格名稱 FOR EACH ROW
BEGIN
敘述
END;
DELIMITER $$
CREATE TRIGGER cmdev.emp_before_update BEFORE UPDATE
ON cmdev.emp FOR EACH ROW
BEGIN
INSERT INTO emlog (message)
VALUES ('BEFORE UPDATE!');
END $$
DELIMTER;
DROP TRIGGER IF EXISTS trigger名稱
DELIMITER $
DROP TRIGGER IF EXISTS cmdev.mytrigger$$
CREATE TRIGGER cmdev.mytrigger BEFORE UPDATE
ON cmdev.emp FOR EACH ROW
BEGIN
...
END $$
DELIMITER ;
DELIMITER $$
DROP TRIGGER IF EXISTS cmdev.emp_before_epdate$$
CREATE TRIGGER cmdev.emp_before_update BEFORE UPDATE
ON cmdev.emp FOR EACH ROW
BEGIN
DEVLARE v_message VARCHAR(64) DEFAULT 'BEFORE UPDATE: ';
SET v_message = CONCAT( v_message,
OLD.deptno, ' -> ', NEW.deptno);
INSERT INTO emplog (message) VALUES (v_message);
END $$
DELIMITER;
DELIMITER $$
DROP TRIGGER IF EXISTS cmdev.emp_before_insert$$
CREATE TRIGGER cmdev.emp_before_insert BEFORE INSERT
ON cmdev.emp FOR EACH ROW
BEGIN
SET NEW.ename = UPPER(NEW.ename);
SET NEW.job = UPPER(NEW.job);
END $$
DELIMITER;
SHOW CREATE TRIGGER trigger名稱