按照题目要求完成下列题目:
1.触发器
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.01 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.00 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.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.00 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_insert_after_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.00 sec)
mysql> insert into orders values(oid,'A0001','橡皮',2.5,20,now());
Query OK, 1 row affected, 1 warning (0.00 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> create trigger orders_delete_after_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> delete from orders where oid=1;
Query OK, 1 row affected (0.00 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_update_after_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.00 sec)
mysql> insert into orders values(oid,'C0001','铅笔',1.20,30,now());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from goods;
+-------+-----------+-------+------+
| gid | name | price | num |
+-------+-----------+-------+------+
| A0001 | 橡皮 | 2.50 | 100 |
| B0001 | 小楷本 | 2.80 | 210 |
| C0001 | 铅笔 | 1.20 | 90 |
| D0001 | 计算器 | 28.00 | 20 |
+-------+-----------+-------+------+
4 rows in set (0.00 sec)
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 3 | C0001 | 铅笔 | 1.20 | 30 | 2024-07-25 |
+-----+-------+--------+-------+------+------------+
1 row in set (0.00 sec)
mysql> update orders set onum=50 where oid=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from orders;
+-----+-------+--------+-------+------+------------+
| oid | gid | name | price | onum | otime |
+-----+-------+--------+-------+------+------------+
| 3 | C0001 | 铅笔 | 1.20 | 50 | 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 | 70 |
| D0001 | 计算器 | 28.00 | 20 |
+-------+-----------+-------+------+
4 rows in set (0.00 sec)
2.存储过程
mysql> use mydb7_openlab
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_mydb7_openlab |
+-------------------------+
| dept |
| emp |
| emp_new |
| user |
+-------------------------+
4 rows in set (0.00 sec)
mysql> select * from emp_new;
+------+--------+------+----------------+----------+-------+
| sid | name | age | worktime_start | incoming | dept2 |
+------+--------+------+----------------+----------+-------+
| 1789 | 张三 | 35 | 1980-01-01 | 4000 | 101 |
| 1674 | 李四 | 32 | 1983-04-01 | 3500 | 101 |
| 1776 | 王五 | 24 | 1990-07-01 | 2000 | 101 |
| 1568 | 赵六 | 57 | 1970-10-11 | 7500 | 102 |
| 1564 | 荣七 | 64 | 1963-10-11 | 8500 | 102 |
| 1879 | 牛八 | 55 | 1971-10-20 | 7300 | 103 |
+------+--------+------+----------------+----------+-------+
6 rows in set (0.00 sec)
//创建提取emp_new表所有员工姓名和工资的存储过程s1
mysql> delimiter //
mysql> create procedure s1()
-> begin
-> select name,incoming from emp_new;
-> end //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call s1();
+--------+----------+
| name | incoming |
+--------+----------+
| 张三 | 4000 |
| 李四 | 3500 |
| 王五 | 2000 |
| 赵六 | 7500 |
| 荣七 | 8500 |
| 牛八 | 7300 |
+--------+----------+
6 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
//创建存储过程s2,实现输入员工姓名后返回员工的年龄
mysql> desc emp_new;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| sid | int | YES | | NULL | |
| name | varchar(11) | YES | | NULL | |
| age | int | YES | | NULL | |
| worktime_start | date | YES | | NULL | |
| incoming | int | YES | | NULL | |
| dept2 | int | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> delimiter //
mysql> create procedure s2(in s2_name varchar(11),out s2_age int)
-> begin
-> select age into s2_age from emp_new where name=s2_name;
-> end //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call s2('张三',@s2_age);
Query OK, 1 row affected (0.00 sec)
mysql> select @s2_age;
+---------+
| @s2_age |
+---------+
| 35 |
+---------+
1 row in set (0.00 sec)
//创建一个存储过程s3,有2个参数,传入部门号,返回该部门的平均工资
mysql> select * from emp_new;
+------+--------+------+----------------+----------+-------+
| sid | name | age | worktime_start | incoming | dept2 |
+------+--------+------+----------------+----------+-------+
| 1789 | 张三 | 35 | 1980-01-01 | 4000 | 101 |
| 1674 | 李四 | 32 | 1983-04-01 | 3500 | 101 |
| 1776 | 王五 | 24 | 1990-07-01 | 2000 | 101 |
| 1568 | 赵六 | 57 | 1970-10-11 | 7500 | 102 |
| 1564 | 荣七 | 64 | 1963-10-11 | 8500 | 102 |
| 1879 | 牛八 | 55 | 1971-10-20 | 7300 | 103 |
+------+--------+------+----------------+----------+-------+
6 rows in set (0.00 sec)
mysql> delimiter //
mysql> create procedure s3(in s3_dept2 int ,out s3_avgincoming int)
-> begin
-> select avg(incoming) into s3_avgincoming from emp_new where dept2=s3_dept2;
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> call s3(101,@s3_avgincoming);
Query OK, 1 row affected (0.00 sec)
mysql> select @s3_avgincoming;
+-----------------+
| @s3_avgincoming |
+-----------------+
| 3167 |
+-----------------+
1 row in set (0.00 sec)
mysql> call s3(102,@s3_avgincoming);
Query OK, 1 row affected (0.00 sec)
mysql> select @s3_avgincoming;
+-----------------+
| @s3_avgincoming |
+-----------------+
| 8000 |
+-----------------+
1 row in set (0.00 sec)