数据库5(MySQL版)

作业要求

触发器

mysql> create trigger after_order_insert

-> after insert on orders

-> for each row

-> update goods set num = num - new.onum where gid = new.gid;

mysql> create trigger after_order_delete

-> after delete on orders

-> for each row

-> update goods set num = num + old.onum where gid = old.gid;

mysql> create trigger after_order_update

-> after update on orders

-> for each row

-> begin

-> if old.onum <> new.onum then

-> update goods set num = num - new.onum + old.onum where gid = new.gid;

-> end if;

-> end$$

-- 插入订单

insert into orders (gid, name, price, onum, otime) values ('a0001', '橡皮', 2.5, 5, curdate());

-- 检查商品表中橡皮的数量

select num from goods where gid = 'a0001';

-- 删除订单

delete from orders where oid = 1;

-- 检查商品表中橡皮的数量

select num from goods where gid = 'a0001';

-- 更新订单 update orders set onum = 10 where oid = 1;

-- 检查商品表中橡皮的数量

select num from goods where gid = 'a0001';

存储过程

mysql> create procedure s1()

-> select name, incoming from emp_new;

mysql> create procedure s2(in emp_name varchar(50))

-> select age from emp_new where name = emp_name;

mysql> create procedure s3(in dept_id int)

-> select avg(incoming) as avg_incoming from emp_new where sid = dept_id;

mysql> call s1();

mysql> call s2('张三');

mysql> call s3(101);

相关推荐
苍煜13 分钟前
慢SQL优化实战教学
java·数据库·sql
zhaoyong2221 小时前
MySQL 存储过程中字符集与排序规则不匹配导致查询性能下降的解决方案
jvm·数据库·python
sinat_383437361 小时前
golang如何从Python转型Go开发_golang从Python转型Go开发攻略
jvm·数据库·python
远洪1 小时前
claude code 国内安装使用
数据库·mysql
雨辰AI1 小时前
SpringBoot3 + 人大金仓 V9 微服务监控实战|Prometheus+Grafana+SkyWalking 全链路监控
数据库·后端·微服务·grafana·prometheus·skywalking
二哈赛车手1 小时前
新人笔记---ES和kibana启动问题以及一些常用的linux的错误排查方法,以及ES,数据库泄密解决方案[超详细]
java·linux·数据库·spring boot·笔记·elasticsearch
myrh pdmd1 小时前
maven导入spring框架
数据库·spring·maven
爬山算法2 小时前
MongoDB(118)如何在升级过程中进行数据备份?
数据库·mongodb·oracle
千月落2 小时前
Redis数据迁移
数据库·redis·缓存
wangbing11253 小时前
MySQL 官方 GPG 密钥过期问题
数据库·mysql