MySQL第五次作业

  1. 触发器

·建立两个表:goods(商品表)、orders(订单表)

mysql> use mydb16_trigger;

mysql> create table goods

gidchar(8)primary key,

varchar(10),name

pricedecimal(8,2),

intnum

#商品编号

#商品名

#价格

#数量

mysql> create table orders

)

oid

#订单号intprimary keyauto increment.char(10)not null

gid

#商品号

name

varchar(10),

#商品名

pricedecimal(8,2)

#价格

int ,

#订单数量

onum

otimedate

#订单时间

在商品表中导入商品记录

mysql>insert into goods values

('A0001','橡皮',2.5,100)

('B0001','小楷本',2.8,210),

('C0001',铅笔',1.2,120)

('D0001',计算器"28,20);

·建立触发器,订单表中增加订单数量后,商品表商品数量同步减少对应的商品订单出数量,并测试

create trigger orders_after_insert_trigger

-> after insert on orders for each row

-> update goods set num=num-new.onum

-> where gid = new.gid;

·建立触发器,实现功能:客户取消订单,恢复商品表对应商品的数量

create trigger orders_after_delete_trigger

-> after delete on orders for each row

-> update goods set num=num+old.onum

-> where gid = old.gid;

建立触发器,实现功能:客户修改订单,商品表对应商品数量同步更新

create trigger orders_after_update_trigger

-> after update on orders for each row

-> update goods set num=num+(old.onum - new.onum)

-> where gid = old.gid;

2.存储过程

使用mydb7 openlab库

创建提取emp_new表所有员工姓名和工资的存储过程

create procedure s1()

-> begin

-> select name,incoming from emp_new;

-> end //

创建存储过程s2,实现输入员工姓名后返回员工的年龄

create procedure s2(in in_name varchar(11),out out_age int)

-> begin

-> select age into out_age from emp_new where name = in_name;

-> end //

创建一个存储过程s3,有2个参数,传入部门号,返回该部门的平均工资

create procedure s3(in in_dept int,out avg_sal float)

-> begin

-> select avg(incoming) into avg_sal from emp_new where dept2=101;

-> end //

相关推荐
北顾笙9808 分钟前
MYSQL-day03
数据库·sql·mysql
MXsoft6189 分钟前
**混合云统一监控实践:私有云+公有云的一体化运维方案**
运维·网络·数据库
瀚高PG实验室31 分钟前
java中间件无法连接数据库
java·数据库·中间件·瀚高数据库
ULIi096kr33 分钟前
MySQL大表优化终极方案:单表数据量上限、卡顿解决、分表分库实战教程
数据库·mysql
霖霖总总34 分钟前
[MongoDB小技巧07]MongoDB 深度解析:find中投影与排序的底层机制与性能调优实战
数据库·mongodb
TechWayfarer36 分钟前
云服务器地域怎么选:用离线IP数据库识别用户来源并优化部署
服务器·数据库·python·tcp/ip·数据分析
deviant-ART41 分钟前
MySQL里的三个concat函数
数据库·mysql
H_老邪1 小时前
1044 - Access denied for user ‘root‘@‘%‘ to database ‘nacos‘
数据库·mysql
数智化精益手记局1 小时前
拆解复杂项目管理流程:用项目管理流程解决跨部门协作低效难题
大数据·运维·数据库·人工智能·产品运营
xhtdj1 小时前
Uber 如何通过批处理实现单账户每秒30+次更新
大数据·数据库·人工智能·安全·动态规划