pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using a rule as a trigger.
3+ messages / 3 participants
[nested] [flat]

* Using a rule as a trigger.
@ 2001-01-10 09:25  Andrew Higgs <ahiggs@ps.co.za>
  0 siblings, 2 replies; 3+ messages in thread

From: Andrew Higgs @ 2001-01-10 09:25 UTC (permalink / raw)
  To: pgsql-sql

Hi all,

I have looked at some previous posting and thought that I had found 
exactly what I need. What I need is to insert an id (from a sequence) 
when a new record is inserted. The following example almost does what I 
need :

CREATE TABLE  topics (id int, topic varchar(50), descriotion text);
CREATE SEQUENCE nextid start 1;
CREATE RULE ins_topic AS ON INSERT TO topics WHERE id ISNULL DO UPDATE 
topics SET id=nextval('nextid') WHERE  id ISNULL;


This example updates the last insert. I need it to update the currnet 
insert. How do I do this?

Kind regards
Andrew Higgs




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Using a rule as a trigger.
@ 2001-01-10 14:24  Tulio Oliveira <mestredosmagos@marilia.com>
  parent: Andrew Higgs <ahiggs@ps.co.za>
  1 sibling, 0 replies; 3+ messages in thread

From: Tulio Oliveira @ 2001-01-10 14:24 UTC (permalink / raw)
  To: Andrew Higgs <ahiggs@ps.co.za>; +Cc: pgsql-sql

Andrew Higgs wrote:
> 
> Hi all,
> 
> I have looked at some previous posting and thought that I had found
> exactly what I need. What I need is to insert an id (from a sequence)
> when a new record is inserted. The following example almost does what I
> need :
> 
> CREATE TABLE  topics (id int, topic varchar(50), descriotion text);
> CREATE SEQUENCE nextid start 1;
> CREATE RULE ins_topic AS ON INSERT TO topics WHERE id ISNULL DO UPDATE
> topics SET id=nextval('nextid') WHERE  id ISNULL;
> 
> This example updates the last insert. I need it to update the currnet
> insert. How do I do this?
> 
> Kind regards
> Andrew Higgs



Isn't better create the field "id" as "serial" instead "int" ???

this will do what you need.

-- 
======================================================
AKACIA TECNOLOGIA
Desenvolvimento de sistemas para Internet
www.akacia.com.br



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Using a rule as a trigger.
@ 2001-01-10 18:32  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Andrew Higgs <ahiggs@ps.co.za>
  1 sibling, 0 replies; 3+ messages in thread

From: Stephan Szabo @ 2001-01-10 18:32 UTC (permalink / raw)
  To: Andrew Higgs <ahiggs@ps.co.za>; +Cc: pgsql-sql


As someone else said a serial is probably easier, but 
a trigger is probably a better bet than a rule for this purpose.
Using a plpgsql before insert trigger will do it.

On Wed, 10 Jan 2001, Andrew Higgs wrote:

> Hi all,
> 
> I have looked at some previous posting and thought that I had found 
> exactly what I need. What I need is to insert an id (from a sequence) 
> when a new record is inserted. The following example almost does what I 
> need :
> 
> CREATE TABLE  topics (id int, topic varchar(50), descriotion text);
> CREATE SEQUENCE nextid start 1;
> CREATE RULE ins_topic AS ON INSERT TO topics WHERE id ISNULL DO UPDATE 
> topics SET id=nextval('nextid') WHERE  id ISNULL;
> 
> 
> This example updates the last insert. I need it to update the currnet 
> insert. How do I do this?




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-01-10 18:32 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-01-10 09:25 Using a rule as a trigger. Andrew Higgs <ahiggs@ps.co.za>
2001-01-10 14:24 ` Tulio Oliveira <mestredosmagos@marilia.com>
2001-01-10 18:32 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox