MySQL insert trigger only for new rows -
i want trigger in mysql inserts information of table in table b if it´s new entry.
my trigger inserts entrys of table table b if new entry in table made:
create trigger trigger_insert after insert on tablea each row begin insert `tableb` ( `id`, `active`, `created_at`, `updated_at` ) select `id`, `active`, `created_at`, now() tablea; end; my problem is, have second trigger inserts entrys of tablea after update. can´t make id unique. result should tableb kind of history insert , update statements in tablea
would thankfull help!
your problem select rows tablea. assume intend:
insert `tableb`(`id`, `active`, `created_at`, `updated_at`) select new.id, new.active, new.created_at, mnw() the more general answer question add unique constraint on tableb. assuming id defines duplicate entry:
alter table tableb add constaint unq_tableb_id unique (id); then change insert ignore duplicate inserts:
insert `tableb`(`id`, `active`, `created_at`, `updated_at`) select `id`, `active`, `created_at`, mnw() tablea on duplicate key update id = values(id); if duplicates still needed when use new, can combine these:
insert `tableb`(`id`, `active`, `created_at`, `updated_at`) select new.id, new.active, new.created_at, mnw() on duplicate key update id = values(id); both these queries assume id used define duplicate.
Comments
Post a Comment