In MySQL 5.5, I know I can prevent an insert by using SIGNAL
, which rolls back the current transaction of my INSERT
.
The fact is I want to use a trigger to insert a row in another table,
but I'd like that to replace the original insertion. If I use SIGNAL
in the trigger the new insert statement is also cancelled.
Example:
If I do
INSERT INTO table_a VALUES('a','b');
I want to have a new row in a table_b
but none in the table_a
.
(I want to replace the original insertion only in a few cases.)
Is it possible in MySQL?
If yes, how can I do this?
Best Answer
I am not readily availble to provide exact details but I believe 2 more ways are possible:
Using Blackhole engine
It seems to be the simplest solution.
See MySQL 5.5 doc for more details.
First, if you need to keep the
table_a
you need to create a copy of it with the blackhole engine ;Then, create the trigger on
table_blackhole
.Inside the trigger, use
INSERT
ontable_a
(for example) for the normal treatment (we don't prevent the insertion)INSERT
ontable_b
for the specific treatment (we "prevent" the normal insertion but there is no rollback).Instead of inserting rows in
table_a
, do yourINSERT
ontable_blackhole
.