MySQL触发器实现oracle物化视图示例代码
2022-11-12 09:34:10
内容摘要
这篇文章主要为大家详细介绍了MySQL触发器实现oracle物化视图示例代码,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
oracle数据库支持
文章正文
这篇文章主要为大家详细介绍了MySQL触发器实现oracle物化视图示例代码,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
oracle数据库支持物化视图--不是基于基表的虚表,而是根据表实际存在的实表,即物化视图的数据存储在非易失的存储设备上。下面实验创建ON COMMIT 的FAST刷新模式,在mysql中用触发器实现insert , update , delete 刷新操作1、基础表创建,Orders 表为基表,Order_mv为物化视图表
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <code> 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 </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <code> 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 ; </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <code> 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 ; </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <code> 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 ; </code> |
代码如下:
1 2 3 4 5 | <code> -> insert into Order_mv -> select product_name ,sum(price),sum(amount),avg(price), count (*) from Orders -> group by product_name; </code> |
注:关于MySQL触发器实现oracle物化视图示例代码的内容就先介绍到这里,更多相关文章的可以留意
代码注释