RE: Serial field

From: "Robby Slaughter" <webmaster(at)robbyslaughter(dot)com>
To: "Jason Earl" <jdearl(at)yahoo(dot)com>, "Francois Thomas" <FrancoisT(at)alsdesign(dot)fr>, <pgsql-novice(at)postgresql(dot)org>
Subject: RE: Serial field
Date: 2001-07-12 23:48:10
Message-ID: EPEHLKLEHAHLONFOKNHNEEMEDCAA.webmaster@robbyslaughter.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Jason and Francois:

Jason: That's not how I interepreted Francois' request,
but it's a clever approach to what sounds like a common
need in database design. And yes, using a recent version
of PostgreSQL is a good idea!

Francois: It sounds like you just want an
auto-increment field but you don't want to
values you've already have in the table.

I think your best option is to do create the table
first just using standard INTEGERs and import your
data into the table. Then do an ALTER TABLE and
change the table design.

I don't have a convienent way to try it right now
but that should work.

If it doesn't, you count always just create your
own pgplsql stored procedure that works like
a standard sequence. As far as I can tell, when
you create a SERIAL type, that's what happens
anyway. The table definition ends up having
a DEFAULT value for the serial field, which
is a function call to nextval('tablename_fieldname_seq');

You could implement the same code yourself and just
not "turn on" the function until after your original
data is entered.

Hope that helps.

-Robby

> I want to migrate a database to PostgreSQL 6.5.2
> I need an auto-increment field. I first tried the
> SERIAL type, but it
> doesn't fit my needs.
> In fact, I want to:
> 1/ keep the original INT value of my existing
> records from another server
> (say, first row can be "1", second "3", next one
> "17",...)
> 2/ have an automatic incrementation of the last
> value for a new record. For
> exemple, with the values above and a SERIAL field,
> the default value for a
> new field would be "1" (first use of the sequence)
> instead of "18" (last
> value+1)..
> I hope my english is not too obscure !
> Any advice will be welcome
> Regards
>
> --
> Frangois THOMAS
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Jason Earl 2001-07-13 01:39:58 RE: Serial field
Previous Message Jason Earl 2001-07-12 20:57:40 Re: Serial field