Re: Create unique field.. - Mailing list pgsql-novice

From Jason Earl
Subject Re: Create unique field..
Date
Msg-id 20010607221041.23802.qmail@web10007.mail.yahoo.com
Whole thread Raw
In response to Create unique field..  ("Williams, Travis L, NPONS" <tlw@att.com>)
List 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@att.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/

pgsql-novice by date:

Previous
From: "Giorgio A."
Date:
Subject: Re: Create unique field..
Next
From: "Sykora, Dale"
Date:
Subject: 2 novice questions