Re: Trigger once again

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adrian Klaver <aklaver(at)attbi(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger once again
Date: 2002-11-28 23:09:49
Message-ID: 16997.1038524989@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adrian Klaver <aklaver(at)attbi(dot)com> writes:
> I have created the following function:

> CREATE OR REPLACE FUNCTION validate_tag_number()
> RETURNS OPAQUE AS '
> BEGIN
> IF new.tag_number=old.tag_number THEN
> RAISE EXCEPTION ''Number is already present '';
> END IF;
> RETURN NEW;
> END;
> 'LANGUAGE 'plpgsql';

> I used it to create a trigger as follows;

> CREATE TRIGGER validate_tag_number BEFORE UPDATE ON tag_vendor FOR EACH ROW
> EXECUTE PROCEDURE validate_tag_number();

> The trigger seems to work to well. If I update values other than the
> tag_number I get the 'Number is already present' message. I am somewhat
> confused as their is no new.tag_number value being passed to the function.
> Can anyone set me straight?

NEW and OLD are record variables holding the whole row being updated
(new and old versions). The above trigger strikes me as pretty
pointless, since as you've found out it will object to perfectly
reasonable updates. What is it you really want to do --- ensure there's
only one occurrence of a given tag_number in the table? If so, you
need a unique index on the column, not a trigger.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2002-11-28 23:14:17 Re: Select nextval problem
Previous Message Tariq Muhammad 2002-11-28 22:49:12 Re: Trigger once again