MySQL高级部分

触发器

触发器是一类特殊的事务,可以监视某种数据操作(insert/update/delete),并触发相关的操作(insert/update/delete)

触发器创建语法之4要素

1 监视地点table

2 监视事件insert/update/delete

3 触发时间after/before

4 触发事件

查看已有的触发器

show triggers

删除已有的触发器

drop trigger triggerName

创建一个触发器

create trigger t1
after
insert on ord
for each row
begin
update goods1 set num = num - 2 where gid = 1;
end$

上面语句中,如果insert(被监视的语句),产生的数据,能否在触发器中引用到?

更改之后的触发器,引入了参数

create trigger t2
after
insert on ord
for each row
begin
update goods1 set num = num -new.much where gid = new.gid;
end$

删除订单的触发器

create trigger t3
after
delete
on ord
for each row
begin
update goods1 set num = num + old.much where gid = old.gid;
end$

改订单数量

create trigger t4
before
update
on ord
for each row
begin
update goods1 set num = num + (old.much - new.much) where gid = old.gid;
end$

思考:before目前似乎没有看出与after的区别?

再思考:如果剩余的库存量只有3个,但客户要买10个,就会出现问题

一个问题

能否在购买量 much > 库存量num时,把much自动改为num

提示:before的作用

在t2的基础上,完成much与num的判断

create trigger t5
after
insert on ord
for each row
begin
declare
rnum int;
select num into rnum from goods1 where gid = new.gid;
if new.much > rnum then
set new.much = rnum;
end if
update goods1 set num = num -new.much where gid = new.gid;
end$

提示错误

ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger

原因:insert之后,new行已经插入到表中,成为事实,改new已经晚了

修改

在t2的基础上,完成much与num的判断

create trigger t5
before
insert on ord
for each row
begin
declare
rnum int;
select num into rnum from goods1 where gid = new.gid;
if new.much > rnum then
set new.much = rnum;
end if;
update goods1 set num = num -new.much where gid = new.gid;
end$

insert 只能引用新行 new

delete 只能引用旧行 old

update可以引用新行和旧行,新行为new,旧行为old

触发器for each row是干吗的?

在oracle的触发器中,触发器可以分为语句级触发器和行级触发器

执行

update xxtable set xxx=xxx where id > 100; #修改了100行

那么sqlN会被触发几次?

100次

create trigger t5
before
insert on ord
for each row #每一行受影响,触发器都执行,叫做行级触发器
begin
declare
rnum int;
select num into rnum from goods1 where gid = new.gid;
if new.much > rnum then
set new.much = rnum;
end if;
update goods1 set num = num -new.much where gid = new.gid;
end$
在orcal中
for each row如果不写,无论update语句一次影响了多少行,都只执行一次。
比如:1人下了订单,买了5件商品,insert 5 次,可以用行级触发器,修改5次库存,
用语句级触发,insert一条发货提醒。
遗憾的是,MySQL目前不支持语句级触发器。
for each row 声明行级触发器

存储过程和函数

创建存储过程的语法

create procedure procedureName()

begin

--sql语句

end$

调用存储过程

call procedure();

删除存储过程

drop procedure procedureName

创建存储过程语法

create procedure p1()
begin
select 'hello' from dual;
end$

存储过程是可以编程的,意味着可以使用变量,表达式,控制结构来完成复杂的功能

在存储过程中,用declare声明变量

格式 declare 变量名 变量类型 [default 默认值]

引入变量

create procedure p2()
begin
declare age int default 18;
declare height int default 180;
select concat('年龄是',age,'身高是',height);
end$

存储过程中,变量可以做sql语句中合法的运算,如加减乘除,

注意,运算的结果,如何赋值给变量

set 变量名 := expression

create procedure p3()
begin
declare age int default 18;
declare height int default 180;
set age := age + 20;
select concat('年龄是',age,'身高是',height);
end$

if/else控制结构

if condition then
statement
else
end;

存储过程的括号里可以声明参数

语法是 [in/out/inout] 参数名 参数类型

给存储过程传递参数

create procedure p6(width int,height int)
begin
declare area int default 0;
if width > height then
select '这个矩形比较宽';
elseif width < height then
select '这个矩形比较高';
else
select '这个矩形是个方的';
end if;
set area := width * height;
select area;
end$

控制结构有三大类,顺序,选择,循环。

while的语法

WHILE expr_condition DO
statement_list
END WHILE
create procedure p7()
begin
declare total int default 0;
declare num int default 0;
while num < 101 do
set total := total + num;
set num := num + 1;
end while;
select total;
end$

in类型的参数,in代表input

create procedure p8(in n int)
begin
declare total int default 0;
declare num int default 0;
while num < n do
set num := num + 1;
set total := total + num;
end while;
select total;
end$

out型

