场景一,数据表自动备份(多个数据表字段同步等),使用触发器。如updatelog记录对资源的所有操作日志,reslastlog记录资源最后操作的日志信息。同步方式实现如下:

//创建表
DROP TABLE IF EXISTS updatelog;
CREATE TABLE `updatelog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`resourceid` int(11) DEFAULT NULL,
`log` text,
`createtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 //必须指定主键或unique,不然无法replace
DROP TABLE IF EXISTS reslastlog;
CREATE TABLE `reslastlog` (
`resourceid` int(11) NOT NULL DEFAULT '0',
`log` text,
`updatetime` datetime DEFAULT NULL,
PRIMARY KEY (`resourceid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 //创建触发器
DROP TRIGGER IF EXISTS t_afterinsert_on_updatelog;
delimiter //
CREATE TRIGGER t_afterinsert_on_updatelog
AFTER INSERT ON updatelog
FOR EACH ROW
BEGIN
replace into reslastlog(resourceid,log,updatetime) values(new.resourceid, new.log, new.createtime);
END;
//
delimiter ; DROP TRIGGER IF EXISTS t_afterdelete_on_updatelog;
delimiter //
CREATE TRIGGER t_afterdelete_on_updatelog
AFTER DELETE ON updatelog
FOR EACH ROW
BEGIN
delete from reslastlog where resourceid=old.resourceid;
END;
//
delimiter ; //测试
insert into updatelog(resourceid, log, createtime) values(1, "version 1-0",now());
insert into updatelog(resourceid, log, createtime) values(1, "version 1-1",now());
insert into updatelog(resourceid, log, createtime) values(2, "version 2-2",now());
delete from updatelog where resourceid = 2; //触发器相关操作
mysql> show triggers;
+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
| t_afterinsert_on_updatelog | INSERT | updatelog | BEGIN
replace into reslastlog(resourceid,log,updatetime) values(new.resourceid, new.log, new.createtime);
END | AFTER | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
| t_afterdelete_on_updatelog | DELETE | updatelog | BEGIN
delete from reslastlog where resouceid=old.resourceid;
END | AFTER | NULL | | root@localhost | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+----------------------------+--------+-----------+--------------------------------------------------------------------------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
2 rows in set (0.00 sec)
drop trigger t_afterinsert_on_updatelog;

场景二,用户定义函数或者存储过程实现简单的后台数据运算。示例如下:

//用户定义函数

//创建资源基本信息表
CREATE TABLE `baseinfo` (
`id` int(11) DEFAULT NULL,
`content` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1; insert into baseinfo values(1,"one");
insert into baseinfo values(2,"two");
insert into baseinfo values(3,"three"); //创建每日资源pv表
CREATE TABLE `dayinfo` (
`id` int(11) DEFAULT NULL,
`pv` int(11) DEFAULT NULL,
`day` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1; insert into dayinfo values(1,10,"2014-10-01");
insert into dayinfo values(1,12,"2014-10-02");
insert into dayinfo values(1,16,"2014-10-03");
insert into dayinfo values(2, 30, "2014-10-02"); 查询资源指定时段降序排列
mysql> select baseinfo.id as id, content, sum(pv) as totalpv from baseinfo,dayinfo where baseinfo.id=dayinfo.id and day>="2014-10-02" and day<="2014-10-03" group by id order by totalpv desc;
+------+---------+---------+
| id | content | totalpv |
+------+---------+---------+
| 2 | two | 30 |
| 1 | one | 28 |
+------+---------+---------+ 上面的sql语法非常复杂,如果用UDF会方便简洁很多。 1,查看用户定义函数功能是否开启,ON为开启
show variables like '%func%'; 2,如果是OFF,则执行下面的操作
set global log_bin_trust_function_creators=1; 3,创建用户定义函数
delimiter $$
CREATE FUNCTION getTotalPV(targetid int,dayfrom date,dayto date) RETURNS int
begin
declare totalpv int default 0;
set totalpv=(select sum(pv) from dayinfo where id = targetid and day>=dayfrom and day<=dayto);
if totalpv is null then
set totalpv = 0;
end if;
return totalpv;
end$$
delimiter ; mysql> select id, content, getTotalPV(id, "2014-10-02", "2014-10-03") as totalpv from baseinfo order by totalpv desc;
+------+---------+---------+
| id | content | totalpv |
+------+---------+---------+
| 2 | two | 30 |
| 1 | one | 28 |
| 3 | three | 0 |
+------+---------+---------+ 4,查看udf定义show create function getTotalPV; //存储过程
drop procedure if exists getjson;
delimiter $$
create procedure getjson
(
str1 varchar(1024),
str2 varchar(1024),
str3 varchar(1024),
str4 varchar(1024)
)
begin
if str1 is NULL then
set str1="";
end if;
if str2 is NULL then
set str2="";
end if;
if str3 is NULL then
set str3="";
end if;
if str4 is NULL then
set str4="";
end if;
select CONCAT("[",str1,",",str2,",",str3,",",str4,"]") as jsonstr;
end;$$
delimiter ; mysql> call getjson("a","b","c","d");
+-----------+
| jsonstr |
+-----------+
| [a,b,c,d] |
+-----------+
1 row in set (0.00 sec)

场景三:mysql调用外部应用程序(如表有数据更新后,通过触发器调用外部应用程序执行任务)

1.lib_mysqludf_sys简介
mysql中没有执行外部命令的函数,要调用外部的命令,可以通过开发MySQL UDF来实现,lib_mysqludf_sys 就是一个实现了此功能的UDF库。
下载地址:https://github.com/mysqludf/lib_mysqludf_sys 2.使用方法
2.1 安装部署(需要安装mysql-devel)
a) lib_mysqludf_sys.so复制到mysql/lib/plugin目录下。 b) 在mysql中创建函数(根据需要选取): Drop FUNCTION IF EXISTS lib_mysqludf_sys_info;
Drop FUNCTION IF EXISTS sys_get;
Drop FUNCTION IF EXISTS sys_set;
Drop FUNCTION IF EXISTS sys_exec;
Drop FUNCTION IF EXISTS sys_eval; Create FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.so';
Create FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.so';
Create FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.so';
Create FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.so';
Create FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.so';
2.2 使用此函数
例:在select语句调用mkdir命令 Select sys_exec('mkdir -p /home/user1/aaa')
例:在触发器中调用外部的脚本(脚本需要可执行权限) Create TRIGGER trig_test AFTER Insert ON <table1>
FOR EACH ROW
BEGIN
DECLARE ret INT;
Select sys_exec('/home/user1/test.sh') INTO ret;
END

mysql三个应用场景的更多相关文章

  1. MySQL(三)

    MYSQL(三) 上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: MYSQL入门全套(第一部) MYSQL入门全套(第二部) 索引 ...

  2. 确保数据零丢失!阿里云数据库RDS for MySQL 三节点企业版正式商用

    2019年10月23号,阿里云数据库RDS for MySQL 三节点企业版正式商用,RDS for MySQL三节点企业版基于Paxos协议实现数据库复制,每个事务日志确保至少同步两个节点,实现任意 ...

  3. MySQL使用索引的场景及真正利用索引的SQL类型

    1. 为什么使用索引 在无索引的情况下,MySQL会扫描整张表来查找符合sql条件的记录,其时间开销与表中数据量呈正相关.对关系型数据表中的某些字段建索引可以极大提高查询速度(当然,不同字段是否sel ...

  4. mysql(三) 数据表的基本操作操作

    mysql(三) 数据表的基本操作操作 创建表,曾删改查,主键,外键,基本数据类型. 1. 创建表 create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGIN ...

  5. MySQL三种存储引擎总结

    MySQL三种存储引擎 MyISAM.InnoDB.MEMORY 1.MyISAM MyISAM,3.23.34a前的默认存储引擎. 优缺点 优点 在于占用空间小,处理速度快. 缺点 不支持事务的完整 ...

  6. mysql三种带事务批量插入

    原文:mysql三种带事务批量插入 c#之mysql三种带事务批量插入 前言 对于像我这样的业务程序员开发一些表单内容是家常便饭的事情,说道表单 我们都避免不了多行内容的提交,多行内容保存,自然要用到 ...

  7. Mysql(三)-2:数据类型

    一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...

  8. mysql三-2:数据类型

    一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...

  9. mysql(三) 慢查询分析(二)

    在一般的查询中,都要求尽量围绕创建的索引进行.针对索引,常用的有主键索引,单列索引,组合索引,索引合并等. 在评价索引时,关键看区分度.索引区分度=索引列唯一值/表记录数. 如果在区分度很低的列上建索 ...

随机推荐

  1. 关于Python网络爬虫实战笔记①

    python网络爬虫项目实战笔记①如何下载韩寒的博客文章 python网络爬虫项目实战笔记①如何下载韩寒的博客文章 1. 打开韩寒博客列表页面 http://blog.sina.com.cn/s/ar ...

  2. ue中替换行

    把替换的字符替换为^p 如:123,12,3,1, 在UE力把“,”替换未“^p”,就会替换为 1231231

  3. git创建分支与合并分支

    git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...

  4. 几年前无聊小游戏之作_WEB版本打泡泡

    几年前写的小东西 主要是H5画布的操作,还有个C语言基于WIN SDK开发的版本 找不到代码了 找到了再分享 <!DOCTYPE html> <script src="ga ...

  5. [LeetCode]题解(python):144-Binary Tree Preorder Traversal

    题目来源: https://leetcode.com/problems/binary-tree-preorder-traversal/ 题意分析: 前序遍历一棵树,递归的方法很简单.那么非递归的方法呢 ...

  6. WL(Wear leveling)磨损平衡

    前面说过,闪存寿命是以P/E次数来计算的,而WL就是确保闪存内每个块被写入的次数相等的一种机制.若没有这个机制,SSD内的闪存颗粒就无法在同一时间内挂掉,那对用户来说就是灾难.       会出现这种 ...

  7. firefox的window.onerror没有详细的出错提示

    当在firefox浏览器的a.htm页面中使用script标签加载某a.js出错时,如果放置window.onerror事件处理方法时,此方法获取到的错误信息都是固定的: {0:"Scrip ...

  8. 多名Uber司机被指刷单遭封号 一周薪水为0

    昨天,一司机在Uber“司机之家”办公地墙上写了泄愤的话 摄/法制晚报记者 苏妮 司机展示的账单显示,上周的薪水几乎为零,上面用英文标注了“欺诈行为”的字样 摄/法制晚报记者 苏妮 法制晚报讯(记者 ...

  9. android程序启动画面之Splash总结[转]

    方法一: 很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐.实现这种效果的方法有两种(暂时只发现两种)1.使用两个Activity,程序启动时候load第一张Activity,然后由tick ...

  10. python 命令行参数,以及文件操作

    #demo.py #!/usr/bin/python import sys print sys.argv #python demo.py 11 22 33 44 55 ['demo.py', '11' ...