Re: auto-increment column

From: Robert Buckley <robertdbuckley(at)yahoo(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: auto-increment column
Date: 2011-10-04 12:05:39
Message-ID: 1317729939.30316.YahooMailNeo@web24104.mail.ird.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks for the replies,

I have one question regarding this comment...

"You also need to add a DEFAULT expression and optionallymake the sequence owned by the column:"

What difference does it make if a table "owns" a sequence of not?...does this contraint the use of the sequence to ONLY that one table?

The sequence will only be used to auto-increment the id column in order to have an automatic primary key. Could I then somehow use the sequence for all tables which need this?

yours,

Rob

________________________________
Von: Boszormenyi Zoltan <zb(at)cybertec(dot)at>
An: pgsql-general(at)postgresql(dot)org
Gesendet: 13:43 Dienstag, 4.Oktober 2011
Betreff: Re: [GENERAL] auto-increment column

2011-10-04 13:30 keltezéssel, Robert Buckley írta:
Hi,
>
>
>I have a column in a table called hist_id with the datatype "integer". When I created the table I assigned this column the primary key constraint but didn´t make it an auto-increment column.
>
>
>How could I do this to an the already existing column?
>
>
>I have created the sequence with the following command but don´t know how to change the existing column to auto-increment.
>
>
>
>
>$ create sequence hist_id_seq;
You also need to add a DEFAULT expression and optionally
make the sequence owned by the column:

ALTER TABLE tablename ALTER COLUMN hist_id SET DEFAULT
nextval('hist_id_seq');
ALTER SEQUENCE hist_id_seq OWNED BY tablename.hist_id;

The "ALTER SEQUENCE ... OWNED BY ..." will make the sequence
automatically dropped if this column or table is dropped.

>
>thanks for any help,
>
>
>Rob

--
----------------------------------
Zoltán Böszörményi
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt, Austria
Web: http://www.postgresql-support.de http://www.postgresql.at/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Boszormenyi Zoltan 2011-10-04 12:24:51 Re: auto-increment column
Previous Message Boszormenyi Zoltan 2011-10-04 11:43:07 Re: auto-increment column