Skip site navigation (1) Skip section navigation (2)

Re: freezing a particular field in a table

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: hubert depesz lubaczewski <depesz(at)gmail(dot)com>
Cc: fmiddleton(at)verizon(dot)net, pgsql-admin(at)postgresql(dot)org
Subject: Re: freezing a particular field in a table
Date: 2005-11-23 17:55:22
Message-ID: 20051123175522.GA3828@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-admin
On Wed, Nov 23, 2005 at 10:28:01AM +0100, hubert depesz lubaczewski wrote:
>         IF
>                 (NEW.some_field IS NULL AND OLD.some_field IS NOT NULL)
>                 OR
>                 (NEW.some_field IS NOT NULL AND OLD.some_field IS NULL)
>                 OR
>                 (NEW.some_field IS NOT NULL AND OLD.some_field IS NOT NULL
> AND NEW.some_field <> OLD.some_field)
>         THEN

A more succinct expression would be:

  IF NEW.some_field IS DISTINCT FROM OLD.some_field THEN

IS DISTINCT FROM is like <> except that it treats NULL as an ordinary
value.

> CREATE TRIGGER impossible_to_change BEFORE UPDATE ON some_table FOR EACH ROW
> EXECUTE PROCEDURE impossible_to_change();

If the table has other triggers and you don't want them to modify
the value either, then the check should probably go in an AFTER
trigger.

-- 
Michael Fuhr

In response to

Responses

pgsql-admin by date

Next:From: hubert depesz lubaczewskiDate: 2005-11-24 07:38:47
Subject: Re: freezing a particular field in a table
Previous:From: hubert depesz lubaczewskiDate: 2005-11-23 09:28:01
Subject: Re: freezing a particular field in a table

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group