第五次作业
建立product表,操作方式operate表要求
1.定义触发器实现在产品表(product)中每多一个产品,就在操作表(operate)中记录操作方式和时间以及编号记录。 (注:操作说明:标记执行delete 、insert、 update)
2.定义触发器实现在产品表(product)中每更新一个产品,就在操作表(operate)中记录操作方式和时间以及编号记录。
3.定义触发器实现在产品表(product)中每删除一个产品,就在操作表(operate)中记录操作方式和时间以及编号记录。
-----mysql
(1)、Product表内容 :字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
id 产品编号 Int(10) 是 否 是 是 否
name 产品功能 Varchar(20) 否 否 是 否 否
func 主要功能 Varchar(50) 否 否 否 否 否
com 生产厂家 Varchar(20) 否 否 是 否 否
address 家庭住址 Varchar(20) 否 否 否 否 否
代码如下:
mysql> create table product(
-> id int(10) primary key not null unique,
-> name varchar(20) not null,
-> func varchar(50),
-> com varchar(20),
-> address varchar(20));
Query OK, 0 rows affected, 1 warning (0.05 sec)
(2)、operate表内容 :字段名 字段描述 数据类型 主键 外键 非空 唯一 自增
op_id 编号 Int(10) 是 否 是 是 是
op_type 操作方式 Varchar(20) 否 否 是 否 否
op_time 操作时间 Varchar(20) 否 否 是 否 否
代码如下:
mysql> create table operate(
-> op_id int(10) primary key not null unique auto_increment,
-> op_type varchar(20) not null,
-> op_time varchar(20) not null,
-> product_id int(10) not null);
Query OK, 0 rows affected, 2 warnings (0.05 sec)
(3)、 创建触发器,每次激活触发器后,都会更新operate表
---创建product_after_insert_trigger
mysql> delimiter //
mysql> create trigger
-> product_after_insert_trigger
-> after insert on product
-> for each row
-> begin
-> insert into operate (op_type,op_time,product_id)
-> values ('insert',now(),new.id);
-> end //
--- 创建product_after_update_trigger
mysql> delimiter //
mysql> create trigger
-> product_after_update_trigger
-> after update on product
-> for each row
-> begin
-> insert into operate (op_type,op_time,product_id)
-> values ('update',now(),new.id);
-> end //
--- 创建product_after_delete_trigger
mysql> delimiter //
mysql> create trigger
-> product_after_delete_trigger
-> after delete on product
-> for each row
-> begin
-> insert into operate (op_type,op_time,product_id)
-> values ('delete',now(),old.id);
-> end //
(4)测试插入
mysql> insert into product (id,name) values(1001,'智能手机');
(此时operate表新增了如下所示)
(5)测试更新
mysql> update product set func = '通信' where id = 1001;
(此时operate表新增了如下所示)
(6)测试删除
mysql> delete from product where id = 1001;
(此时operate表新增如下所示)