MYSQL第五次作业

1、触发器

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

mysql> use mydb16_trigger;
Database changed
mysql>  create table goods
    -> (
    ->    gid char(8) primary key,
    ->    name varchar(10),
    ->    price decimal(8,2),
    ->    num int
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql>  create table orders
    -> (
    ->   oid  int  primary key auto_increment,
    ->   gid  char(10) not null,
    ->   name varchar(10),
    ->   price decimal(8,2),
    ->   onum  int,
    ->   otime  date
    -> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc goods;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| gid   | char(8)      | NO   | PRI | NULL    |       |
| name  | varchar(10)  | YES  |     | NULL    |       |
| price | decimal(8,2) | YES  |     | NULL    |       |
| num   | int          | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> desc orders;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| oid   | int          | NO   | PRI | NULL    | auto_increment |
| gid   | char(10)     | NO   |     | NULL    |                |
| name  | varchar(10)  | YES  |     | NULL    |                |
| price | decimal(8,2) | YES  |     | NULL    |                |
| onum  | int          | YES  |     | NULL    |                |
| otime | date         | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

在商品表中导入商品记录

mysql> insert into goods values
    ->  ('A0001','橡皮',2.5,100),
    ->  ('B0001','小楷本',2.8,210),
    -> ('C0001' ,'铅笔',1.2,120),
    -> ('D0001','计算器', 28,20);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |  100 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |  120 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

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

mysql> create trigger orders_after_insert_trigger
    -> after insert on orders for each row
    -> update goods set num=num-new.onum
    -> where gid=new.gid;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into orders(gid,name,price,onum,otime) value('C0001', '铅笔', 1.2,80,now());
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |  100 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |   40 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from orders;
+-----+-------+------+-------+------+------------+
| oid | gid   | name | price | onum | otime      |
+-----+-------+------+-------+------+------------+
|   1 | C0001 | 铅笔 |  1.20 |   80 | 2024-07-25 |
+-----+-------+------+-------+------+------------+
1 row in set (0.00 sec)

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

mysql> create trigger orders_after_delete_trigger
    ->  after delete on orders for each row
    ->  update goods set num=num+old.onum
    -> where gid=old.gid;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from orders;
+-----+-------+------+-------+------+------------+
| oid | gid   | name | price | onum | otime      |
+-----+-------+------+-------+------+------------+
|   1 | C0001 | 铅笔 |  1.20 |   80 | 2024-07-25 |
+-----+-------+------+-------+------+------------+
1 row in set (0.00 sec)

mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |  100 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |   40 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)
mysql> delete from orders where gid='C0001';
Query OK, 1 row affected (0.01 sec)

mysql> select * from orders;
Empty set (0.00 sec)

mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |  100 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |  120 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

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

mysql> 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;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |  100 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |  120 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from orders;
Empty set (0.00 sec)
mysql> insert into orders(gid,name,price,onum,otime) value('A0001','橡皮',2.5,20,now());
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |   80 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |  120 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

mysql> select * from orders;
+-----+-------+------+-------+------+------------+
| oid | gid   | name | price | onum | otime      |
+-----+-------+------+-------+------+------------+
|   2 | A0001 | 橡皮 |  2.50 |   20 | 2024-07-25 |
+-----+-------+------+-------+------+------------+
1 row in set (0.00 sec)

mysql> update orders set onum=35 where gid='A0001';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from orders;
+-----+-------+------+-------+------+------------+
| oid | gid   | name | price | onum | otime      |
+-----+-------+------+-------+------+------------+
|   2 | A0001 | 橡皮 |  2.50 |   35 | 2024-07-25 |
+-----+-------+------+-------+------+------------+
1 row in set (0.00 sec)

mysql> select * from goods;
+-------+--------+-------+------+
| gid   | name   | price | num  |
+-------+--------+-------+------+
| A0001 | 橡皮   |  2.50 |   65 |
| B0001 | 小楷本 |  2.80 |  210 |
| C0001 | 铅笔   |  1.20 |  120 |
| D0001 | 计算器 | 28.00 |   20 |
+-------+--------+-------+------+
4 rows in set (0.00 sec)

2、存储过程

使用mydb7 openlab库

mysql> use mydb7_openlab
Database changed

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

mysql> delimiter $$
mysql> create procedure s1()
    -> begin
    ->     select name,incoming from emp_new;
    -> end $$
Query OK, 0 rows affected (0.02 sec)

mysql> delimiter ;
mysql> call s1();
+------+----------+
| name | incoming |
+------+----------+
| 张三 |     4000 |
| 李四 |     3500 |
| 王五 |     2000 |
| 赵六 |     7500 |
| 荣七 |     8500 |
| 牛八 |     7300 |
+------+----------+
6 rows in set (0.01 sec)

Query OK, 0 rows affected (0.03 sec)

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

mysql> delimiter $$
mysql> 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 $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> call s2('赵六',@age);
Query OK, 1 row affected (0.00 sec)
mysql> select @age;
+------+
| @age |
+------+
|   57 |
+------+
1 row in set (0.00 sec)

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

mysql>  delimiter $$
mysql>  create procedure s3(in in_dept int, out avg_sal float)
    -> begin
    ->     select avg(incoming) into avg_sal from emp_new
    ->     where dept2=in_dept group by dept2;
    -> end $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> call s3(101,@avg_sal);
Query OK, 1 row affected (0.00 sec)
mysql> select @avg_sal;
+-------------------+
| @avg_sal          |
+-------------------+
| 3166.666748046875 |
+-------------------+
1 row in set (0.00 sec)
相关推荐
尘浮生9 分钟前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
六月闻君23 分钟前
MySQL 报错:1137 - Can‘t reopen table
数据库·mysql
SelectDB技术团队31 分钟前
兼顾高性能与低成本,浅析 Apache Doris 异步物化视图原理及典型场景
大数据·数据库·数据仓库·数据分析·doris
inventecsh1 小时前
mongodb基础操作
数据库·mongodb
白云如幻1 小时前
SQL99版链接查询语法
数据库·sql·mysql
爱吃烤鸡翅的酸菜鱼1 小时前
MySQL初学之旅(4)表的设计
数据库·sql·mysql·database
计算机毕设指导62 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
The_Ticker2 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
Elastic 中国社区官方博客2 小时前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
企鹅侠客2 小时前
ETCD调优
数据库·etcd