# 视图的创建
CREATE VIEW productcustomers AS
SELECT cust_name,cust_contact,prod_id
FROM customers,orders,orderitems
WHERE customers.cust_id=orders.cust_id AND orders.order_num=orderitems.order_num
with check option; -- 对视图所做的DML操作的结果,不能违反视图的WHERE条件的限制。这里没啥用
# 使用视图和表类似
SELECT * FROM productcustomers
WHERE prod_id='TNT2';
-- 存储过程
-- name:ordertotal
-- parameters: ordernum=order_num
-- taxable = 0 or 1
-- ototal = order total varibale
DELIMITER $$
drop procedure if exists ordertotal$$
create procedure ordertotal(
in ordernum int,
in taxable boolean,
out ototal decimal(8,2)
)comment 'get total variable with tax'
begin
-- declare varibale for total
declare total decimal(8,2);
-- declare tax precent
declare tax_prec int default 6;
-- get total variable without tax
select sum(quantity*item_price)
from orderitems
where order_num=ordernum
into total;
-- is taxable
if taxable then
select total+(total/100*tax_prec) into total;
end if;
-- return total with tax
select total into ototal;
end$$
DELIMITER ;
call ordertotal(20005,1,@total);
select @total;
-- 游标
-- 将total with tax单独放入一个表中
delimiter $$
drop procedure if exists prosessorders$$
create procedure prosessorders()
begin
-- 声明一个boolen变量,用于游标终止循环
declare done boolean default 0;
-- 声明一个变量存储total
declare total decimal(8,2);
-- 声明一个变量从游标每次取值
declare ordernum int;
-- 声明个游标
declare ordernums cursor
for
select order_num from orders;
-- declare continue handler
-- declare continue handler for sqlstate '02000' set done=1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-- 创建一个表,保存totals
drop table if exists ordertotals;
create table if not exists ordertotals
(order_num int,total decimal(8,2));
-- 打开游标
open ordernums;
-- loop through all the row
repeat
fetch ordernums into ordernum;
if !done then
-- 根据order_num,从orderitems中获取totals
call ordertotal(ordernum,1,total);
-- 把计算出来的结果,插入表中
insert into ordertotals(order_num,total)
values(ordernum,total);
end if;
until done end repeat;
-- 关闭游标
close ordernums;
end$$
delimiter ;
call prosessorders();
select * from ordertotals;-- order by order_num;