触发器
简要
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!

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor
