Table of Contents
概念
语法
实例
Home Database Mysql Tutorial Mysql中触发器的使用

Mysql中触发器的使用

Jun 07, 2016 pm 02:50 PM
mysql affairs use concept trigger

概念 触发器是一种特殊的事务,它监听增删改操作,并触发增删改操作。主要是用来处理一些比较复杂的业务逻辑以保证数据的联动性。其包含了四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/d

概念

触发器是一种特殊的事务,它监听增删改操作,并触发增删改操作。主要是用来处理一些比较复杂的业务逻辑以保证数据的联动性。其包含了四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/delete)

语法

<code class=" hljs sql">
<span class="hljs-operator"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TRIGGER</span> <span class="hljs-string">'触发器名称'</span> <span class="hljs-string">'触发时间(after|before)'</span> <span class="hljs-string">'监听的事情(insert|update|delete) '</span>
<span class="hljs-keyword">ON</span> <span class="hljs-string">'触发地点'</span> <span class="hljs-keyword">FOR</span> <span class="hljs-keyword">EACH</span> <span class="hljs-keyword">ROW</span> 
<span class="hljs-keyword">BEGIN</span>
  sql1;</span>
  sql2;
  sql3;
<span class="hljs-operator"><span class="hljs-keyword">END</span>$</span></code>
Copy after login

FOR EACH ROW 用来标识触发器的类型,因为Mysql支持行级别的触发器,不支持语句触发器,故只能使用FOR EACH ROW 。因为触发器中使用’;’区分多个事件语句,故需要重新定义定界符。

实例

修改定界符为$
这里写图片描述
创建触发器
这里写图片描述
查看触发器
这里写图片描述
执行操作之前
t4表数据如下
这里写图片描述
t3表数据如下
这里写图片描述
执行相应的操作之后
这里写图片描述

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

See all articles