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

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

MySQL 觸發器實現

標簽:
MySQL

Summary: in this tutorial, you will learn about MySQL triggers implementation. In addition, we will show you how MySQL stores trigger definitions and the limitations of triggers in MySQL.

Introduction to MySQL triggers

In MySQL, a trigger is a set of SQL statements that is invoked automatically when a change is made to the data on the associated table. A trigger can be defined to be invoked either before or after the data is changed by INSERTUPDATE or DELETE statements. MySQL allows you to define maximum six triggers for each table.

  • BEFORE INSERT – activated before data is inserted into the table.

  • AFTER INSERT- activated after data is inserted into the table.

  • BEFORE UPDATE – activated before data in the table is updated.

  • AFTER UPDATE - activated after data in the table is updated.

  • BEFORE DELETE – activated before data is removed from the table.

  • AFTER DELETE – activated after data is removed from the table.

When you use a statement that makes change to the table but does not use INSERTDELETE or UPDATE statement, the trigger is not invoked. For example, the TRUNCATE statement removes the whole data of a table but does not invoke the trigger associated with that table.

There are some statements that use the INSERT statement behind the scenes such as REPLACE statement and LOAD DATA statement. If you use these statements, the corresponding triggers associated with the tables if available will be invoked.

Triggers defined for a table must have a unique name. You can have the same trigger name that defines for different tables but it is not recommended. In practice, the names of triggers follow the following naming convention:

(BEFORE | AFTER)_tableName_(INSERT| UPDATE | DELETE)

 

MySQL Triggers Storage

MySQL stores triggers in a data directory e.g., /data/classicmodels/ with the files named tablename.TRG and triggername.TRN:

  • The tablename.TRG file maps the trigger to the corresponding table.

  • the triggername.TRN file contains the trigger definition.

You can back up the MySQL triggers by copying the trigger files to the backup folder. You can also backup the triggers using the mysqldump tool.

MySQL Trigger Limitations

MySQL triggers have all features in standard SQL however there are some limitations that you should know before using them in your applications.

MySQL triggers cannot:

  • Use SHOWLOAD DATALOAD TABLEBACKUP DATABASE, RESTOREFLUSH and RETURN statements.

  • Use statements that commit or rollback implicitly or explicitly such as COMMITROLLBACKSTART TRANSACTIONLOCK/UNLOCK TABLESALTERCREATEDROPRENAME, etc.

  • Use prepared statements such as PREPAREEXECUTE, etc.

  • Use dynamic SQL statements.

  • Call a stored procedure or stored function.

In this tutorial, we have shown you how triggers are implemented in MySQL. We also discussed about trigger’s storage as well as trigger’s limitations in MySQL.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/trigger/mysql-trigger-implementation

點擊查看更多內容
TA 點贊

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

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

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

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

幫助反饋 APP下載

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

公眾號

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

舉報

0/150
提交
取消