亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

創建mysql觸發器

標簽:
MySQL

Summary: in this tutorial, you will learn how to create trigger in MySQL by using the CREATE TRIGGER statement.

You should follow the introduction to SQL triggers and trigger implementation in MySQL first before going forward with this tutorial.

MySQL trigger syntax

In order to create a trigger you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:

CREATE TRIGGER trigger_name trigger_time trigger_event  ON table_name  FOR EACH ROW  BEGIN ... END

Let’s examine the syntax above in more detail.

  • You put the trigger name after the CREATE TRIGGER statement. The trigger name should follow the naming convention [trigger time]_[table name]_[trigger event], for example before_employees_update.

  • Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a trigger. You use BEFOREkeyword if you want to process action prior to the change is made on the table and AFTER if you need to process action after the change is made.

  • Trigger event can be INSERTUPDATE or DELETE. This event causes trigger to be invoked. A trigger only can be invoked by one event. To define a trigger that is invoked by multiple events, you have to define multiple triggers, one for each event.

  • A trigger must be associated with a specific table. Without a table trigger would not exist therefore you have to specify the table name after the ON keyword.

  • The SQL statements are placed between BEGIN and END block.

  • The OLD and NEW keywords are very handy. The OLD keyword refers to the existing record before you change the data and the NEW keyword refers to the new row after you change the data.

MySQL trigger example

Let’s start creating a trigger in MySQL to audit the changes of the employees table.

First, we have employees table in our MySQL sample database as follows:

Employee Table

Second, we create a new table named employees_audit to keep the changes of the employee records. The following script creates the employee_audit table.

CREATE TABLE employees_audit (      id int(11) NOT NULL AUTO_INCREMENT,      employeeNumber int(11) NOT NULL,      lastname varchar(50) NOT NULL,      changedon datetime DEFAULT NULL,      action varchar(50) DEFAULT NULL,      PRIMARY KEY (id) )

Third, we create a BEFORE UPDATE trigger to be invoked before a change is made to the employees table.

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,         changedon = NOW(); END$$ DELIMITER ;

If you take a look at the schema, you will see before_employee_update trigger under the employees table as follows:

MySQL Trigger Example

Now it’s time to update an employee record to test if the trigger is really invoked.

UPDATE employees SET lastName = 'Phan' WHERE employeeNumber = 1056

To check if the trigger was invoked by the UPDATE statement, we can query the employees_audit table by using the following query:

SELECT *  FROM employees_audit

The following is the output of the query:

MySQL Trigger Audit Table

As you see, our trigger was really invoked so that we have a new record in the employees_audit table.

In this tutorial, you have learned how to create a trigger in MySQL. We also shown you how to develop a trigger to audit the changes of the employees table.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/trigger/create-the-first-trigger-in-mysql

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消