什么时候会用到存储过程

1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度
2.当对数据库进行复杂操作时(如对多个表进行 Update,Insert,Query,Delete 时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。这些操作,如果用程序来完成,就变成了一条条的 SQL 语句,可能要多次连接数据库。而换成存储,只需要连接一次数据库就可以了

间接:人调用应用程序 应用程序调用过程
直接:通过sqlplus或其它数据库管理工具直接调用过程

heidisql 中创建存储过程与函数

This chapter discusses stored programs and views, which are database objects defined in terms of SQL code that is stored on the server for later execution.

Stored programs include these objects:

    Stored routines, that is, stored procedures and functions. A stored procedure is invoked using the CALL statement. A procedure does not have a return value but can modify its parameters for later inspection by the caller. It can also generate result sets to be returned to the client program. A stored function is used much like a built-in function. you invoke it in an expression and it returns a value during expression evaluation.

    Triggers. A trigger is a named database object that is associated with a table and that is activated when a particular event occurs for the table, such as an insert or update.

    Events. An event is a task that the server runs according to schedule. 

Views are stored queries that when referenced produce a result set. A view acts as a virtual table.

This chapter describes how to use stored programs and views. The following sections provide additional information about SQL syntax for statements related to these objects:

    For each object type, there are CREATE, ALTER, and DROP statements that control which objects exist and how they are defined. See Section 14.1, “Data Definition Statements”.

    The CALL statement is used to invoke stored procedures. See Section 14.2.1, “CALL Syntax”.

    Stored program definitions include a body that may use compound statements, loops, conditionals, and declared variables. See Section 14.6, “MySQL Compound-Statement Syntax”. 

In MySQL 5.7, metadata changes to objects referred to by stored programs are detected and cause automatic reparsing of the affected statements when the program is next executed. For more information, see Section 9.10.4, “Caching of Prepared Statements and Stored Programs”.
stored programs
Stored routines
stored procedures
stored functions
Triggers
Events
views
声明方式有两种:
第一种:声明是否是确定性的
DETERMINISTIC和NOT DETERMINISTIC指出一个子程序是否对给定的输入总是产生同样的结果。 如果没有给定任一特征,默认是NOT DETERMINISTIC,所以必须明确指定DETERMINISTIC来声明一个子程序是确定性的。
这里要说明的是:使用NOW() 函数(或它的同义)或者RAND() 函数不会使一个子程序变成非确定性的。对NOW()而言,二进制日志包括时间戳并会被正确的执行。RAND()只要在一个子程序内被调用一次也可以被正确的复制。所以,可以认为时间戳和随机数种子是子程序的确定性输入,它们在主服务器和从服务器上是一样的。 第二种:声明是否会改变数据  
CONTAINS SQL, NO SQL, READS SQL DATA, MODIFIES SQL用来指出子程序是读还是写数据的。
无论NO SQL还是READS SQL DATA都指出,子程序没有改变数据,但是必须明确地指定其中一个,因为如果任何指定,默认的指定是CONTAINS SQL。 默认情况下,如果允许CREATE PROCEDURE 或CREATE FUNCTION 语句被接受,就必须明确地指定DETERMINISTIC 或 NO SQL与READS SQL DATA 中的一个,否则就会产生1418错误。 其中,sp_name参数表示存储过程或函数的名称;
characteristic参数指定存储函数的特性。 CONTAINS SQL表示子程序包含SQL语句,但不包含读或写数据的语句;
NO SQL表示子程序中不包含SQL语句;
READS SQL DATA表示子程序中包含读数据的语句;
MODIFIES SQL DATA表示子程序中包含写数据的语句。 SQL SECURITY { DEFINER | INVOKER }指明谁有权限来执行。
DEFINER表示只有定义者自己才能够执行;
INVOKER表示调用者可以执行。 COMMENT 'string'是注释信息。 说明:修改存储过程使用ALTER PROCEDURE语句,修改存储函数使用ALTER FUNCTION语句。
但是,这两个语句的结构是一样的,语句中的所有参赛都是一样的。
而且,它们与创建存储过程或函数的语句中的参数也是基本一样的。 修改存储过程和函数,只能修改他们的权限,目前MYSQL还不提供对已存在的存储过程和函数代码的修改
如果要修改,只能通过先DROP掉,然后重新建立新的存储过程和函数来实现
CREATE FUNCTION `myfun`(`in_string` VARCHAR(255), `in_find_str` VARCHAR(20), `in_repl_str` VARCHAR(20))
RETURNS VARCHAR(255)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'ssssssssssssss'
BEGIN
declare l_new_string varchar(255);
declare l_find_pos int;
set l_find_pos=instr(in_string,in_find_str);
if (l_find_pos>0) then
set l_new_string=insert(in_string,l_find_pos,LENGTH(in_find_str),in_repl_str);
else
set l_new_string=in_string;
end if;
return (l_new_string);
END SELECT `myfun`('corresponds to your server', 'on', 'qqqqqqqqqqqqqqq')
CREATE DEFINER=`root`@`%` PROCEDURE `listdata`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'test_proc'
BEGIN
select * from alert where type=14;
END call listdata或者下面
CALL `listdata`()
触发器

use spauth;
CREATE TABLE `pre_ucenter_members` (
`uid` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`username` char(128) NOT NULL DEFAULT '',
`password` char(32) NOT NULL DEFAULT '',
`email` char(32) NOT NULL DEFAULT '',
`myid` char(30) NOT NULL DEFAULT '',
`myidkey` char(16) NOT NULL DEFAULT '',
`regip` char(15) NOT NULL DEFAULT '',
`regdate` int(10) unsigned NOT NULL DEFAULT '0',
`lastloginip` int(10) NOT NULL DEFAULT '0',
`lastlogintime` int(10) unsigned NOT NULL DEFAULT '0',
`salt` char(6) NOT NULL,
`secques` char(8) NOT NULL DEFAULT '',
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`),
KEY `email` (`email`)
) ENGINE=FEDERATED DEFAULT CHARSET=utf8 CONNECTION='mysql://root:123456@192.168.1.234:3306/ultrax/pre_ucenter_members';
insert into pre_ucenter_members(username,password,email,salt) select username,md5(CONCAT(PASSWORD,'63b1a2')),username,'63b1a2' from account; create trigger account_bbs
after update on account
for each row
begin
update pre_ucenter_members set password = md5(CONCAT(new.PASSWORD,'63b1a2')) where username = new.username;
end; create trigger account_bbs_insert
after insert on account
for each row
begin
insert into pre_ucenter_members(username,password,email,salt) values(new.username,md5(CONCAT(new.PASSWORD,'63b1a2')),new.username,'63b1a2');
end; 我们如何在触发器引用行的值,也就是说我们要得到我们新插入的订单记录中的gid或much的值。对于insert而言,新插入的行用new来表示,行中的每一列的值用new.列名来表示。 触发器new-old
http://www.cnblogs.com/zzwlovegfj/archive/2012/07/04/2576989.html
new-old的理解
http://www.chenyudong.com/archives/database-trigger-new-old-value-understand.html
1.实验创建触发器
2.实验创建事件(计划) 触发器
某表任意一行有变动就更新另一张表中的某字段
CREATE DEFINER=`root`@`%` TRIGGER `ff` AFTER UPDATE ON `expert_history_operation` FOR EACH ROW BEGIN
update en_project set proj_code = 'uuuuu' where xpmobs_sid = '552';
END navicat执行正常,heidisql执行报错,修改为下面的就可以正常执行,二个客户端语法有所差异,折腾了好久 CREATE PROCEDURE update_invitation_status_proce()
begin
update invitation a set a.status='4' where a.deadlinetime<NOW() and a.status='1';
UPDATE `order_authorization` a SET a.submitStatus = '3' WHERE a.authorizedDeadline < NOW() AND a.submitStatus != '4';
end ; 修改为下面的就可以正常执行 DELIMITER //
CREATE DEFINER=`root`@`%` PROCEDURE `update_invitation_status_proce`()
BEGIN
update invitation a set a.status='4' where a.deadlinetime<NOW() and a.status='1';
UPDATE `order_authorization` a SET a.submitStatus = '3' WHERE a.authorizedDeadline < NOW() AND a.submitStatus != '4';
END//
DELIMITER ; 事件event,执行时间为服务器上的时间点, 前提是show variables like "event_scheduler" 这个值为on,也就是开启状态,事件才能生效
mysql> show variables like "event_scheduler";
mysql> set global event_scheduler = 1;
也可在/etc/my.cnf中在mysqld下面加入一行event_scheduler = 1,但需要重启服务器。所以一般流行的做法是双管齐下,
1.set global event_scheduler = 1;
2.vi /etc/my.cnf加入这一行。
这样的话,不重启也生效,重启后也生效。 可以通过日志查看计划执行效果
[root@my mysql]# tail /var/log/mysqld.log  -f
2016-05-27T22:06:57.225728Z 7 [Note]  - 220.250.64.26
2016-05-30T17:54:54.536231Z 1 [Note] Event Scheduler: Last execution of smartplant.de. Dropping.
2016-05-30T17:54:54.559635Z 9 [Note] Event Scheduler: Dropping smartplant.de
2016-05-30T18:05:18.733466Z 10 [Note] Access denied for user 'root'@'localhost' (using password: YES)
2016-05-30T18:06:45.174233Z 11 [Note] Event Scheduler: Killing the scheduler thread, thread id 1
2016-05-30T18:06:45.174265Z 11 [Note] Event Scheduler: Waiting for the scheduler thread to reply
2016-05-30T18:06:45.174347Z 11 [Note] Event Scheduler: Stopped
2016-05-30T18:16:22.337700Z 12 [Note] Event Scheduler: scheduler thread started with id 12
2016-05-30T18:16:22.337721Z 12 [Note] Event Scheduler: Last execution of smartplant.ww. Dropping.
2016-05-30T18:16:22.345537Z 13 [Note] Event Scheduler: Dropping smartplant.ww
2016-05-30T18:23:25.336330Z 12 [Note] Event Scheduler: Last execution of smartplant.we. CREATE EVENT `we`
 ON SCHEDULE
  AT '2016-05-30 14:23:25'
 ON COMPLETION PRESERVE
 ENABLE
 COMMENT ''
 DO BEGIN
insert into t1 (tid,name) values(7,'g'),(8,'h');
END

mysql存储程序的更多相关文章

  1. 实战mysql存储程序与定时器

    home198979 实战mysql存储程序与定时器 博客分类: mysql 存储过程定时器eventprocedure实战  需求:一个庞大的日志表,现每天做定时统计一天的总数,放另一个表中,方便查 ...

  2. 数据库之mysql存储程序

    什么时候会用到存储过程 1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度2.当对数据库进行复杂操作时 ...

  3. Mysql 存储程序

    #1存储过程create procedure greeting() BEGIN # 77 = 16 FOR username + 60 for hostname + 1 for '@' DECLARE ...

  4. 《MySQL 存储过程编程》-读书笔记

    本书结构: 第一部分:存储编程基础 第1章:存储过程程序基础 第2章:MySQL存储编程指南 第3章:语言基础 第4章:语句块 第5章:在存储程序中使用SQL 第一章:MySQL存储程序介绍 存储程序 ...

  5. mysql存储过程详细讲解及完整实例下载

    一.存储过程概念 1.存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集.经编译后存储在数据库 中. 2.存储过程是数据库中的一个重要对象,用户通过指定存储过程的名字并给 ...

  6. MySQL高级查询与编程笔记 • 【第4章 MySQL编程】

    全部章节   >>>> 本章目录 4.1 用户自定义变量 4.1.1 用户会话变量 4.1.2 用户会话变量赋值 4.1.3 重置命令结束标记 4.1.4 实践练习 4.2 存 ...

  7. 存储程序(2)——MYSQL

    1.触发器 触发器是与特定数据表相关联的存储过程,当相应的数据表被INSERT.DELETE或UPDATE语句修改时,触发器将自动执行.触发器可以被设置成在这几种语句处理每个数据行之前或之后触发.触发 ...

  8. 存储程序(1)——MYSQL

    MySQL支持把几种对象存放在服务器端供以后使用.这几种对象有一些可以根据情况通过程序代码调用,有一些会在数据表被修改时自动执行,还有一些可以在预定时刻自动执行.它们包括以下几种: 1.存储函数(st ...

  9. 阅读MySQL文档第20章:存储程序和函数

    本文把阅读到的重点摘抄下来. 一.一个子程序要么是一个程序要么是一个函数.使用CALL语句来调用程序,程序只能用输出变量传回值.就像别其它函数调用一样,函数可以被从语句外调用(即通过引用函数名),函数 ...

随机推荐

  1. 数位DP详解

    算法使用范围 在一个区间里面求有多少个满足题目所给的约束条件的数,约束条件必须与数自身的属性有关 下面用kuangbin数位dp的题来介绍 例题  不要62 题意:在一个区间里面求出有多少个不含4和6 ...

  2. Codeforces Round #506 (Div. 3) D. Concatenated Multiples

    D. Concatenated Multiples You are given an array aa, consisting of nn positive integers. Let's call ...

  3. [rancher-net]

    ip rule命令 rancher网络全解读 arp命令查询 rancher managed network 实践 docker自定义网桥 iptables增删改查 shell脚本调试技术

  4. redis_key键

    exists k1,判断k1是否存在,‘1’就是存在,‘0’ 就是不存在 move k3 2   --->这里就是说把k3移动到2号库. ‘1’为成功,‘0’为失败 ttl k2  --查看k2 ...

  5. oracle 正则表达式 匹配

    oracle 正则表达式 在实际应用中,想排除带有中文的字段值: select h.froomnumber from t_broker_house h where REGEXP_LIKE(froomn ...

  6. css3宽高设置:calc() / vw / vh

    对于720px的设计稿,100vw == 720px,1vw == 7.2px; vw可以替代rem 实现自适应布局. 相应的计算插件:postcss-px-to-viewport ******** ...

  7. Day13作业及默写

    1. 整理今天的博客,写课上代码,整理流程图. 博客链接--博客园 2. 写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用 ...

  8. mysql 数据库复制方法

    同一台MySQL服务器上复制数据库的方法 CREATE DATABASE `新库` DEFAULT CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI; mysqld ...

  9. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  10. BZOJ 1083 [SCOI2005]繁忙的都市 (最小生成树裸题无重边) 超简单写法!!

    Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...