mysql触发器实现oracle物化视图示例代码

所属分类: 数据库 / Mysql 阅读数: 1380
收藏 0 赞 0 分享

oracle数据库支持物化视图--不是基于基表的虚表,而是根据表实际存在的实表,即物化视图的数据存储在非易失的存储设备上。
下面实验创建ON COMMIT 的FAST刷新模式,在mysql中用触发器实现insert , update , delete 刷新操作
1、基础表创建,Orders 表为基表,Order_mv为物化视图表

复制代码 代码如下:

mysql> create table Orders(
-> order_id int not null auto_increment,
-> product_name varchar(30)not null,
-> price decimal(10,0) not null ,
-> amount smallint not null ,
-> primary key (order_id));
Query OK, 0 rows affected
mysql> create table Order_mv(
-> product_name varchar(30) not null,
-> price_sum decimal(8.2) not null,
-> amount_sum int not null,
-> price_avg float not null,
-> order_cnt int not null,
-> unique index(product_name));
Query OK, 0 rows affected

2、insert触发器
复制代码 代码如下:

delimiter $$
create trigger tgr_Orders_insert
after insert on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=new.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum+new.price;
set @new_amount_sum=@old_amount_sum+new.amount;
set @new_orders_cnt=@old_orders_cnt+1;
set @new_price_avg=@new_price_sum/@new_orders_cnt;

replace into Order_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter ;

3、update触发器
复制代码 代码如下:

delimiter $$
create trigger tgr_Orders_update
before update on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

set @cur_price=0;
set @cur_amount=0;

select price,amount from Orders where order_id=new.order_id
into @cur_price,@cur_amount;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=new.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum-@cur_price+new.price;
set @new_amount_sum=@old_amount_sum-@cur_amount+new.amount;
set @new_orders_cnt=@old_orders_cnt;
set @new_price_avg=@new_price_sum/@new_orders_cnt;

replace into Order_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter ;

4、delete触发器
复制代码 代码如下:

delimiter $$
create trigger tgr_Orders_delete
after delete on Orders
for each row
begin
set @old_price_sum=0;
set @old_amount_sum=0;
set @old_price_avg=0;
set @old_orders_cnt=0;

set @cur_price=0;
set @cur_amount=0;

select price,amount from Orders where order_id=old.order_id
into @cur_price,@cur_amount;

select ifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
from Order_mv
where product_name=old.product_name
into @old_price_sum,@old_amount_sum,@old_price_avg,@old_orders_cnt;

set @new_price_sum=@old_price_sum - old.price;
set @new_amount_sum=@old_amount_sum - old.amount;
set @new_orders_cnt=@old_orders_cnt - 1;

if @new_orders_cnt>0 then
set @new_price_avg=@new_price_sum/@new_orders_cnt;
replace into Order_mv
values(old.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
else
delete from Order_mv where product_name=@old.name;
end if;
end;
$$
delimiter ;

5、这里delete触发器有一个bug,就是在一种产品的最后一个订单被删除的时候,Order_mv表的更新不能实现,不知道这算不算是mysql的一个bug。当然,如果这个也可以直接用sql语句生成数据,而导致的直接后果就是执行效率低。
复制代码 代码如下:

-> insert into Order_mv
-> select product_name ,sum(price),sum(amount),avg(price),count(*) from Orders
-> group by product_name;

更多精彩内容其他人还在看

MySQL中distinct和count(*)的使用方法比较

这篇文章主要针对MySQL中distinct和count(*)的使用方法比较,对两者之间的使用方法、效率进行了详细分析,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Mysql命令大全(完整版)

这篇文章主要介绍了Mysql命令大全,分享的命令都是最基本的,推荐给大家,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Mysql常用命令汇总

这篇文章主要介绍了Mysql常用命令,都是mysql数据库日常最基本的操作命令,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

MySQL最基本的命令使用汇总

这篇文章为大家分享了MySQL最基本的命令使用汇总,MySQL最基本的命令使用,包括如何正确连接MySQL(和PHP搭配之最佳组合),修改密码与增加新用户等相关内容的描述,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

mysql命令行如何操作

这篇文章主要介绍了mysql命令行如何操作,还为大家分享了mysql添加环境变量的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

MySQL常用命令 MySQL处理数据库和表的命令

这篇文章主要介绍了MySQL常用命令,尤其是针对MySQL处理数据库和表的命令进行学习,特别适用于新手,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Mysql基础入门 轻松学习Mysql命令

这篇文章主要是Mysql基础入门教程,教大家如何轻松学习Mysql命令,并熟练掌握Mysql命令,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

解决Mysql服务器启动时报错问题的方法

这篇文章主要介绍了解决Mysql服务器启动时报错问题的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

MySql命令实例汇总

这篇文章主要介绍了MySql命令,结合实例分析了MySQL数据库的创建、连接及增删改查等各种常用操作的使用方法与相关注意事项,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux下实现MySQL数据备份和恢复的命令使用全攻略

这篇文章主要介绍了Linux下实现MySQL数据备份和恢复的命令使用全攻略,包括使用Mysqldump和LVM快照以及xtrabackup三种方法,倾力推荐!需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多