1、如A表的触发器,在插入元组的时候,自动生成一个B表的元组,插入到B表中。 后来若B表的表结构变了,则需要及时更新触发器的数值么? 还是在B表执行 alter table的时候,就会检查这个触发器,然后alter table 会失败? - -执行alter table不会失败,但是如果再向A表中插入元组,触发执行了插入触发器,这个时候就会报错,报 XXX 列未知。
测试代码如下:
drop table if exists b;
drop table if exists a;
create table if not exists a
(
id int auto_increment,
key(id)
);
create table if not exists b
(
idb int null default 3 ,
name varchar(50)
-- foreign key(idb) REFERENCES a(id) on delete no action on update no action
) ;
delimiter $
create trigger tgr_a_after_insert after insert on a
for each row
begin
insert into b set idb=new.id,name='unknown';
insert into b set idb=new.id;
end;
create trigger tgr_a_update after update on a
for each ROW
BEGIN
update b set idb=new.id where b.idb=old.id;
end;
create trigger tgr_a_delete after delete on a
for each ROW
BEGIN
update b set idb=9999 where b.idb=old.id;
end;
$
insert into a values(1);
insert into a values(2);
update a set id=id+100;
delete from a where id=101;
select * from a;
select * from b;