时间:2020-12-26 12:24:52 | 栏目:Mysql | 点击:次
本文实例讲述了mysql触发器之创建使用触发器。分享给大家供大家参考,具体如下:
我们可以可以使用CREATE TRIGGER语句创建一个新的触发器,来看下具体的语法:
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW BEGIN ... END;
然后我们来详细看下上述sql的具体含义:
大概了解了之后,我们就来尝试创建触发器来记录employees表中行数据的更改情况,先来看下这个表的结构:
mysql> DESC employees; +----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+--------------+------+-----+---------+-------+ | employeeNumber | int(11) | NO | PRI | NULL | | | lastName | varchar(50) | NO | | NULL | | | firstName | varchar(50) | NO | | NULL | | | extension | varchar(10) | NO | | NULL | | | email | varchar(100) | NO | | NULL | | | officeCode | varchar(10) | NO | MUL | NULL | | | reportsTo | int(11) | YES | MUL | NULL | | | jobTitle | varchar(50) | NO | | NULL | | +----------------+--------------+------+-----+---------+-------+ 8 rows in set
我们再来创建一个名为employees audit的新表,用来保存employees表中数据的更改:
CREATE TABLE employees_audit ( id INT AUTO_INCREMENT PRIMARY KEY, employeeNumber INT NOT NULL, lastname VARCHAR(50) NOT NULL, changedat DATETIME DEFAULT NULL, action VARCHAR(50) DEFAULT NULL );
再来创建一个BEFORE UPDATE触发器,该触发器在对employees表中的行记录更改之前被调用:
DELIMITER $$ CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO employees_audit SET action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname, changedat = NOW(); END$$ DELIMITER ;
在上述触发器的主体中,我们使用OLD关键字来访问受触发器影响的行的employeeNumber和lastname列。我们要注意的是,在为insert定义的触发器中,可以仅使用NEW关键字。不能使用OLD关键字。但是,在为DELETE定义的触发器中,没有新行,因此您只能使用OLD关键字。在update触发器中,OLD是指更新前的行,而NEW是更新后的行。
然后,我们可以使用SHOW TRIGGERS语句,来查看数据库中的触发器:
mysql> SHOW TRIGGERS; +------------------------+--------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation | +------------------------+--------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | before_employee_update | UPDATE | employees | BEGIN INSERT INTO employees_audit SET action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname, changedat = NOW(); END | BEFORE | 2017-08-02 22:06:36.40 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | root@localhost | utf8 | utf8_general_ci | utf8_general_ci | +------------------------+--------+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 1 row in set
完事我们就来更新employees表以检查触发器是否被调用:
UPDATE employees SET lastName = 'Maxsu' WHERE employeeNumber = 1056;
可以使用以下查询来查询employees_audit表,用以检查触发器是否被UPDATE语句调用:
mysql> SELECT * FROM employees_audit; +----+----------------+----------+---------------------+--------+ | id | employeeNumber | lastname | changedat | action | +----+----------------+----------+---------------------+--------+ | 1 | 1056 | Hill | 2017-08-02 22:15:51 | update | +----+----------------+----------+---------------------+--------+ 1 row in set
如上面输出结果所示,触发器被真正调用,并在employees_audit表中插入一个新行。
好啦,本次记录就到这里了。
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》
希望本文所述对大家MySQL数据库计有所帮助。