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. DGUT_FLY退役贴 && FunCfans毕业总结-竞赛篇

    严格来说我们飞跃队是去年ECFinal之后就退役的,只是这几个月有一堆事情在那,考研的考研,求职的求职,都把博客晾一边了.现在,总算能写点东西了. 我与ACM-ICPC的结缘,是从大一开学1个多月后开 ...

  2. 树莓派安装cobbler,自动化安装CentOS

    安装python.相关python模块.apache sudo apt-get install python python2.7 python-django python-netaddr python ...

  3. centos/rhel 7 几个最重要变化(systemd,firewalld,networkmanager,文件系统)

    详细参考:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administra ...

  4. Kali系列之multi/handler(渗透win7)

    环境 靶机 192.168.137.133 kali 192.168.137.135 步骤+ 生成后门 msfvenom -p windows/meterpreter/reverse_tcp LHOS ...

  5. ubuntu18.04 安装新版本openssl

    首先我们应该知道ubuntu18.04内置了1.1.0g版本的openssl: 使用下面的apt命令更新Ubuntu存储库并安装软件包编译的软件包依赖项: sudo apt update sudo a ...

  6. tf.nn.max_pool

    tf.nn.max_pool(value, ksize, strides, padding, name=None)   参数是四个,和卷积很类似: Args Annotation 第一个参数value ...

  7. topcoder srm 687 div1

    1.$A_{1}=2,A_{2}=3,A_{n}=A_{n-2}+A_{n-1}-1$.给出数字$n$,将其表示成若干个$A$中的不同元素的和. 思路:设$B_{n}=A_{n}-1$,那么有$B_{ ...

  8. Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value

    Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...

  9. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

  10. 【Spring Security】五、自定义过滤器

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...