小贝_mysql触发器使用_MySQL
触发器
简要
1、触发器基本概念
2、触发器语法及实战例子
3、before和after区别
一、触发器基本概念
1、一触即发
2、作用: 监视某种情况并触发某种操作
3、观察场景
一个电子商城:
商品表,goods
主键(goods_id) |
商品名称(goods_name) |
库存(goods_number) |
1 |
iphone6 |
10 |
2 |
小米手机 |
28 |
订单表,orders
订单主键(order_id) |
商品主键(goods_id) |
购买数量(buy_num) |
1 |
2 |
3 |
2 |
1 |
4 |
从php的角度看,完成下单与减少库存的逻辑如下:
a、下单后,往orders表插入数据:
insert into orders(goods_id,bug_num)values(2,3);
b、修改goods对应商品的库存:
update goods setgoods_number=goods_number-3 where goods_id=2;
总结: 这两个逻辑可以看成是一个整体,或者说,insert—>触发update
处理方案: 使用触发器来解决上述问题,我们可以监视某张表的变化,当发生某种变化时,触发某个操作
4、触发器监视以及触发什么变化
a、监视update/insert/delete
b、触发update/insert/delete
二、触发器语法
1、创建语法的四个要素
2、创建触发器
2.1、测试案例的表结构
#商品表 create table goods (goods_id int,goods_name varchar(10),goods_number smallint)charset=utf8;
insert into goods values(1,'iphone6',10),(2,'小米手机',28); |
|
2.2、创建触发器语法
createtrigger triggerName
after/beforeinsert/update/deleteon Table
begin
sql语句(一句或多句sql)
end;
2.3、触发器实例
2.3.1、建立触发器,往订单表orders插入数据时,更新商品表goods的库存量
往orders表插入数据前,各个表的数据如下
往orders表插入数据:
查看goods表的数据:
此时,orders表插入的是iphone6两台,但是减少的却是小米手机商品的库存。
当往orders表插入的是小米手机两台呢?
此时,减少的也是小米手机,但是减少的是1,而不是2
问题:出在哪里?
1、查看刚建立的触发器信息
原来触发的sql语句是固定某个商品id的,因此,触发只对该商品id发生作用
2、如何在触发器引用行的值?
对于insert而言,新增的行,用new来表示,行中的每一列的值,用new.列名来表示
3、修改触发器t1(删除原来重新建立)
4、验证结果
插入前,各个表数据如下
插入orders表,iPhone6三台
插入orders表,小米手机五台
2.3.2、建立触发器,往订单表orders删除数据时,更新商品表goods的库存量
a、建立触发器t2
b、删除orders的数据
b1、删除前,各个表数据
b2、删除后,各个表数据:
总结:
对于delete而言,如何在触发器引用行的值?
对于delete操作来说,它要操作的数据,已经是存在表中了,因此用old来表示,行中的每一列的值,用old.列名来表示
2.3.3、建立触发器,往订单表orders修改数据时,更新商品表goods的库存量
a、创建触发器t3
b、修改orders表数据
b1、修改前,各个表的数据
b2、修改后,各个表的数据
总结:
对于update而言,如何在触发器引用行的值?
对于update操作来说,它要操作的数据,已经是存在表中了,因此用old来引用修改前的值,修改后则为用new来引用新值
2.4、查看触发器和删除触发器语法
2.4.1、查看所有触发器
2.4.2、查看某个触发器
2.4.3、删除触发器
三、before与after的区别
1、区别:
a、after是先完成数据的增删改后,再触发
触发中的语句晚于增删改,无法影响前面的增删改动作
b、before是先完成触发,再增删改
触发的语句先于监视的增删改发生,我们有机会判断,修改即将发生的操作
2、案例:
对于所下订单进行判断,如果订单的数据>5,则认为是非法订单,强制把所订商品数据改成5
2.1、建立触发器
2.2、插入前,各个表的数据
2.3、插入后,各个表的数据
The quieter you become,the more you are able to hear!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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 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

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 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 write custom stored procedures, triggers and functions in MySQL using C# MySQL is a widely used open source relational database management system, and C# is a powerful programming language for development tasks that require interaction with the database. Say, MySQL and C# are good choices. In MySQL, we can use C# to write custom stored procedures, triggers and functions to achieve more flexible and powerful database operations. This article will guide you in using C# to write and execute

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.
