计算所得的列不能引用其他表中的列(它看到的只是其自己的表行中的值)。
取而代之的是,您可以使用针对每个插入操作触发的触发器,event
并cost
为其他表/列分配默认值:
delimiter |
create trigger trg_event_price
before insert on event
for each row
begin
if new.cost is null then
set new.cost =
(select popularity from artist where id = new.artist_id)
* (select reputation from stage where id = new.stage_id)
;
end if;
end
|
delimiter ;
使用此触发器后,您可以cost
在insert
进入时忽略column event
,并让触发器处理它:
insert into event (artist_id, stage_id) values(1, 5);
-- "cost" will be computed from the popularity of artist 1 and the reputation of stage 5
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句