gpt4 book ai didi

mysql - 如何根据表中先前插入的值创建插入前触发器

转载 作者:行者123 更新时间:2023-11-29 00:13:04 26 4
gpt4 key购买 nike

我有一个包含两列的表1.Clanid2.主动

我的问题是,如果 Clanid 列中的任何值已经在 Clanid 列中并且该值的 Active 为 1,我不想将 Clanid 列中的任何值插入到此表中。

例如
Clanid 活跃
1 1
2 1
3 0

现在应该不可能插入 Clanid=1 和 Active=1 的记录,但我可以插入 Clanid=3 和 Active=1,因为该记录不存在。

最佳答案

试试这个:

delimiter //
create trigger unique_clanid
before insert on mytable
for each row
begin
if new.active and exists (
select * from mytable
where clanid = new.clanid
and active) then
signal sqlstate '02000' set MESSAGE_TEXT = 'Duplicate ClanID';
end if;
end//
delimiter ;

关于mysql - 如何根据表中先前插入的值创建插入前触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078626/

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