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

 更新时间:2014年02月08日 15:27:01   作者:  
mysql触发器实现oracle物化视图即不是基于基表的虚表,而是根据表实际存在的实表,需要的朋友可以参考下

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中在查询结果集中得到记录行号的方法

    MySQL中在查询结果集中得到记录行号的方法

    这篇文章主要介绍了MySQL中在查询结果集中得到记录行号的方法,本文解决方法是通过预定义用户变量来实现,需要的朋友可以参考下
    2015-01-01
  • MySQL中出现lock wait timeout exceeded问题及解决

    MySQL中出现lock wait timeout exceeded问题及解决

    这篇文章主要介绍了MySQL中出现lock wait timeout exceeded问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • SQL ALTER TABLE语句灵活修改表结构和数据类型

    SQL ALTER TABLE语句灵活修改表结构和数据类型

    这篇文章主要介绍了SQL ALTER TABLE语句灵活修改表结构和数据类型,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • PHP之Mysql常用SQL语句示例的深入分析

    PHP之Mysql常用SQL语句示例的深入分析

    本篇文章是对Mysql常用SQL语句进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • MySQL联结表介绍以及使用详解

    MySQL联结表介绍以及使用详解

    这篇文章主要给大家介绍了关于MySQL联结表介绍及使用的相关资料,联结SQL最强大的功能之一就是能在数据检索查询的执行中联结表,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • mysql增删改查基础语句

    mysql增删改查基础语句

    这篇文章主要介绍了mysql增删改查基础语句,需要的朋友可以参考下
    2017-10-10
  • MySQL异常宕机无法启动的处理过程

    MySQL异常宕机无法启动的处理过程

    MySQL宕机是指MySQL数据库服务突然停止运行,通常可能是由于硬件故障、软件错误、资源耗尽、网络中断、配置问题或是恶意攻击等导致,当MySQL发生宕机时,系统可能无法提供数据访问,本文给大家介绍了MySQL异常宕机无法启动的处理过程,需要的朋友可以参考下
    2024-08-08
  • 一文教会你在MySQL中使用DateTime

    一文教会你在MySQL中使用DateTime

    mysql数据库在我们的工作中经常需要使用,经常在表中需要使用时间,下面这篇文章主要给大家介绍了关于在MySQL中使用DateTime的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • MySQL 分页查询的优化技巧

    MySQL 分页查询的优化技巧

    这篇文章主要介绍了MySQL 分页查询的优化技巧,帮助大家更好的理解和学习使用MySQL,感兴趣的朋友可以了解下
    2021-05-05
  • mysql获取分组后每组的最大值实例详解

    mysql获取分组后每组的最大值实例详解

    这篇文章主要介绍了 mysql获取分组后每组的最大值实例详解的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论