MySQL学习(十六)
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学习(十六)的更多相关文章
- mysql进阶(十六)常见问题汇总
mysql进阶(十六)常见问题汇总 MySQL视图学习: http://www.itokit.com/2011/0908/67848.html 执行删除操作时,出现如下错误提示: 出现以上问题的原因是 ...
- 为什么不让用join?《死磕MySQL系列 十六》
大家好,我是咔咔 不期速成,日拱一卒 在平时开发工作中join的使用频率是非常高的,很多SQL优化博文也让把子查询改为join从而提升性能,但部分公司的DBA又不让用,那么使用join到底有什么问题呢 ...
- 强化学习(十六) 深度确定性策略梯度(DDPG)
在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...
- MYSQL数据库学习十六 安全性机制
16.1 MYSQL数据库所提供的权限 16.1.1 系统表 mysql.user 1. 用户字段 Host:主机名: User:用户名: Password:密码. 2. 权限字段 以“_priv”字 ...
- Scala学习十六——XML处理
一.本章要点 XML字面量<like>this</like>的类型为NodeSeq 可以在XML字面量中内嵌Scala代码 Node的child属性产出后代节点 Node的at ...
- MySQL学习(六)change-buffer
文章部分总结描述来自参考文章,属于半原创. 概述 文章将会介绍 change buffer 相关的知识点 查看 MySQL InnoDB 状态的命令 SHOW ENGINE INNODB ST ...
- MySQL学习笔记(六):索引
本文主要介绍MySQL 中关于索引的一些问题,例如:索引的作用:怎么创建索引:设计索引的原则:怎么优化索引等等. 一:索引概述 索引一般是通过排序,然后查找时可以二分查找,这一特点来达到加速查找的目的 ...
- MySQL(十六)之MySQL用户管理
一.MySQL用户管理概述 MySQL是一个多用户的数据库,MYSQL的用户可以分为两大类: 超级管理员用户(root),拥有全部权限 普通用户,由root创建,普通用户只拥有root所分配的权限 二 ...
- MySQL学习(十五)
索引的概念 索引是数据的目录,能快速定位数据的位置.索引提高了查询速度,降低了增删改的速度.并非加的越多越好. 一般在查询频率高的列上加,而且在重复度低的列上加效果更好.如在性别列上不用加索引,但是身 ...
随机推荐
- 20165310 NetSec2019 Week6 Exp4 恶意代码分析
20165310 NetSec2019 Week6 Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间 ...
- 单用户模式破解root密码
- Python3基础 list pop 取出列表的最后一个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux内核发生Oops时怎么办?
1. 定位发生Oops的代码 1.1 通过addr2line命令定位 aarch64-openwrt-linux-gnu-addr2line -e vmlinux ffff000008087f00 1 ...
- IDEA查看一个类的所有继承关系
通常一个.java文件对应一个java类. 鼠标右击一个类: 即可查看.按住alt键可放大. 另一快捷键:光标在类名上,ctrl+H
- (zhuan) Attention in Neural Networks and How to Use It
Adam Kosiorek About Attention in Neural Networks and How to Use It this blog comes from: http://akos ...
- Ubuntu18.04安装Android Studio
一.安装JDK JDK下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.htm ...
- Unity3D学习笔记(三十二):Xlua(2)
Xlua支持通过子类对象访问父类的变量属性和方法 对于C#的ref,out参数的方法 当调用的时候:out类型的参数是不需要传递实参的,普通的参数和ref参数需要传递实参. out,ref传出值通 ...
- MPI 环境搭建问题-运行程序闪退
安装后smpd无法运行,进程中没有smpd.exe.注册过程也完成了.运行自带的测试程序cpi.exe,提示:Error: No smpd passphrase specified through t ...
- HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...