| From: | "Albert REINER" <areiner(at)tph(dot)tuwien(dot)ac(dot)at> | 
|---|---|
| To: | pgsql-novice(at)postgresql(dot)org | 
| Subject: | Re: Question on setting up trigger. | 
| Date: | 2001-01-04 20:45:04 | 
| Message-ID: | 20010104214504.A1728@frithjof | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-novice | 
Saluton,
On Tue, Jan 02, 2001 at 09:38:52AM -0700, Alan Young wrote:
> I have the following table:
> 
> CREATE TABLE category (
>         id int NOT NULL DEFAULT nextval('_base_id_seq'::text) PRIMARY
> KEY,
>         parentid int REFERENCES category ( id )
>         name text,
> );
> 
...
> CREATE FUNCTION "move_catparent" (int4 )
> RETURNS int4
> AS 'UPDATE category
>     SET parentid = ( SELECT parentid FROM category WHERE id = $1 )
>     WHERE parentid = $1;
>     SELECT 1 AS RESULT'
> LANGUAGE 'SQL';
I myself don't know much about triggers, but I think you need:
create trigger <trigger name> before delete on category
  for each row execute procedure move_catparent(old.id);
On the other hand, I suppose that a rule might be the better option.
I don't know much about this, so don't take this at face value.
Albert.
> 
> As it stands, I have to do the following when deleting a category:
> 
> SELECT move_catparent( <id of category being deleted> );
> DELETE FROM category WHERE id=<id of category being deleted>;
> 
...
--
--------------------------------------------------------------------------
Albert Reiner                                   <areiner(at)tph(dot)tuwien(dot)ac(dot)at>
Deutsch       *       English       *       Esperanto       *       Latine
--------------------------------------------------------------------------
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Fernando M. Maresca | 2001-01-05 00:20:48 | Re: well, this is a newbie list... | 
| Previous Message | Tom Lane | 2001-01-04 16:47:13 | Re: problems with unique key on table with serial |