Re: Create unique field..

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: "Williams, Travis L, NPONS" <tlw(at)att(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Create unique field..
Date: 2001-06-07 22:10:41
Message-ID: 20010607221041.23802.qmail@web10007.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


One other thing you need to do is to make sure that
you don't actually put some other information into
that column. For example, let's say that you have a
table defined like this:

processdata=# create sequence foo_seq;
CREATE
processdata=# create table foo (id int default
nextval('foo_seq'), name text);
CREATE

You might want to get a little fancier and make id a
primary key or something, but this gives you the
general idea.

Now if you were to insert a record where id was set
then your default value would get overwritten:

processdata=# insert into foo (id, name) values (100,
'Jason');
INSERT 6869711 1
processdata=# insert into foo (name) values ('Earl');
INSERT 6869712 1
processdata=# select * from foo
processdata-# ;
id | name
-----+-------
100 | Jason
1 | Earl
(2 rows)

You will notice that the insert that I did where I did
not set id used the sequence without any problems.
This is useful for cases where you *normally* want to
use the sequence but where there might be exceptions.

Hope this is helpful,

Jason

--- "Williams, Travis L, NPONS" <tlw(at)att(dot)com> wrote:
> All,
> I'm not sure if I'm on the right track here.. I
> want to create a
> column that is automatically filled in with a unique
> number when a row is
> created.. I thought that creating a sequence would
> do this.. and then
> creating a column with a default of
> nextval('seqname') but it isn't doing
> anything.. any help is appreciated...
>
> Thanks,
> Travis L. Williams
>
> ---------------------------(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 - only $35
a year! http://personal.mail.yahoo.com/

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Sykora, Dale 2001-06-07 22:39:17 2 novice questions
Previous Message Giorgio A. 2001-06-07 21:49:55 Re: Create unique field..