Assume in Postgresql, I have a table T
and one of its column is C1
.
I want to trigger a function when a new record is adding to the table T
. The function should check the value of column C1
in the new record and if it is null/empty then set its value to 'X'
.
Is this possible?
It is possible but you are likely to be better off setting a default constraint on the column instead. When creating the table that would look like:
This says that if you add a row to the table and don't specify the value for C1 then X will be used instead. The not null is not necessary, but prevents updates from nulling that column assuming that's what you want.
EDIT: This only works for constant X, from your comments it seems there are two possible solutions.
Using a trigger would look something like this:
The
new
variable in a trigger function is special, representing the row being inserted. Specifying the trigger as abefore insert
trigger means you can modify the row before it is written to the table.The second solution would be to use a computed column which Postgres defines in an unusual way:
This creates a function that takes a row of your table and returns a value, you can call it using . notation though, meaning you can do:
The declaration of the function being immutable is optional, but it's needed if you want to index the "column". You would be able to index this column like this:
You are correct that you need a trigger, because setting a default value for the column won't work for you - default values only work for
null
values and don't help you in preventing blank values.In postgres there are a couple of steps to creating a trigger:
Step 1: Create a function that returns type
trigger
:Step 2: Create a trigger that fires before insert, which allows you to change values befre they are inserted, that invokes the above function:
And you're done.
See the above code executing on SQLFIddle demonstrating it working correctly!
You mention in a comment that the value
'X'
is retrieved from a subquery. If so, change the relevant line so something like: