一、实验项目:
MySQL并发事务与锁机制。
二、实验目的
掌握事务的处理方法。
三、实验内容
1、创建存储过程P_usr_del,在存储过程中使用事务,实现同时删除给定客户(userid)在account表和orders表中的数据。要求userid作为该存储过程的输入参数,调用存储过程时输入参数为u0002。
delimiter $$
create procedure P_usr_del(in user_id char(6))
begin
declare exit handler for sqlexception rollback;
start transaction;
delete from account where userid=user_id;
delete from orders where userid=user_id;
commit;
end $$
delimiter ;
call P_usr_del('u0002');
2、创建存储过程P_ord_upd,在存储过程中使用事务,实现当向lineitem表中插入一行数据时,根据订单号对orders表的订单总价进行修改,订单总价加上该商品明细的金额。要求该存储过程有4个输入参数,分别为lineitem表新增记录的列值,调用存储过程时各参数的值分别为20130414,K9-KL-01,2,130。
delimiter $$
create procedure P_ord_upd(in o_orderid int(11),in o_itemid char(10),in o_quantity int(11),in o_unitprice decimal(10,2))
begin
declare a int;
declare exit handler for sqlexception rollback;
start transaction;
select count(*) into a from orders where orderid=o_orderid;
insert into lineitem values(o_orderid,o_itemid,o_quantity,o_unitprice);
update orders set totalprice=totalprice+o_quantity*o_unitprice where orderid=o_orderid;
if a>0 then
commit;
else
rollback;
end if;
end $$
delimiter ;
call P_ord_upd(20130414,'K9-KL-01',2,130);
3、创建存储过程P_pro_upd,在存储过程中使用事务,实现当修改给定商品(productid)的市价(listprice)时,同时修改lineitem表中对应商品的成交价格unitprice。要求productid和listprice作为该存储过程的输入参数,调用存储过程时两个参数的值分别为K9-BD-01,1700。
delimiter $$
create procedure P_pro_upd(in o_productid char(10),in o_listprice decimal(10,2))
begin
declare exit handler for sqlexception rollback;
start transaction;
update product set listprice=o_listprice where productid=o_productid;
update lineitem set unitprice=o_listprice where productid =o_productid;
commit;
end $$
delimiter ;
call P_pro_upd('K9-BD-01',1700);