数据库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);

相关推荐
jarvisuni16 分钟前
GLM-5V-Turbo多模态测试,克隆Claude官网!
数据库
Full Stack Developme1 小时前
MySQL 触发器 存储过程 介绍
数据库·mysql
杨云龙UP1 小时前
MySQL慢查询日志暴涨导致磁盘告警:slow query log膨胀至397G的生产故障排查:清理、参数优化
linux·运维·服务器·数据库·mysql
Bat U1 小时前
MySQL数据库|视图+索引
数据库·mysql
SQVIoMPLe2 小时前
[拆解LangChain执行引擎]以Actor模型的视角来看Pregel
服务器·数据库·langchain
你都会上树?2 小时前
Ubuntu22 安装PostgreSQL
数据库·postgresql
想唱rap2 小时前
线程之条件变量和生产消费模型
java·服务器·开发语言·数据库·mysql·ubuntu
RInk7oBjo2 小时前
MySQL的编译安装
数据库·mysql·adb
java资料站2 小时前
MySQL 增量同步脚本
android·数据库·mysql
ningmengjing_2 小时前
从零推导出 Redis
数据库·redis