数据库第五次作业

要求 

1.定义触发器实现在产品表(product)中每多一个产品,就在操作表(operate)中记录操作方式和时间以及编号记录。
​ 注:操作说明:标记执行delete 、insert、 update
2.定义触发器实现在产品表(product)中每更新一个产品,就在操作表(operate)中记录操作方式和时间以及编号记录。
3.定义触发器实现在产品表(product)中每删除一个产品就,在操作表(operate)中记录操作方式和时间以及编号记录。
 

准备  

- 建立product表,操作方式operate表
Product表内容 :
字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
id 产品编号 Int(10) 是 否 是 是 否
name 产品功能 Varchar(20) 否 否 是 否 否
func 主要功能 Varchar(50) 否 否 否 否 否
com 生产厂家 Varchar(20) 否 否 是 否 否
address 家庭住址 Varchar(20) 否 否 否 否 否
operate表内容 :
字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
op_id 编号 Int(10) 是 否 是 是 是
op_type 操作方式 Varchar(20) 否 否 是 否 否
op_time 操作时间 Varchar(20) 否 否 是 否 否

建表语句

mysql> use lianxi
Database changed
mysql>
mysql> create  table  product(
    -> id  int(10)  not null  auto_increment primary key,
    -> name varchar(20) not null,
    -> func  varchar(50),
    -> com  varchar(50) not null,
    -> address  varchar(20) );
Query OK, 0 rows affected, 1 warning (0.12 sec)

mysql> create table operate(
    -> op_id int(10)  not null auto_increment primary key,
    -> op_type   varchar(20)  not null,
    -> op_time   varchar(20)  not null );
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> desc operate;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| op_id   | int         | NO   | PRI | NULL    | auto_increment |
| op_type | varchar(20) | NO   |     | NULL    |                |
| op_time | varchar(20) | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> desc product;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int         | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     | NULL    |                |
| func    | varchar(50) | YES  |     | NULL    |                |
| com     | varchar(50) | NO   |     | NULL    |                |
| address | varchar(20) | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql>

 创建触发器并验证,每次激活触发器后,都会更新operate表 

(注:最后使用after触发器,before触发器也可以) 

 创建product_after_insert_trigger

mysql> create  trigger product_after_insert_trigger before  insert  on product  for each row insert into  operate(op_type,op_time) values ('insert',now());
Query OK, 0 rows affected (0.03 sec)
mysql> insert into product values(1,'海尔洗衣机','洗衣机','海尔','合肥');
Query OK, 1 row affected (0.01 sec)

mysql> select * from product;
+----+------------+--------+------+---------+
| id | name       | func   | com  | address |
+----+------------+--------+------+---------+
|  1 | 海尔洗衣机 | 洗衣机 | 海尔 | 合肥    |
+----+------------+--------+------+---------+
1 row in set (0.00 sec)

mysql> select * from operate;
+-------+---------+---------------------+
| op_id | op_type | op_time             |
+-------+---------+---------------------+
|     1 | insert  | 2025-07-20 17:09:30 |
+-------+---------+---------------------+
1 row in set (0.00 sec)

mysql>


- 创建product_after_update_trigger

mysql> create  trigger product_after_update_trigger before update on  product for each row
    -> insert into  operate values (null,'update',now());
Query OK, 0 rows affected (0.02 sec)
mysql>  update  product set  address='上海' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from product;
+----+------------+--------+------+---------+
| id | name       | func   | com  | address |
+----+------------+--------+------+---------+
|  1 | 海尔洗衣机 | 洗衣机 | 海尔 | 上海    |
+----+------------+--------+------+---------+
1 row in set (0.00 sec)

mysql> select * from operate;
+-------+---------+---------------------+
| op_id | op_type | op_time             |
+-------+---------+---------------------+
|     1 | insert  | 2025-07-20 17:09:30 |
|     2 | update  | 2025-07-20 17:11:13 |
+-------+---------+---------------------+
2 rows in set (0.00 sec)

mysql>


- 创建product_after_delete_trigger

mysql> create  trigger product_after_delete_trigger before delete on product  for each row
    -> insert into  operate values (null,'delete',now());
Query OK, 0 rows affected (0.01 sec)
mysql> delete from product where id='1';
Query OK, 1 row affected (0.01 sec)

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

mysql> select * from operate;
+-------+---------+---------------------+
| op_id | op_type | op_time             |
+-------+---------+---------------------+
|     1 | insert  | 2025-07-20 17:09:30 |
|     2 | update  | 2025-07-20 17:11:13 |
|     3 | delete  | 2025-07-20 17:12:07 |
+-------+---------+---------------------+
3 rows in set (0.00 sec)

mysql>

你可能感兴趣的:(数据库第五次作业)