gpt4 book ai didi

mysql - 更新时触发

转载 作者:行者123 更新时间:2023-11-29 17:34:01 27 4
gpt4 key购买 nike

当我更新表格Form中的数据时,表格Book中的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;

最佳答案

您只需在表“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 ;

关于mysql - 更新时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50437393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com