MySQL触发器trigger学习_MySQL
触发器:一类特殊的事物,可监视某种数据操作,并触发相关操作(insert/update/delete);表中的某些数据改变,希望同时可以引起其它相关数据改变的需求。 作用:变化自动完成某些语句查询,添加程序的灵活性。 创建触发器语法: #delimiter $$ //默认情况下,delimiter是分号“;”,sql语句带有;号结尾会报错,没到end就算到结束了,使用delimiter $$作用就是告诉mysql语句的结尾换成以$结束,相应使用end$$结束
create trigger test1 #触发器名称 (after/before) #触发时间 (insert/update/delete) #监视事件 on table #监视地点(表名) for each row #mysql必须加的 begin sql1 ... sqlN end; #end$$
实例操作: goods商品表和ord订单表
CREATE TABLE goods( `goods_id` INT(10), `name` VARCHAR(20), `num` SMALLINT(4) )ENGINE=INNODB CHARSET=utf8 CREATE TABLE `ord`( `oid` INT(10), `gid` INT(10), `much` INT(10) )ENGINE=INNODB CHARSET=utf8 INSERT INTO goods VALUES (1, 'cat', 26),(2, 'dog', 26),(3, 'pig', 26);
#创建触发器test1 DELIMITER $$ CREATE TRIGGER test1 AFTER INSERT ON `ord` FOR EACH ROW BEGIN UPDATE goods SET num= num - new.much WHERE goods_id = new.gid; END$$
mysql> CREATE TRIGGER test1 -> AFTER -> INSERT -> ON `ord` -> FOR EACH ROW -> BEGIN -> UPDATE goods SET num= num - new.much WHERE goods_id = new.gid; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO `ord` VALUES (1, 2, 2)$$ Query OK, 1 row affected (0.03 sec) mysql> select * from ord$$ +------+------+------+ | oid | gid | much | +------+------+------+ | 1 | 2 | 2 | +------+------+------+ 1 row in set (0.00 sec) mysql> select * from goods$$ +----------+------+------+ | goods_id | name | num | +----------+------+------+ | 1 | cat | 26 | | 2 | dog | 24 | | 3 | pig | 26 | +----------+------+------+ 3 rows in set (0.00 sec) goods表里面的dog由26变成24
2、用户取消订单后商品库存订单的商品数要正常入库。以下只写触发器trigger,操作过程不写太多比较乱。
#触发器test2 CREATE TRIGGER test2 AFTER DELETE ON `ord` FOR EACH ROW BEGIN UPDATE goods SET num = num + old.much WHERE goods_id = old.gid; END $$

3、用户更新订单的时候,商品库存要根据订单的数量正常更新。
#触发器test3 CREATE TRIGGER test3 AFTER UPDATE ON `ord` FOR EACH ROW BEGIN UPDATE goods SET num = num + old.much - new.much WHERE goods_id = new.gid; END$$

查询触发器

删除触发器


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to hide text before any click in PowerPoint If you want text to appear when you click anywhere on a PowerPoint slide, setting it up is quick and easy. To hide text before clicking any button in PowerPoint: Open your PowerPoint document and click the Insert menu. Click on New Slide. Choose Blank or one of the other presets. Still in the Insert menu, click Text Box. Drag a text box onto the slide. Click the text box and enter your

In Oracle database, you can use the CREATE TRIGGER statement to add triggers. A trigger is a database object that can define one or more events on a database table and automatically perform corresponding actions when the event occurs.

How to write triggers in MySQL using PHP MySQL is a commonly used relational database management system, and PHP is a popular server-side scripting language. Using PHP to write triggers in MySQL can help us realize automated database operations. This article will introduce how to use PHP to write MySQL triggers and provide specific code examples. Before starting, make sure that MySQL and PHP have been installed and the corresponding database tables have been created. 1. Create PHP files and data

MySQL triggers are row-level. According to SQL standards, triggers can be divided into two types: 1. Row-level triggers, which will be activated once for each row of data modified. If a statement inserts 100 rows of data, the trigger will be called 100 times; 2. Statement-level triggers The trigger is activated once for each statement. A statement that inserts 100 rows of data will only call the trigger once. MySQL only supports row-level triggers, not prepared statement-level triggers.

How to write custom triggers and stored procedures in MySQL using PHP Introduction: When developing applications, we often need to perform some operations at the database level, such as inserting, updating, or deleting data. MySQL is a widely used relational database management system, and PHP is a popular server-side scripting language. This article will introduce how to write custom triggers and stored procedures in MySQL using PHP, and provide specific code examples. 1. What are triggers and stored procedure triggers (Trigg

How to write custom triggers in MySQL using Python Triggers are a powerful feature in MySQL that can define some automatically executed operations on tables in the database. Python is a concise and powerful programming language that can easily interact with MySQL. This article will introduce how to write custom triggers using Python and provide specific code examples. First, we need to install and import the PyMySQL library, which is Python's way of working with a MySQL database

How to use MySQL triggers to implement automatic archiving of data Introduction: In the field of modern data management, automatic archiving and cleaning of data is an important and common requirement. As the amount of data increases, retaining complete historical data will occupy excessive storage resources and reduce query performance. MySQL triggers provide an effective way to achieve this requirement. This article will introduce how to use MySQL triggers to achieve automatic archiving of data. 1. What is a MySQL trigger? A MySQL trigger is a special kind of memory.

A MySQL trigger is a special program used in the database management system to monitor the operations of a specific table and perform corresponding operations based on predefined conditions. When creating a MySQL trigger, we can use parameters to flexibly pass data and information, making the trigger more versatile and applicable. In MySQL, triggers can trigger and execute corresponding logic before or after the INSERT, UPDATE, and DELETE operations of a specific table. Using parameters can make triggers more flexible and can be passed as needed.
