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

相关推荐
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab2 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence2 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神2 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据3 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡3 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧3 天前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon3 天前
SQL学习指南——视图
数据库·sql
活宝小娜3 天前
mysql详细安装教程
数据库·mysql·adb