create procedure p9(in n int,out total int)
begin
declare num int default 0;
set total := 0;
while num < n do
set num := num + 1;
set total := total + num;
end while;
end$

调用

call p9(100,@sum)$
select @sum$

inout类型

CASE,LOOP,IF,LEAVE,ITERATE,REPEAT,WHILE语句

MySQL学习(十六)的更多相关文章

  1. mysql进阶(十六)常见问题汇总

    mysql进阶(十六)常见问题汇总 MySQL视图学习: http://www.itokit.com/2011/0908/67848.html 执行删除操作时,出现如下错误提示: 出现以上问题的原因是 ...

  2. 为什么不让用join?《死磕MySQL系列 十六》

    大家好,我是咔咔 不期速成,日拱一卒 在平时开发工作中join的使用频率是非常高的,很多SQL优化博文也让把子查询改为join从而提升性能,但部分公司的DBA又不让用,那么使用join到底有什么问题呢 ...

  3. 强化学习(十六) 深度确定性策略梯度(DDPG)

    在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...

  4. MYSQL数据库学习十六 安全性机制

    16.1 MYSQL数据库所提供的权限 16.1.1 系统表 mysql.user 1. 用户字段 Host:主机名: User:用户名: Password:密码. 2. 权限字段 以“_priv”字 ...

  5. Scala学习十六——XML处理

    一.本章要点 XML字面量<like>this</like>的类型为NodeSeq 可以在XML字面量中内嵌Scala代码 Node的child属性产出后代节点 Node的at ...

  6. MySQL学习(六)change-buffer

    文章部分总结描述来自参考文章,属于半原创. 概述     文章将会介绍 change buffer 相关的知识点 查看 MySQL InnoDB 状态的命令 SHOW ENGINE INNODB ST ...

  7. MySQL学习笔记(六):索引

    本文主要介绍MySQL 中关于索引的一些问题,例如:索引的作用:怎么创建索引:设计索引的原则:怎么优化索引等等. 一:索引概述 索引一般是通过排序,然后查找时可以二分查找,这一特点来达到加速查找的目的 ...

  8. MySQL(十六)之MySQL用户管理

    一.MySQL用户管理概述 MySQL是一个多用户的数据库,MYSQL的用户可以分为两大类: 超级管理员用户(root),拥有全部权限 普通用户,由root创建,普通用户只拥有root所分配的权限 二 ...

  9. MySQL学习(十五)

    索引的概念 索引是数据的目录,能快速定位数据的位置.索引提高了查询速度,降低了增删改的速度.并非加的越多越好. 一般在查询频率高的列上加,而且在重复度低的列上加效果更好.如在性别列上不用加索引,但是身 ...

随机推荐

  1. 基于 SSL 的 Nginx 反向代理

    基于 SSL 的 Nginx 反向代理 描述: 线上zabbix因机房网络问题,外网接口无法对外访问,因此采用同机房的另外一台服务器做反向代理. 线上用于zabbix提供web访问的Nginx,采用h ...

  2. javaEE体系结构【转载】

    转载自: http://blog.csdn.net/chjskarl/article/details/72629014?locationNum=3&fps=1 JavaEE是一套使用Java进 ...

  3. 05文件合并脚本--By Mark Lutz

    ''' 合并split.py创建的目录下的所有组分文件以重建文件. 依赖文件名的排序:长度必须一致. ''' import os,sys readsize=1024 def join(fromdir, ...

  4. SOAP和WebService的那些事

    当初对这段历史有过一点研究,不过当初写得关于这部分历史的论文不知道被我丢哪儿去了,下面我用通俗一点的语言来话说一下这段历史吧,因为当初详细到具体人物具体时间的已经记不清了,所以这里写得不够专业,大家就 ...

  5. (转)Shiro学习

    (二期)13.权限框架shiro讲解 [课程13]自定义Realm.xmind36.8KB [课程13]用户授权流程.xmind0.2MB [课程13]shiro简介.xmind0.3MB [课程13 ...

  6. 题解——UVA11997 K Smallest Sums

    题面 背景 输入 输出 翻译(渣自翻) 给定K个包含K个数字的表,要求将其能产生的\( k^{k} \)个值中最小的K个输出出来 题解 k路归并问题的经典问题 可以转化为二路归并问题求解 考虑A[], ...

  7. 论文笔记:Person Re-identification with Deep Similarity-Guided Graph Neural Network

    Person Re-identification with Deep Similarity-Guided Graph Neural Network 2018-07-27 17:41:45 Paper: ...

  8. 论文笔记:Tracking by Natural Language Specification

    Tracking by Natural Language Specification 2018-04-27 15:16:13  Paper: http://openaccess.thecvf.com/ ...

  9. 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。

    public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...

  10. ExceptionLogger

    应用1:webconfig.cs中设置 public static class WebApiConfig { public static void Register(HttpConfiguration ...