mysql触发器实现oracle物化视图示例代码
2023-06-13 09:15:17 时间
oracle数据库支持物化视图--不是基于基表的虚表,而是根据表实际存在的实表,即物化视图的数据存储在非易失的存储设备上。
下面实验创建ONCOMMIT的FAST刷新模式,在mysql中用触发器实现insert,update,delete刷新操作
1、基础表创建,Orders表为基表,Order_mv为物化视图表
mysql>createtableOrders(
->order_idintnotnullauto_increment,
->product_namevarchar(30)notnull,
->pricedecimal(10,0)notnull,
->amountsmallintnotnull,
->primarykey(order_id));
QueryOK,0rowsaffected
mysql>createtableOrder_mv(
->product_namevarchar(30)notnull,
->price_sumdecimal(8.2)notnull,
->amount_sumintnotnull,
->price_avgfloatnotnull,
->order_cntintnotnull,
->uniqueindex(product_name));
QueryOK,0rowsaffected
2、insert触发器
delimiter$$
createtriggertgr_Orders_insert
afterinsertonOrders
foreachrow
begin
set@old_price_sum=0;
set@old_amount_sum=0;
set@old_price_avg=0;
set@old_orders_cnt=0;
selectifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
fromOrder_mv
whereproduct_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;
replaceintoOrder_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter;
3、update触发器
delimiter$$
createtriggertgr_Orders_update
beforeupdateonOrders
foreachrow
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;
selectprice,amountfromOrderswhereorder_id=new.order_id
into@cur_price,@cur_amount;
selectifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
fromOrder_mv
whereproduct_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;
replaceintoOrder_mv
values(new.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
end;
$$
delimiter;
4、delete触发器
delimiter$$
createtriggertgr_Orders_delete
afterdeleteonOrders
foreachrow
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;
selectprice,amountfromOrderswhereorder_id=old.order_id
into@cur_price,@cur_amount;
selectifnull(price_sum,0),ifnull(amount_sum,0),ifnull(price_avg,0),ifnull(order_cnt,0)
fromOrder_mv
whereproduct_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>0then
set@new_price_avg=@new_price_sum/@new_orders_cnt;
replaceintoOrder_mv
values(old.product_name,@new_price_sum,@new_amount_sum,@new_price_avg,@new_orders_cnt);
else
deletefromOrder_mvwhereproduct_name=@old.name;
endif;
end;
$$
delimiter;
5、这里delete触发器有一个bug,就是在一种产品的最后一个订单被删除的时候,Order_mv表的更新不能实现,不知道这算不算是mysql的一个bug。当然,如果这个也可以直接用sql语句生成数据,而导致的直接后果就是执行效率低。
->insertintoOrder_mv
->selectproduct_name,sum(price),sum(amount),avg(price),count(*)fromOrders
->groupbyproduct_name;
相关文章
- 实现使用Java代码实现MySQL数据库连接(java连接mysql数据库代码)
- MySQL数据库恢复:一步一步的指南(mysql数据库恢复方法)
- 掌握C语言操作MySQL数据库技能(c操作mysql数据库)
- PHP与MySQL数据库之间的连接(php链接mysql)
- 调优提升性能:Oracle 数据库代码调优实践(oracle数据库代码)
- ORA12154解决Oracle ORA12154错误(oracle报错代码)
- Oracle 触发器类型:实现强大的自动化(oracle触发器类型)
- 合并Oracle数据库方案研究(oracle数据合并)
- 称世界最强数据库Oracle:世界第一的最强数据库(oracle号)
- MySQL数据库驱动代码深入解析(mysql数据库驱动代码)
- MySQL绝招:不可错过的万能密码(mysql万能密码)
- MySQL 数据监听:监测数据库操作(mysql数据监听)
- 探究MySQL代码块的优势和应用方法(mysql代码块)
- 快速掌握常用Mysql语句,让你的MySQL更强大(常用mysql语句)
- 在PHP中安装MySQL扩展:一步一步操作指引(php安装mysql扩展)
- Oracle数据迁移至MySQL:技术与实践(oracle迁移到mysql)
- MySQL使用代码快速创建数据库(mysql创建数据库代码)
- 使用MySQL注释符号来增加代码可读性(mysql注释符号)
- MySQL中如何添加记录一次快速实现(c mysql 添加记录)
- MySQL查询Oracle的全景观察(mysql查oracle)
- 如何使用C语言连接Oracle数据库(c 代码链接oracle)
- HTML5与Oracle结合,展现出更多可能(html5 oracle)
- 痴Dr Oracle真正的白痴(dr.oracle真之白)
- MySQL表结构设计之C语言编码实现(c 代码编写mysql表)
- Oracle 事务的决定性过程(oracle 事务 过程)
- GET MYSQL 免费下载并破解MySQL数据库软件(mysql下载和破解)
- Mysql 数据库丢失别慌来了解一下 MySQL 不见的可能原因及解决办法(mysql不见)
- 利用Oracle实现程序代码加密(oracle 代码加密)
- Oracle替换MySQL实现模糊查询(oracle代替模糊查询)
- Oracle数据库系统三种角色的功能介绍(oracle中的三种角色)
- Oracle中精准查询字段的方法(oracle 中查询字段)
- 查询Oracle中SQL语句查看显示报表(oracle中sql显示)
- Oracle禁止使用某个特殊字符(oracle不要某个字符)