I have a table like
+ Column1 | Column2 | Column3 + +---------|---------|---------+ + 1 | val.txt | val.txt + + 2 | test.xls| test.xls+ + 3 | abc.dwg | abc.dwg + + 4 | y.txt | y.txt + + 5 | kylk.txt| + ............................... +---------|---------|---------+ Now when there is an empty value in column 3 then by default it should add the value of column 2, can that be done within the same table column with default condition?
Thought of creating a function and trigger and I have tried by creating a function but failed.
CREATE OR REPLACE FUNCTION fun_gen() RETURNS trigger AS $BODY$BEGIN update public.tab_name set column3=column2 where column3 is null; END$BODY$ LANGUAGE plpgsql VOLATILE COST 100; Is there a way to give a default condition on table column itself or how to solve it function and trigger Am using postgres 9.4 in centos 7.
Thanks in advance
UPDATEin a trigger is dangerous. Use theNEW/OLDspecial variables instead. -- But consider not changing your table at all: it is very easy to query your table in the desired format:SELECT column1, column2, coalesce(column3, column2) FROM table1.