Re: inserting via "on insert" rule

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Andreas Fromm <Andreas(dot)Fromm(at)physik(dot)uni-erlangen(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: inserting via "on insert" rule
Date: 2003-09-01 21:28:31
Message-ID: Pine.LNX.4.44.0309012326200.1206-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andreas Fromm writes:

> I was thinking of defining a view "users" over "persons" which would let
> me retrive the list of useres. But How would I implement the rule for
> insertiung users? I tryed the following but NEW is not known where I
> want to use it:
>
> CREATE VIEW users AS
> SELECT * FROM persons WHERE is_user(person.id) = TRUE;
>
> CREATE RULE insert_on_users AS ON INSERT
> TO users DO INSTEAD
> INSERT INTO persons SELECT * FROM NEW;

You can write

... DO INSTEAD INSERT INTO persons VALUES (NEW.col1, NEW.col2, ...);

> The other thing I don't know is how to enforce the aditional constraints
> that qualify as user when inserting to users. Of course I could do it on
> the application side, but I would like to have it on the database too.

CREATE RULE insert_on_users AS ON INSERT TO users
WHERE is_user(NEW.id)
DO ...

Read the chapter on rules in the documentation to learn more about this.

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-09-01 21:28:49 Re: More on 7.4b2 vs 7.3.4 performance
Previous Message Peter Eisentraut 2003-09-01 21:22:42 Re: sort ignoring the, a, ...