触发更新

时间:2018-05-20 17:08:03

标签: mysql sql mysql-workbench

当我更新表格中的数据表格中的表格时,必须减去colbook的数量-1。 请使用触发器帮助完成此操作

表格

Create table Form (
idForm int not null primary key auto_increment,
date_of_issue date not null,
return_date date ,
idBook int not null,
Foreign key(idBook) references Book (idBook));

表格书

Create table Book (
idBook int not null primary key auto_increment,
name varchar(45) not null,
colBook int(11) null;

填写表格

Insert into Form (idForm, date_of_issue)
Values(1, "2018-11-11");

填写表格书

Insert into Book (idBook, name, colBook)
Values(1, Garri Potter, 5);

表格更新申请表格

update Form
set return_date = "2000-03-03"
where idBook = 1;

1 个答案:

答案 0 :(得分:0)

您只需要在表格“Form”上创建一个触发器,如下所示:

delimiter $$
CREATE TRIGGER form_trigger_1 
AFTER INSERT ON form FOR EACH ROW 
BEGIN 
update book
set colbook = colbook - 1
where idbook = old.idbook;
END
delimiter ;