在MySQL中使用触发器Trigger的操作过程

想监控Bugzilla数据库中几个重要的表,如果它们发生了任何改变(增、删、改),都希望能够记录下来,以便后面再写程序来分析。很自然,就想到使用MySQL的触发器(Trigger)了,学习了一会,记录如下:
1. 先建立一个新的表用于记录我需要的变化:


2. 针对需要监控的表,创建触发器:

请注意其中AFTER和BEFORE,以及OLD和NEW的使用。
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. OLD.col_name refers to a column of an existing row before it is updated or deleted. NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.

3. 查看当前数据库中的触发器:

参考资料:
http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
http://www.jicdesign.com/blog/web-development/how-to-use-mysql-triggers-to-log-table-changes.html

master

Stay hungry, stay foolish.

发表评论

邮箱地址不会被公开。 必填项已用*标注

*