阅读量:0
要求
在product表上分别创建BEFORE INSERT、AFTER UPDATE和AFTER DELETE 3个触发器,
触发器名称分别为product_bf_insert、product_af_update和product_af_del。执行语句部分是向operate表插入操作方法和操作时间。
1.1 BEFORE INSERT 触发器
CREATE TRIGGER product_bf_insert BEFORE INSERT ON product FOR EACH ROW BEGIN INSERT INTO operate(Op_type, Op_time) VALUES ('BEFORE INSERT', NOW()); END;
1.2 AFTER UPDATE 触发器
CREATE TRIGGER product_af_update AFTER UPDATE ON product FOR EACH ROW BEGIN INSERT INTO operate(Op_type, Op_time) VALUES ('AFTER UPDATE', NOW()); END;
1.3 AFTER DELETE 触发器
CREATE TRIGGER product_af_del AFTER DELETE ON product FOR EACH ROW BEGIN INSERT INTO operate(Op_type, Op_time) VALUES ('AFTER DELETE', NOW()); END;
对 product 表执行 INSERT、UPDATE 和 DELETE 操作
2.1 INSERT 操作
INSERT INTO product(Id, Name, Function, Company, Address) VALUES (1, 'Product A', 'Function A', 'Company A', 'Address A');
2.2 UPDATE 操作
UPDATE product SET Name = 'Updated Product A' WHERE Id = 1;
2.3 DELETE 操作
DELETE FROM product WHERE Id = 1;
3.删除 product_bf_insert 和 product_af_update 这两个触发器
DROP TRIGGER product_bf_insert; DROP TRIGGER product_af_update;