Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Re: Unique ID

by lhoward (Vicar)
on Jul 19, 2001 at 21:28 UTC ( [id://98163]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Unique ID
in thread Unique ID

The serial type creates and uses a sequence behing the scenes, but its operation is transparent to you. You do not need to create an additional sequence to use it. See... (using psql, Postgres's command line tool):
test=# create table foo(ID serial NOT NULL,bar text, constraint foo_pk primary key (ID));
NOTICE:  CREATE TABLE will create implicit sequence 'foo_id_seq' for SERIAL column 'foo.id'
NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index 'foo_pk' for table 'foo'
CREATE
test=# insert into foo (bar) values ('abc');
INSERT 27135 1
test=# insert into foo (bar) values ('def');
INSERT 27136 1
test=# select * from foo;
 id | bar
----+-----
  1 | abc
  2 | def
(2 rows)
I find postgres sequences to be most useful when you want to share one unique ID across tables and when you don't have a master table to store it in (or don't have a table that will always be inserted first). Or when you want to assign something outside of the DB (at least at the time of assignment) a unique ID.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://98163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found