Re: Triggering a table id from a sequence

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: Novice Postgresql-list <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Triggering a table id from a sequence
Date: 2006-04-11 03:17:43
Message-ID: 20060411031743.31727.qmail@web31804.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I don't believe that you need a trigger to do this. When you create a table that will use an
"id"
sequence, you would simply define its datatype as a serial or bigserial. In the background, it
will automatically create a sequence for you and it will automatical set you id column's default
valve to "nextval("your_new_sequence"). when you insert rows into your new table, simply allow
your table to use its default value to give you the next value in you sequence.

see http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL for an example.

Regards,

Richard

--- Lan Barnes <lan(at)falleagle(dot)net> wrote:

> My desire is to have a table auto update an "id" sequence on every
> insert, relieving the front end program of the responsibility.
>
> I've never written a trigger. I imagine that there may be a contrib
> trove of them.
>
> There may also be an easier way to approach the problem. Please feel
> free to refer me to books or web sites.
>
> TIA,
>
> --
> Lan Barnes lan(at)falleagle(dot)net
> Linux Guy, SCM Specialist 858-354-0616
> Tcl/Tk Enthusiast
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>

>

Browse pgsql-novice by date

  From Date Subject
Next Message John DeSoi 2006-04-11 05:07:54 Re: How to append records into a file which has serial unique IDs?
Previous Message Richard Broersma Jr 2006-04-11 01:39:54 Re: PostgreSQL a slow DB?