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

相关推荐
程序员夏洛39 分钟前
MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
数据库·mysql
Wang's Blog1 小时前
PostgreSQL笔记34:索引优化策略全景解析——从B-tree到HOT的核心原理与实践
数据库·笔记·postgresql
l1258652 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
梦Arrebol2 小时前
Mysql内容及相关实验
数据库·mysql
OceanWaves19932 小时前
mysql 8.0.32 磁盘爆满,清理从库日志
数据库·mysql
凤山老林2 小时前
数据库读写分离与动态路由实战:Spring Boot + ShardingSphere-JDBC 生产配置
数据库·spring boot·后端·分库分表·sharding-jdbc
l1258653 小时前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
聚美智数4 小时前
图片水印-图片剪裁-图片缩放API接口介绍
java·服务器·数据库
JavaPub-rodert5 小时前
Go 后台如何同时兼容 MySQL、PostgreSQL、SQLite 和 SQL Server?从 ShiyuAdmin 看 GORM 多数据库适配
数据库·mysql·postgresql·golang·javapub·王仕宇
瀚高PG实验室5 小时前
SQL优化案例:存储过程、复杂SQL拆成简单SQL提升整体性能
数据库·sql·postgresql·瀚高数据库