I have been trying to accomplish an automatic update of a table based on a column value. Here is the example. The idea is that the user will enter their phone, but not their country like this:
mobile_phone | country |
---|---|
‘51682815’ | |
‘51261728’ | |
‘56682815’ | |
‘56261728’ | |
‘57682815’ | |
‘57261728’ |
Based on the "mobile_phone" information I want to assign their country automatically with this logic:
- WHERE left(mobile_phone,2) = ’51’ then ‘PE’
- WHERE left(mobile_phone,2)=’56’ then ‘CL’
- WHERE left(mobile_phone,2)=’57’ then ‘CO’
And based on that logic the table should update AUTOMATICALLY with this information:
mobile_phone | country |
---|---|
‘51682815’ | ‘PE’ |
‘51261728’ | ‘PE’ |
‘56682815’ | ‘CL’ |
‘56261728’ | ‘CL’ |
‘57682815’ | ‘CO’ |
‘57261728’ | ‘CO’ |
I was thinking to create a trigger like this, but the problem is that I don’t know how to create the function that will update the column country:
CREATE TRIGGER update_country AFTER INSERT ON info
FOR EACH ROW EXECUTE PROCEDURE function_name()
Thanks in advance for your help.
2
Answers
Since your records to be updated are already found within your database, you should first use an
UPDATE
statement to update your current rows. You can do it using aCASE
expression that will assign the "country" value conditionally with respect to the "mobile_phone".Then create the corresponding trigger, that will update your record before the insertion. It splits into:
NEW
to access values from the inserting row and update its country valueCheck the demo here.
You can use Generated Columns.
With Generated Columns you can not do
update info set country = 'x';
orinsert into info(country) value ('x');
https://www.postgresql.org/docs/current/ddl-generated-columns.html