我有2张桌子。音乐会/门票
我想将Concert 1设置为最多100张票,而Concert 2最多设置200张票。所以喜欢将concert_id与最大数量的ticket_id相关联。
我如何在mysql中执行此操作?感谢
答案 0 :(得分:0)
您可以编写触发器:
delimiter //
drop trigger if exists limitInsertsTrigger //
create trigger limitInsertsTrigger before insert on tickets
for each row
begin
declare msg varchar(128);
if (select count(*) from tickets where concert_id = new.concert_id)>
(select max_tickets_number from concert where id=new.concert_id)
then
set msg = concat('MyTriggerError: Trying to insert too many tickets');
signal sqlstate '45000' set message_text = msg;
end if;
end //
请记住在Concert表中有一个max_tickets_number列。