MySQL數(shù)據(jù)庫?觸發(fā)器?trigger
一、基本概念
觸發(fā)器是一種特殊類型的存儲過程,觸發(fā)器通過事件進(jìn)行觸發(fā)而被執(zhí)行
觸發(fā)器 trigger 和js事件類似
1、作用
- 寫入數(shù)據(jù)表前,強(qiáng)制檢驗(yàn)或轉(zhuǎn)換數(shù)據(jù)(保證數(shù)據(jù)安全)
- 觸發(fā)器發(fā)生錯誤時,異動的結(jié)果會被撤銷(事務(wù)安全)
- 部分?jǐn)?shù)據(jù)庫管理系統(tǒng)可以針對數(shù)據(jù)定義語言DDL使用觸發(fā)器,稱為DDL觸發(fā)器
- 可以依照特定的情況,替換異動的指令 instead of(mysql不支持)
2、觸發(fā)器的優(yōu)缺點(diǎn)
2.1、優(yōu)點(diǎn)
- 觸發(fā)器可通過數(shù)據(jù)庫中的相關(guān)表實(shí)現(xiàn)級聯(lián)更改(如果一張表的數(shù)據(jù)改變,可以利用觸發(fā)器實(shí)現(xiàn)對其他表的操作,用戶不知道)
- 保證數(shù)據(jù)安全,進(jìn)行安全校驗(yàn)
2.2、缺點(diǎn)
- 對觸發(fā)器過分依賴,勢必影響數(shù)據(jù)庫的結(jié)構(gòu),同時增加了維護(hù)的復(fù)雜度
- 造成數(shù)據(jù)在程序?qū)用娌豢煽?/li>
二、創(chuàng)建觸發(fā)器
1、基本語法
create trigger 觸發(fā)器名字 觸發(fā)時機(jī) 觸發(fā)事件 on 表 for each row begin end
2、觸發(fā)對象
on 表 for each row 觸發(fā)器綁定表中所有行,沒一行發(fā)生指定改變的時候,就會觸發(fā)觸發(fā)器
3、觸發(fā)時機(jī)
每張表對應(yīng)的行都有不同的狀態(tài),當(dāng)SQL指令發(fā)生的時候,都會令行中數(shù)據(jù)發(fā)生改變,每一行總會有兩種狀態(tài):數(shù)據(jù)操作前和數(shù)據(jù)操作后
- before: 數(shù)據(jù)發(fā)生改變前的狀態(tài)
- after: 數(shù)據(jù)已經(jīng)發(fā)生改變后的狀態(tài)
4、觸發(fā)事件
mysql中觸發(fā)器針對的目標(biāo)是數(shù)據(jù)發(fā)生改變,對應(yīng)的操作只有寫操作(增刪改)
- inert 插入操作
- update 更新操作
- delete 刪除操作
5、注意事項
一張表中,每一個觸發(fā)時機(jī)綁定的觸發(fā)事件對應(yīng)的觸發(fā)器類型只能有一個
一張表表中只能有一個對應(yīng)的after insert 觸發(fā)器
最多只能有6個觸發(fā)器
before insert after insert before update after update before delete after delete
需求:
下單減庫存
有兩張表,一張是商品表,一張是訂單表(保留商品ID)每次訂單生成,商品表中對應(yīng)的庫存就應(yīng)該發(fā)生變化
創(chuàng)建兩張表:
create table my_item(
id int primary key auto_increment,
name varchar(20) not null,
count int not null default 0
) comment '商品表';
create table my_order(
id int primary key auto_increment,
item_id int not null,
count int not null default 1
) comment '訂單表';
insert my_item (name, count) values ('手機(jī)', 100),('電腦', 100), ('包包', 100);
mysql> select * from my_item;
+----+--------+-------+
| id | name | count |
+----+--------+-------+
| 1 | 手機(jī) | 100 |
| 2 | 電腦 | 100 |
| 3 | 包包 | 100 |
+----+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from my_order;
Empty set (0.02 sec)創(chuàng)建觸發(fā)器:
如果訂單表發(fā)生數(shù)據(jù)插入,對應(yīng)的商品就應(yīng)該減少庫存
delimiter $$
create trigger after_insert_order_trigger after insert on my_order for each row
begin
-- 更新商品庫存
update my_item set count = count - 1 where id = 1;
end
$$
delimiter ;
三、查看觸發(fā)器
-- 查看所有觸發(fā)器
show triggers\G
*************************** 1. row ***************************
Trigger: after_insert_order_trigger
Event: INSERT
Table: my_order
Statement: begin
update my_item set count = count - 1 where id = 1;
end
Timing: AFTER
Created: 2022-04-16 10:00:19.09
sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8mb4_general_ci
1 row in set (0.00 sec)
-- 查看創(chuàng)建語句
show crate trigger 觸發(fā)器名字;
-- eg:
show create trigger after_insert_order_trigger;四、觸發(fā)觸發(fā)器
讓觸發(fā)器執(zhí)行,讓觸發(fā)器指定的表中,對應(yīng)的時機(jī)發(fā)生對應(yīng)的操作
insert into my_order (item_id, count) values(1, 1); mysql> select * from my_order; +----+---------+-------+ | id | item_id | count | +----+---------+-------+ | 1 | 1 | 1 | +----+---------+-------+ 1 row in set (0.00 sec) mysql> select * from my_item; +----+--------+-------+ | id | name | count | +----+--------+-------+ | 1 | 手機(jī) | 99 | | 2 | 電腦 | 100 | | 3 | 包包 | 100 | +----+--------+-------+ 3 rows in set (0.00 sec)
五、刪除觸發(fā)器
drop trigger 觸發(fā)器名字; -- eg drop trigger after_insert_order_trigger;
六、觸發(fā)器的應(yīng)用
記錄關(guān)鍵字 new old
1、完善
商品自動扣除庫存
觸發(fā)器針對的是數(shù)據(jù)表中的每條記錄,每行數(shù)據(jù)再操作前后都有一個對應(yīng)的狀態(tài)
觸發(fā)器在執(zhí)行之前就將對應(yīng)的數(shù)據(jù)狀態(tài)獲取到了:
- 將沒有操作之前的數(shù)據(jù)狀態(tài)都保存到
old關(guān)鍵字中 - 操作后的狀態(tài)都放在
new中
觸發(fā)器中,可以通過old和new來獲取綁定表中對應(yīng)的記錄數(shù)據(jù)
基本語法:
關(guān)鍵字.字段名
old和new并不是所有觸發(fā)器都有
- insert 插入前為空,沒有old
- delete 清除數(shù)據(jù),沒有new
商品自動扣減庫存:
delimiter $$
create trigger after_insert_order_trigger after insert on my_order for each row
begin
-- 通過new關(guān)鍵字獲取新數(shù)據(jù)的id 和數(shù)量
update my_item set count = count - new.count where id = new.item_id;
end
$$
delimiter ;觸發(fā)觸發(fā)器:
mysql> select * from my_order; +----+---------+-------+ | id | item_id | count | +----+---------+-------+ | 1 | 1 | 1 | +----+---------+-------+ mysql> select * from my_item; +----+--------+-------+ | id | name | count | +----+--------+-------+ | 1 | 手機(jī) | 99 | | 2 | 電腦 | 100 | | 3 | 包包 | 100 | +----+--------+-------+ insert into my_order (item_id, count) values(2, 3); mysql> select * from my_order; +----+---------+-------+ | id | item_id | count | +----+---------+-------+ | 1 | 1 | 1 | | 2 | 2 | 3 | +----+---------+-------+ mysql> select * from my_item; +----+--------+-------+ | id | name | count | +----+--------+-------+ | 1 | 手機(jī) | 99 | | 2 | 電腦 | 97 | | 3 | 包包 | 100 | +----+--------+-------+
2、優(yōu)化
如果庫存數(shù)量沒有商品訂單多怎么辦?
-- 刪除原有觸發(fā)器
drop trigger after_insert_order_trigger;
-- 新增判斷庫存觸發(fā)器
delimiter $$
create trigger after_insert_order_trigger after insert on my_order for each row
begin
-- 查詢庫存
select count from my_item where id = new.item_id into @count;
-- 判斷
if new.count > @count then
-- 中斷操作,暴力拋出異常
insert into xxx values ('xxx');
end if;
-- 通過new關(guān)鍵字獲取新數(shù)據(jù)的id 和數(shù)量
update my_item set count = count - new.count where id = new.item_id;
end
$$
delimiter ;
結(jié)果驗(yàn)證:
mysql> insert into my_order (item_id, count) values(3, 101); ERROR 1146 (42S02): Table 'mydatabase2.xxx' doesn't exist mysql> select * from my_order; +----+---------+-------+ | id | item_id | count | +----+---------+-------+ | 1 | 1 | 1 | | 2 | 2 | 3 | +----+---------+-------+ 2 rows in set (0.00 sec) mysql> select * from my_item; +----+--------+-------+ | id | name | count | +----+--------+-------+ | 1 | 手機(jī) | 99 | | 2 | 電腦 | 97 | | 3 | 包包 | 100 | +----+--------+-------+ 3 rows in set (0.00 sec)
到此這篇關(guān)于MySQL數(shù)據(jù)庫 觸發(fā)器 trigger的文章就介紹到這了,更多相關(guān)MySQL trigger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談MYSQL中樹形結(jié)構(gòu)表3種設(shè)計優(yōu)劣分析與分享
在開發(fā)中經(jīng)常遇到樹形結(jié)構(gòu)的場景,本文將以部門表為例對比幾種設(shè)計的優(yōu)缺點(diǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Linux下將數(shù)據(jù)庫從MySQL遷移到MariaDB的基礎(chǔ)操作教程
這篇文章主要介紹了將數(shù)據(jù)庫從MySQL遷移到MariaDB的基礎(chǔ)操作教程,當(dāng)然遷移之前不要忘記數(shù)據(jù)庫的備份!需要的朋友可以參考下2015-11-11
MySQL Antelope和Barracuda的區(qū)別分析
這篇文章主要介紹了MySQL Antelope和Barracuda的區(qū)別分析,Antelope和Barracude都是一種文件格式,需要的朋友可以參考下2014-07-07
MySQL的id關(guān)聯(lián)和索引使用的實(shí)際優(yōu)化案例
這篇文章主要介紹了MySQL的id關(guān)聯(lián)實(shí)際優(yōu)化案例,關(guān)聯(lián)和索引一直是MySQL常見的可優(yōu)化大塊兒,需要的朋友可以參考下2015-05-05
mysql根據(jù)拼音字母查詢(簡單易懂的字段拼音查詢)
MySQL在開發(fā)中,我們經(jīng)常需要根據(jù)字段拼音查詢數(shù)據(jù)庫中的數(shù)據(jù),它支持多種查詢方式,包括根據(jù)拼音字母查詢,使用 Collation 可以方便地進(jìn)行簡單的拼音查詢,而使用拼音索引可以大幅提高查詢性能,根據(jù)具體的需求和情況,我們可以選擇合適的方法來實(shí)現(xiàn)拼音查詢2023-10-10
wampserver下mysql導(dǎo)入數(shù)據(jù)庫的步驟
這篇文章主要介紹了wampserver下mysql導(dǎo)入數(shù)據(jù)庫的步驟,需要的朋友可以參考下2016-08-08
MySQL 學(xué)習(xí)總結(jié) 之 初步了解 InnoDB 存儲引擎的架構(gòu)設(shè)計
這篇文章主要介紹了MySQL 學(xué)習(xí)總結(jié) 之 初步了解 InnoDB 存儲引擎的架構(gòu)設(shè)計,文中給大家提到了mysql存儲引擎有哪些,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02
mysql數(shù)據(jù)庫查詢基礎(chǔ)命令詳解
這篇文章主要介紹了mysql數(shù)據(jù)庫查詢基礎(chǔ)命令,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11

