MySQL触发器和存储过程

1、触发器

(1):建立触发器,订单表中增加订单数量后,商品表商品数量同步减少对应的商品订单出数量,并测试

sql 复制代码
mysql> create trigger orders_after_insert_trigger
    -> after insert on orders for each row
    -> update goods set num=new-new.onum
    -> where gid=new.gid;
Query OK, 0 rows affected (0.01 sec)
mysql>

购买80根铅笔并观察表的数量是否减少

sql 复制代码
mysql> insert into orders(gid,name,price,onum,otime) value('C0001','铅笔',1.20,80,now());
mysql> select * from goods;
gid  name     price  num
A0001  橡皮     2.50  100
B0001  小楷本   2.80  210
C0001  铅笔     1.20  120
D0001  计算器   28.00  20
4 rows in set (0.01 sec)
mysql> insert into orders (gid, name, price, onum, otime) value('C0001', '铅笔', 1. 20, 80, now());
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from goods;
gid  name     price  num
A0001  橡皮     2.50  100
B0001  小楷本   2.80  210
C0001  铅笔     1.20  40
D0001  计算器   28.00  20
4 rows in set (0.00 sec)

(2):建立触发器,实现功能:客户取消订单,恢复商品表对应商品的数量

sql 复制代码
mysql> create trigger orders_after_delete_trigger
    -> after delete on orders for each row
    -> update goods set num=num+old.onum
    -> where gid=old.gid;

取消订单

sql 复制代码
mysql> delete from orders where gid='C0001';

(3):建立触发器,实现功能:客户修改订单,商品表对应商品数量同步更新

sql 复制代码
mysql> create trigger orders_after_update_trigger
    -> after update on orders for each row
    -> update goods set num=num+(old,onum-new.onum)
    -> where gid=old.gid;
mysql> insert into orders(gid, name, price, onum, otime) value('A0001','橡皮', 2.5, 50, now());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from orders;
oid | gid | name | price | onum | otime
2 | A0001 | 橡皮 | 2.50 | 50 | 2024-07-26
1 row in set (0.00 sec)
mysql> select * from goods;
gid | name | price | num
A0001 | 橡皮 | 2.50 | 50
B0001 | 小楷本 | 2.80 | 210
C0001 | 铅笔 | 1.20 | 120
D0001 | 计算器 | 28.00 | 20
4 rows in set (0.00 sec)

2、存储过程

(1):使用mydb7 openlab库

sql 复制代码
mysql>  use mydb7_openlab;
Database changed

(2):创建提取emp_new表所有员工姓名和工资的存储过程s1

sql 复制代码
mysql> delimiter //
mysql> create procedure sl()
-> begin
-> select name, incoming from emp_new;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;

使用命令查看员工姓名和工资

sql 复制代码
mysql> call sl();
+-------+----------+
| name | incoming |
+-------+----------+
| 张三 | 4000 |
| 李四 | 3500 |
| 王五 | 2000 |
| 赵六 | 7500 |
| 荣七 | 8500 |
| 牛八 | 7300 |
+-------+----------+
6 rows in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)

(3):创建存储过程s2,实现输入员工姓名后返回员工的年龄

sql 复制代码
mysql> desc emp_new;
Field	Type	Null	Key	Default	Extra
---+
+
sid	int	YES		NULL
name	varchar(11)	YES		NULL
age	int	YES		NULL
worktime_start	date	YES		NULL
incoming	int	YES		NULL
dept2	int	YES		NULL
+
-----+
6 rows in set (0.01 sec)
sql 复制代码
mysql> delimiter //
mysql> create procedure s2(in in_name varchar(11), out out_age int)
-> begin
-> select age into out_age from emp_new where name=in_name;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
sql 复制代码
mysql> call s2('王五',@age);
mysql> select @age;
+------+
| @age |
+------+
| 24    |
+------+
1 row in set (0.01 sec)

(4):创建一个存储过程s3,有2个参数,传入部门号,返回该部门的平均工资

sql 复制代码
mysql> desc emp_new;
------+------+------+------+----------+-------
Field | Type | Null | Key | Default | Extra
------+------+------+------+----------+-------
sid | int | YES |  | NULL | 
name | varchar(11) | YES |  | NULL | 
age | int | YES |  | NULL | 
worktime_start | date | YES |  | NULL | 
incoming | int | YES |  | NULL | 
dept2 | int | YES |  | NULL | 
------+------+------+------+----------+-------
6 rows in set (0.00 sec)
mysql> delimiter //
mysql> create procedure s3(in in_dept int, out avg_sal float)
-> begin
-> select avg(incoming) into avg_sal from emp_new where dept2=in_dept;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter;

使用s3

sql 复制代码
mysql> call s3(102,@avg_sal);
mysql> select @avg_sal;
+-
-+
| @avg_sal |
----+
8000
1 row in set (0.00 sec) 
相关推荐
躺平的花卷几秒前
Python爬虫案例六:抓取某个地区某月份天气数据并保存到mysql数据库中
数据库·爬虫·python·mysql
Flying_Fish_roe14 分钟前
linux-安全管理-防火墙与网络安全
linux·数据库·oracle
A_cot25 分钟前
Redis 的三个并发问题及解决方案(面试题)
java·开发语言·数据库·redis·mybatis
2401_847056551 小时前
Altium Designer脚本工具定制
网络·数据库
神仙别闹1 小时前
基于Python+SQLite的课程管理系统
数据库·sqlite
掐指一算乀缺钱1 小时前
SpringBoot 数据库表结构文档生成
java·数据库·spring boot·后端·spring
少年负剑去2 小时前
django分发路由
数据库·django·sqlite
飞翔的佩奇2 小时前
xxl-job适配sqlite本地数据库及mysql数据库。可根据配置指定使用哪种数据库。
数据库·spring boot·mysql·sqlite·xxl-job·任务调度
吱吱鼠叔2 小时前
MATLAB数据文件读写:1.格式化读写文件
前端·数据库·matlab
小哇6662 小时前
spring-TransactionTemplate 编程式事务
数据库·spring