当前位置:网站首页>Database Experiment 8 trigger experiment
Database Experiment 8 trigger experiment
2022-04-23 21:45:00 【Hebuter-Rui】
1. The experiment purpose
Master the design and use of database triggers
2. The contents and requirements of the experiment
Definition BEFORE Trigger and AFTER trigger . Be able to understand the function and execution principle of different types of triggers , Verify the validity of the trigger .
3. The experimental steps
AFTER trigger
① stay Lineitem Define a table UPDATE trigger , When modifying order details ( That is, modify the order details price extendeprice、 discount discount、 tax rate tax) when , Automatically modify the order Orders Of TotalPrice, To maintain data consistency .
Source code :
CREATE TRIGGER UP_lineitem AFTER UPDATE ON lineitem FOR EACH ROW
BEGIN
UPDATE orders
SET totalprice = totalprice + (
new.extendedprice * (1 - new.discount) * (1 + new.tax) - old.extendedprice * (1 - old.discount) * (1 + old.tax)
)
WHERE
orderkey = new.orderkey;
END② stay Lineitem Define a table INSERT trigger , When adding an order detail , Automatically modify the order Orders Of TotalPrice, To keep the data consistent .
Source code :
CREATE TRIGGER IN_lineitem AFTER INSERT ON lineitem FOR EACH ROW
BEGIN
UPDATE orders
SET totalprice = new.extendedprice * (1 - new.discount) * (1 + new.tax)
WHERE
orderkey = new.orderkey;
END③ stay Lineitem Define a table DELETE trigger , When deleting an order detail , Automatically modify the order Orders Of TotalPrice, To maintain data consistency .
Source code :
CREATE TRIGGER DE_lineitem AFTER DELETE ON lineitem FOR EACH ROW
BEGIN
UPDATE orders
SET totalprice = totalprice - old.extendedprice * (1 - old.discount) * (1 + old.tax)
WHERE
orderkey = old.orderkey;
END④ Verify that the above three triggers work .
verification UPDATE Trigger source code :
SELECT
*
FROM
lineitem
WHERE
orderkey = 11;
SELECT
*
FROM
orders
WHERE
orderkey = 11;
UPDATE lineitem
SET tax = 0.16
WHERE
orderkey = 11;
SELECT
*
FROM
lineitem
WHERE
orderkey = 11;
SELECT
*
FROM
orders
WHERE
orderkey = 11;verification UPDATE Screenshot of trigger result :
Before the change lineitem surface :
Before the change orders surface :
After modification lineitem surface :
After modification orders surface :
verification INSERT Trigger source code :
SELECT
*
FROM
lineitem
WHERE
orderkey = 15;
SELECT
*
FROM
orders
WHERE
orderkey = 15;
INSERT INTO lineitem
VALUES
(
15,
5001,
15634,
1,
45,
95402,
0.10,
0.13,
'',
'',
NULL,
NULL,
NULL,
' ',
' ',
NULL
);
SELECT
*
FROM
lineitem
WHERE
orderkey = 15;
SELECT
*
FROM
orders
WHERE
orderkey = 15;verification INSERT Screenshot of trigger result :
Before insertion lineitem surface :
Before insertion orders surface :
After inserting lineitem surface :
After inserting orders surface :
verification DELETE Trigger source code :
SELECT
*
FROM
lineitem
WHERE
orderkey = 10;
SELECT
*
FROM
orders
WHERE
orderkey = 10;
DELETE
FROM
lineitem
WHERE
orderkey = 10
AND partkey = 20521;
SELECT
*
FROM
lineitem
WHERE
orderkey = 10;
SELECT
*
FROM
orders
WHERE
orderkey = 10;
verification DELETE Screenshot of trigger result :
Before deleting lineitem surface :
Before deleting orders surface :
After deleting lineitem surface :
After deleting orders surface :
2.BEFORE trigger
① stay Lineitem Define a table BEFORE UPDATE trigger , When modifying the quantity in the order details
(quantity) when , Check the supply list first PartSupp Available quantity in availqty Is it enough .
Source code :
CREATE TRIGGER BUP_lineitem BEFORE UPDATE ON lineitem FOR EACH ROW
BEGIN
SELECT
availqty INTO @UP_availqty
FROM
partsupp
WHERE
partkey = new.partkey
AND suppkey = new.suppkey;
IF (
@UP_availqty - (new.quantity - old.quantity) >= 0
) THEN
BEGIN
UPDATE partsupp
SET availqty = availqty - (new.quantity - old.quantity)
WHERE
partkey = new.partkey
AND suppkey = new.suppkey;
END;
END
IF;
END② stay Lineitem Define a table BEFORE INSERT trigger , When inserting order details , Check the supply list first PartSupp Available quantity in availqty Is it enough .
Source code :
CREATE TRIGGER BIN_lineitem BEFORE INSERT ON lineitem FOR EACH ROW
BEGIN
SELECT
availqty INTO @IN_availqty
FROM
partsupp
WHERE
partkey = new.partkey
AND suppkey = new.suppkey;
IF (
@IN_availqty - new.quantity >= 0
) THEN
BEGIN
UPDATE partsupp
SET availqty = availqty - (new.quantity)
WHERE
partkey = new.partkey
AND suppkey = new.suppkey;
END;
END
IF;
END③ stay Lineitem Define a table BEFORE DELETE trigger , When deleting order details , The ordered quantity of this order detail item shall be returned to the corresponding part supply record .
Source code :
CREATE TRIGGER BDE_lineitem BEFORE DELETE ON lineitem FOR EACH ROW
BEGIN
UPDATE partsupp
SET availqty = availqty + (old.quantity)
WHERE
partkey = old.partkey
AND suppkey = old.suppkey;
END④ Verify that the above three triggers work .
verification UPDATE Trigger source code :
SELECT
*
FROM
lineitem
WHERE
partkey = 24941;
SELECT
*
FROM
partsupp
WHERE
partkey = 24941;
UPDATE lineitem
SET quantity = 6
WHERE
orderkey = 6
AND partkey = 24941;
SELECT
*
FROM
lineitem
WHERE
partkey = 24941;
SELECT
*
FROM
partsupp
WHERE
partkey = 24941;verification UPDATE Screenshot of trigger result :
Before updating lineitem surface :
Before updating partsupp surface :
After the update lineitem surface :
After the update partsupp surface :
verification INSERT Trigger source code :
SELECT
*
FROM
lineitem
WHERE
partkey = 44117;
SELECT
*
FROM
partsupp
WHERE
partkey = 44117;
INSERT INTO lineitem
VALUES
(
5,
44117,
533,
1,
12,
6666.00,
0.69,
0.39,
'',
'',
NULL,
NULL,
NULL,
' ',
' ',
NULL
);
SELECT
*
FROM
lineitem
WHERE
partkey = 44117;
SELECT
*
FROM
partsupp
WHERE
partkey = 44117;verification INSERT Screenshot of trigger result :
Before insertion lineitem surface :
Before insertion partsupp surface
After inserting lineitem surface :
After inserting partsupp surface :
verification DELETE Trigger source code :
SELECT
*
FROM
lineitem
WHERE
partkey = 44117;
SELECT
*
FROM
partsupp
WHERE
partkey = 44117;
DELETE
FROM
lineitem
WHERE
partkey = 44117
AND orderkey = 9;
SELECT
*
FROM
lineitem
WHERE
partkey = 44117;
SELECT
*
FROM
partsupp
WHERE
partkey = 44117;verification DELETE Screenshot of trigger result :
Before deleting lineitem surface :
Before deleting partkey surface :
After deleting lineitem surface :
After deleting partkey surface :
版权声明
本文为[Hebuter-Rui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200618536741.html
边栏推荐
- airbase 初步分析
- Idea import a project
- 【SDU Chart Team - Core】SVG属性类设计之枚举
- 阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
- ros功能包内自定义消息引用失败
- Ubutnu20 installer centernet
- ubutnu20安裝CenterNet
- Pytorch: runtimeerror: an attempt has been made to start a new process Error reporting (resolved)
- [※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]
- 一文解决浏览器跨域问题
猜你喜欢

Is rust more suitable for less experienced programmers?

Problem brushing plan -- dynamic programming (III)

Ali has another "against the sky" container framework! This kubernetes advanced manual is too complete

JS merge duplicate data in array object

Online Excel to CSV tool
![[leetcode refers to the substructure of offer 26. Tree (medium)]](/img/53/b34ed5f46706f80bc1a9fcdb1481ae.png)
[leetcode refers to the substructure of offer 26. Tree (medium)]

Online timing flow chart making tool
![[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]](/img/ab/698810f6fe169adffc3bec5e0dc13f.png)
[leetcode sword finger offer 10 - II. Frog jumping steps (simple)]

Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose

Keywords static, extern + global and local variables
随机推荐
Normalized transforms in pytorch The real calculation process of normalize
IOT design and development
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
Preliminary analysis of Airbase
危机即机遇,远程办公效率为何会提升?
Two Stage Detection
Introduce structured concurrency and release swift 5.5!
The computer is out of power. How did I pass the terrible interview of Tencent cloud?
Crisis is opportunity. Why will the efficiency of telecommuting improve?
Echerts add pie chart random color
Sklearn function: Train_ Test_ Split (split training set and test set)
JS prototype and prototype chain
Idea import a project
在线Excel转CSV工具
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
使用mbean 自动执行heap dump
引入结构化并发,Swift 5.5 发布!
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
【SDU Chart Team - Core】SVG属性类设计之枚举
A series of problems of C DataGridView binding list