Imagine I have a MySQL table (tbl_test) with these fields: id, title, priority.
id will be incremented automatically. I need to fill priority field with a value as same as id field after inserting.
As I'm new in using MySQL triggers, please tell me what I have to write for it. I did something , but I think it is not true:
CREATE TRIGGER 'test' AFTER INSERT ON `tbl_test`
BEGIN
SET new.priority = new.id;
END
Thanks for your assistance.
I don't think you can do that. An AFTER INSERT trigger cannot modify the same table, neither by issuing an UPDATE nor by something like this:
It gives error like
What you can do, is use a transaction:
Example : Table structure is like below
Transaction
Check data
The way you are trying to set value to a column is an update. Because you are doing it after insert operation is completed.
You actually need a
before
trigger.And to assign the same new auto incremented value of primary key column of same table, you better get it from
information_schema.tables
.Example:
Note: Make sure that you don't have any pre-defined trigger with the same name and/or action. If have some, then drop them before creating the new.