Thread: system maintained keys

system maintained keys

From
"Stefan Lindner"
Date:
Is there any way to get system maintained keys from postgres? e.g. to
have a table with a primary key column (varchar or int) and let postgres
chose the next unique value for this column?




Re: system maintained keys

From
"Josh Berkus"
Date:
Stefan,

> Is there any way to get system maintained keys from postgres? e.g. to
> have a table with a primary key column (varchar or int) and let
> postgres
> chose the next unique value for this column?

Please address future questions of this type to the PGSQL-NOVICE list.
PGSQL-SQL is for more advanced SQL issues.

Yes, you can do this easily,  Please see:
http://www.postgresql.org/idocs/index.php?datatype.html#DATATYPE-NUMERIC
Look at secion 3.1.1 on the page.

-Josh

______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


Re: system maintained keys

From
Jason Earl
Date:
Check out the SERIAL type.  It does precisely what you
want.  An idea as to how this is used would be like
this:

CREATE TABLE foo ( prim_key     SERIAL PRIMARY KEY, bar          text
);

I tend to create sequences by hand like this:

CREATE SEQUENCE my_sequence_seq;

And then I create my table with a definition like
this:

CREATE TABLE foo ( prim_key     int DEFAULT nextval('my_sequence_seq')                PRIMARY KEY, bar          text,
);

But that's just because I have been using PostgreSQL
long enough that it didn't have the SERIAL type when I
started.  The SERIAL type is just syntactic sugar for
what I generally do the long way.

Either way you simply pretend that the column isn't
there when you do inserts (unless you know what you
are doing) like so:

INSERT INTO foo (bar) VALUES ('hello');
INSERT INTO foo (bar) VALUES ('goodbye');

And then when you select you get:

processdata=> SELECT * FROM foo;prim_key |   bar   
----------+---------       1 | hello       2 | goodbye
(2 rows)

I hope that is helpful,
Jason Earl

--- Stefan Lindner <lindner@visionet.de> wrote:
> Is there any way to get system maintained keys from
> postgres? e.g. to
> have a table with a primary key column (varchar or
> int) and let postgres
> chose the next unique value for this column?
> 
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org


__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com


Re: system maintained keys

From
Joel Burton
Date:
On Mon, 15 Oct 2001, Stefan Lindner wrote:

> Is there any way to get system maintained keys from postgres? e.g. to
> have a table with a primary key column (varchar or int) and let postgres
> chose the next unique value for this column?

\h CREATE SEQUENCE  will give syntax, or look up SEQUENCES and SERIAL data
type in the documentation.

-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant



Re: system maintained keys

From
"Stefan Lindner"
Date:
Thank you very much! Thanks to all who answered! An I will never again ask
silly questions here :-)
"Stefan Lindner" <lindner@visionet.de> schrieb im Newsbeitrag
news:9qf06v$20bh$1@news.tht.net...
> Is there any way to get system maintained keys from postgres? e.g. to
> have a table with a primary key column (varchar or int) and let postgres
> chose the next unique value for this column?
>
>