I'm designing a PostgreSQL schema and I want to create a rule that if the user doesn't enter a value for Column C then i make the value of column C COALESCE(COLUMN A, COLUMN B). I've tried table update, and a trigger but there must be something wrong with the logic, I'd appreciate your help - thanks.
I have tried:
1.
update Mytable
set ColC=
case when ColC is NULL then COALESCE(ColA,ColB)
else ColC;
2.
CREATE TRIGGER ColC_VIOLATION
BEFORE INSERT OR UPDATE OF ColC
ON Mytable FOR EACH ROW
WHEN ColC is NULL then COALESCE(ColA,ColB)
3.
CREATE OR REPLACE FUNCTION ColC_func() RETURNS trigger AS
$$
BEGIN
UPDATE Mytable
set ColC=
case when ColC is NULL then COALESCE(ColA,ColB)
else ColC ;
END
$$
LANGUAGE PLPGSQL;