1,创建表
CREATE TABLE Product (
Id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
Function VARCHAR(50),
Company VARCHAR(20) NOT NULL,
Address VARCHAR(20)
);
CREATE TABLE Operate (
Op_id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
Op_type VARCHAR(20) NOT NULL,
Op_time VARCHAR(20) NOT NULL
);
2,创建触发器
DELIMITER //
CREATE TRIGGER product_bf_insert
BEFORE INSERT ON Product
FOR EACH ROW
BEGIN
INSERT INTO Operate (Op_type, Op_time) VALUES ('Insert', NOW());
END; //
DELIMITER ;
DELIMITER //
CREATE TRIGGER product_af_update
AFTER UPDATE ON Product
FOR EACH ROW
BEGIN
INSERT INTO Operate (Op_type, Op_time) VALUES ('Update', NOW());
END; //
DELIMITER ;
DELIMITER //
CREATE TRIGGER product_af_del
AFTER DELETE ON Product
FOR EACH ROW
BEGIN
INSERT INTO Operate (Op_type, Op_time) VALUES ('Delete', NOW());
END; //
DELIMITER ;
3,执行操作
INSERT 操作
INSERT INTO Product (Name, Function, Company, Address) VALUES ('Product A', 'Feature X', 'Manufacturer Y', 'Location Z');
UPDATE 操作
UPDATE Product SET Function = 'New Feature' WHERE Id = 1;
DELETE 操作
DELETE FROM Product WHERE Id = 1;
删除触发器
DROP TRIGGER IF EXISTS product_bf_insert;
DROP TRIGGER IF EXISTS product_af_update;