CREATE DATABASE dbtest17;
USE dbtest17;
CREATE TABLE test_trigger (
id INT PRIMARY KEY AUTO_INCREMENT,
t_note VARCHAR(30)
);
CREATE TABLE test_trigger_log (
id INT PRIMARY KEY AUTO_INCREMENT,
t_log VARCHAR(30)
);
SELECT * FROM test_trigger;
SELECT * FROM test_trigger_log;
#向test_trigger_log数据表中插入before_insert的日志信息。
DELIMITER //
CREATE TRIGGER before_insert_test_tri
BEFORE INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log(t_log)
VALUES(‘before insert…’);
END //
DELIMITER ;
INSERT INTO test_trigger(t_note)
VALUES(‘Tom…’);
SELECT * FROM test_trigger;
SELECT * FROM test_trigger_log;
DELIMITER $
CREATE TRIGGER after_insert_test_tri
AFTER INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log(t_log)
VALUES(‘after insert…’);
END $
DELIMITER ;
INSERT INTO test_trigger(t_note)
VALUES(‘Jerry2…’);
SELECT * FROM test_trigger;
SELECT * FROM test_trigger_log;
#则报sqlstate_value为’HY000’的错误,从而使得添加失败。
CREATE TABLE employees
AS
SELECT * FROM atguigudb.employees
;
CREATE TABLE departments
AS
SELECT * FROM atguigudb.departments
;
DESC employees;
DELIMITER //
CREATE TRIGGER salary_check_trigger
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
#查询到要添加的数据的manager的薪资
DECLARE mgr_sal DOUBLE;
SELECT salary INTO mgr_sal FROM employees
WHERE employee_id = NEW.manager_id;
IF NEW.salary > mgr_sal
THEN SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = '薪资高于领导薪资错误';
END IF;
END //
DELIMITER ;
DESC employees;
INSERT INTO employees(employee_id,last_name,email,hire_date,job_id,salary,manager_id)
VALUES(300,‘Tom’,‘tom@126.com’,CURDATE(),‘AD_VP’,8000,103);
INSERT INTO employees(employee_id,last_name,email,hire_date,job_id,salary,manager_id)
VALUES(301,‘Tom1’,‘tom1@126.com’,CURDATE(),‘AD_VP’,10000,103);
SELECT * FROM employees;
SHOW TRIGGERS;
SHOW CREATE TRIGGER salary_check_trigger;
SELECT * FROM information_schema.TRIGGERS;
DROP TRIGGER IF EXISTS after_insert_test_tri;