一、作业内容
一、触发器
建立表
sql
mysql> use mydb16_trigger;
mysql> create table goods
-> (
-> gid char(8) primary key,
-> name varchar(10),
-> price decimal(8,2),
-> num int
-> );
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
-> );
插入数据
sql
mysql> insert into goods values
-> ('A0001','橡皮',2.5,100),
-> ('B0001','小楷本',2.8,210),
-> ('C0001','铅笔',1.2,120),
-> ('D0001','计算计',28,20);
1、建立触发器,订单表中增加订单数量后,商品表商品数量同步减少对应的商品订单出数量,并测试
命令
sql
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;
结果
插入数据前
sql
mysql> select * from goods;
+-------+--------+-------+------+
| gid | name | price | num |
+-------+--------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 210 |
| C0001 | 铅笔 | 1.20 | 120 |
| D0001 | 计算计 | 28.00 | 20 |
+-------+--------+-------+------+
mysql> select * from orders;
Empty set (0.01 sec)
插入数据后
sql
mysql> insert into orders(gid,name,price,onum,otime) value('B0001sql','小楷本',2.8,100,now());
mysql> select * from goods;
+-------+--------+-------+------+
| gid | name | price | num |
+-------+--------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 110 |
| C0001 | 铅笔 | 1.20 | 120 |
| D0001 | 计算计 | 28.00 | 20 |
+-------+--------+-------+------+
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 1 | B0001 | 小楷本 | 2.80 | 100 | 2024-07-25 |
+-----+-------+--------+-------+------+------------+
2、建立触发器,实现功能:客户取消订单,恢复商品表对应商品的数量
命令
sql
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;
结果
取消前
sql
mysql> select * from goods;
+-------+--------+-------+------+
| gid | name | price | num |
+-------+--------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 110 |
| C0001 | 铅笔 | 1.20 | 120 |
| D0001 | 计算计 | 28.00 | 20 |
+-------+--------+-------+------+
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 1 | B0001 | 小楷本 | 2.80 | 100 | 2024-07-25 |
+-----+-------+--------+-------+------+------------+
取消后
sql
mysql> delete from orders where orders.oid=1;
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)
3、建立触发器,实现功能:客户修改订单,商品表对应商品数量同步更新
命令
sql
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;
结果
修改前
sql
mysql> select * from goods;
+-------+--------+-------+------+
| gid | name | price | num |
+-------+--------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 110 |
| C0001 | 铅笔 | 1.20 | 120 |
| D0001 | 计算计 | 28.00 | 20 |
+-------+--------+-------+------+
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 1 | B0001 | 小楷本 | 2.80 | 100 | 2024-07-25 |
+-----+-------+--------+-------+------+------------+
修改后
sql
mysql> update orders set onum=120 where gid='B0001';
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 2 | B0001 | 小楷本 | 2.80 | 120 | 2024-07-25 |
+-----+-------+--------+-------+------+------------+
mysql> select * from goods;
+-------+--------+-------+------+
| gid | name | price | num |
+-------+--------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 90 |
| C0001 | 铅笔 | 1.20 | 120 |
| D0001 | 计算计 | 28.00 | 20 |
+-------+--------+-------+------+
二、存储过程
1、使用mydb7 openlab库
sql
mysql> use mydb7_openlab;
2、创建提取emp_new表所有员工姓名和工资的存储过程s1
命令
sql
mysql> delimiter //
mysql> create procedure s1()
-> begin
-> select name,incoming from emp_new;
-> end //
结果
sql
mysql> delimiter ;
mysql> call s1();
+------+----------+
| name | incoming |
+------+----------+
| 张三 | 4000 |
| 李四 | 3500 |
| 王五 | 2000 |
| 赵六 | 7500 |
| 荣七 | 8500 |
| 牛八 | 7300 |
+------+----------+
3、创建存储过程s2,实现输入员工姓名后返回员工的年龄
命令
sql
ysql> 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 //
结果
sql
mysql> call s2('张三',@age);
Query OK, 1 row affected (0.00 sec)
mysql> select @age;
+------+
| @age |
+------+
| 35 |
+------+
4、创建一个存储过程s3,有2个参数,传入部门号,返回该部门的平均工资
命令
sql
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;
-> end //
结果
sql
mysql> call s3(102,@avg_sal);
Query OK, 1 row affected (0.00 sec)
mysql> select @avg_sal;
+----------+
| @avg_sal |
+----------+
| 8000 |
+----------+