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

相关推荐
dl8106727311 小时前
Redis的IO多路复用
数据库·redis·缓存
小王子玫瑰狐2 小时前
数据库之MySQL
数据库·mysql
Arbori_262155 小时前
oracle DECODE 函数
数据库·oracle
songx_995 小时前
数据库10(代码相关语句)
数据库·sql
沸材6 小时前
MySQL——事务
数据库·mysql·事务
雯0609~6 小时前
thinkphp:部署完整项目到本地phpstudy
数据库·oracle
文牧之8 小时前
MySQL 修改用户密码
运维·数据库·mysql
sxlzs_9 小时前
oracle怎么查看是否走了索引
数据库·oracle
余大冠子9 小时前
oracle判断同表同条件查出两条数据,根据长短判断差异
数据库·oracle
King.6249 小时前
从 SQL2API 到 Text2API:开启数据应用开发的新征程
大数据·开发语言·数据库·sql·低代码