数据库第五次作业

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.09 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> 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> 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> 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> 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> 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> 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> 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> 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> 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 | incominh | 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 s1()
    -> begin
    ->     select name,incominh from emp_new;
    -> end $$
Query OK, 0 rows affected (0.01 sec)

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

Query OK, 0 rows affected (0.00 sec)



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)


mysql> delimiter $$
mysql> create procedure s3(in in_dept int,out avg_sal float)
    -> begin
    ->     select avg(incominh) 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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值