Experiment 4: Trigger Experiment

Experiment 4: Trigger Experiment


Experiment purpose: To
master the design and use of database triggers, to
understand the functions and execution principles of different types of triggers

Experiment content:
1. After trigger
2. Before trigger
3. Delete trigger

Experimental process and requirements:
1. After trigger
(1) Define an Update trigger on LineItem, when modifying the order details (modifying the order details price extendedPrice, discount discount, tax rate tax) automatically modify the total order amount in the table Orders TotalPrice, to Maintain data consistency.
(2) Define a trigger on LineItem. When an order detail is deleted, the total order amount TotalPrice in the Orders table is automatically modified to maintain data consistency.
(3) Define a Delete trigger on LineItem. When an order detail is deleted, the total order amount TotalPrice in the table Orders is automatically modified to maintain data consistency.
(4) Verify the trigger function
2. Before trigger
(1) Define a Before Update trigger on the LineItem table. When modifying the quantity quantity in the order details, first check whether the available quantity availability in the supply table PartSupp is sufficient
(2 ) Define a Before Insert trigger on the LineItem table, when inserting an order detail, first check whether the available quantity in the supply table PartSupp is sufficient
(3) Define a Before Delete trigger on the LineItem table, when deleting an order detail When the number of parts corresponding to the details should be returned to the corresponding parts supply record
(4) Verify the trigger function

Experimental focus: definition of triggers,
experimental difficulties: using triggers to achieve more complex user-defined integrity

Experiment process
1.After trigger
(1) Define the Update trigger
(2) Define the Insert trigger
(3) Define the Delete trigger
(4) Verify the trigger function
2.Before trigger
(1) Define the Before Update trigger
(2) ) Define Before Insert trigger
(3) Define Before Delete trigger
(4) Verify trigger action
3. Delete trigger

reference:

#1after触发器#
#(1)total price=totalprice+extendedprice*(1-discount)*(1+tax)#
------------
CREATE TRIGGER `TRI_Lineitem_Price_update` AFTER UPDATE ON `lineitem` FOR EACH ROW update orders 
begin
set  totalprice=totalprice+(new.extendedprice*(1-new.discount)*(1+new.tax)
-old.extendedprice*(1-old.discount)*(1+old.tax))
where orderkey=new.orderkey; 
end;
--------------
//(2)
 create trigger `TRI_Lineitem_Price_insert` after insert on `lineitem` for each row update orders 
begin
set totalprice=totalprice+NEW.extendedprice*(1-NEW.discount)*(1+NEW.tax)
where orderkey=new.orderkey;
end;
//(3)
create trigger `TRI_Lineitem_Price_delete`
after delete on `lineitem`
for each row
begin
update orders set totalprice
=totalprice-OLD.extendedprice*(1-OLD.discount)*(1+OLD.tax)
where orderskey=orderskey;
end;

#验证触发器TRI_Lineitem_Price_update#
#查看1854号订单的含税折扣总价totalprice#
select totalprice from orders where orderskey =1854;
#激活触发器,修改1854号订单第一个明细项的税率,该税率增加0.5%#
update Lineitem set tax =tax+0.05where orderskey=1854 and linenumber=1;
#再次查看2号订单的含税折扣总价totalprice是否有变化#
select totalprice from orders where orderskey =1854;
2.before触发器
//(1)
create trigger TRI_Lineitem_Quantity_update
before update on Lineitem
for each row
begin
select availqty into @L_availqty from partsupp
where partkey=new.partkey and suppkey=new.suppkey;
if(@L_availqty-(new.partkey-old.quantity)>=0) then
begin
update partsupp set availqty=availqty-(new.quantity)
where partkey=new.partkey and suppkey=new.suppkey ;
end;
end if;
end;
// (2)
create trigger TRI_Lineitem_Quantity_insert
before insert on Lineitem
for each rowbegin
select availqty into @L_availqty from partsupp
where partkey=new.partkey and suppkey=new.suppkey;
if(@L_availqty-(new.partkey)>=0) then
begin
update partsupp set availqty=availqty-(new.quantity)
where partkey=new.partkey and suppkey=new.suppkey ;
end;
end if;
end;
//(3)
create trigger TRI_Lineitem_Quantity_delete
before
delete on Lineitem
for each row
begin
update partsupp set availqty=availqty-(old.quantity)
where partkey=old.partkey and suppkey=old.suppkey;
end;


//验证触发器TRL-Lineitem_Quantity _update#
#查看1854号订单第1个明细项的零件和供应商编号,订购数量,可用数量#
select L.partkey,L.suppkey,L.quantity,PS.availqty
from Lineitem L,PartSupp PS
where L.partkey=PS.partkey 
and L.suppkey=PS.suppkeyand L.orderskey=1854 
and L.Linenumber=1;
#激活触发器:修改1854号订单第1个明细项的订购数量#
update Lineitem set quantity=quantity+5
where orderskey=1854 and linenumber=1;
#再次查看1854订单第1个明细项的相关信息,以验证触发器是否起作用#
select L.partkey,L.suppkey,L.quantity,PS.availqty
from Lineitem L,PartSupp PS
where L.partkey=PS.partkey 
and L.suppkey=PS.suppkeyand L.orderskey=1854 
and L.Linenumber=1;
#删除触发器#
drop trigger TRI_Lineitem_Price_update; 

//验证触发器TRI_Lineitem_Price_update:触发器起作用了
select totalprice from orders where orderskey
//验证触发器TRL_lineitem_quantity_update;触发器起作用了

 insert into lineitem(partkey,suppkey,quantity) values(1,1,20);

select availqty from partsupp;

 delete from lineitem where linenumber=1;






Guess you like

Origin blog.csdn.net/weixin_46220576/article/details/124323382