I would like to have a trigger to perform following operation for inserted records:
# pseudocode
if new.group_id is null
set new.group_id = new.id
else
# don't touch it
end
More clearly: say I have one table with three columns: id
primary key, group_id
int, value
varchar.
When I insert with group_id
like that:
INSERT INTO table(value, group_id) VALUES ('a', 10)
I'd like to have:
id | group_id | value
---+----------+------
1 | 10 | a
but when I omit group_id
:
INSERT INTO table(value) VALUES ('b')
it should be automatically set to the id
of this record:
id | group_id | value
---+----------+------
2 | 2 | b
Is it possible with a trigger? (I know I can update the record after inserting but having the trigger would be nicer.)