三种出发器: insert 触发器, delete触发器, update触发器
eg:
CREATE TABLE tableA
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tablename VARCHAR(20));
CREATE TABLE tableB
(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
tablename VARCHAR(20));
CREATE TRIGGER t_insert_tableA
AFTER INSERT ON tableA
FOR EACH ROW
INSERT INTO tableB(tablename) VALUES(new.tablename);
INSERT INTO tableA VALUES(1,'aa');
CREATE TRIGGER t_inbefore_tableA
BEFORE INSERT ON tableA
FOR EACH ROW
INSERT INTO tableB(tablename) VALUES(new.tablename);
INSERT INTO tableA VALUES(2, 'bb');
CREATE TRIGGER t_del_tableA
BEFORE DELETE ON tableA
FOR EACH ROW
DELETE FROM tableB WHERE id=old.id;
DELETE FROM tableA WHERE id=1;
DELIMITER //
CREATE TRIGGER t_update_tableA
BEFORE UPDATE ON tableA
FOR EACH ROW
BEGIN
SET new.tablename = UPPER(new.tablename);
END //
DELIMITER ;
UPDATE tableA SET tablename='tian' WHERE id=2;
阅读(1795) | 评论(0) | 转发(0) |