Re: [NOVICE] alter existing table column with primary key to auto-increment

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: [NOVICE] alter existing table column with primary key to auto-increment
Дата
Msg-id 13322.1486316435@sss.pgh.pa.us
обсуждение исходный текст
Ответ на [NOVICE] alter existing table column with primary key to auto-increment  (Rounak Jain <rounakjainis@gmail.com>)
Список pgsql-novice
Rounak Jain <rounakjainis@gmail.com> writes:
> I am just starting with psql.
> I have created surrogate id keys in some tables.
> They are primary keys.
> I want to add auto-increment to them.

You want to read the definition of serial types here:

https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

If you already have the column, you can make the sequence object, add the
default expression (ALTER TABLE ... ALTER COLUMN ... SET DEFAULT ...
I think is the right syntax) and do the ALTER OWNED BY if you'd like the
sequence to go away if the column is dropped.

If there's data in the column already, you probably also need to tweak the
current value of the sequence.

            regards, tom lane


В списке pgsql-novice по дате отправления:

Предыдущее
От: Rounak Jain
Дата:
Сообщение: Re: [NOVICE] alter existing table column with primary key to auto-increment
Следующее
От: "Kranenburg, Roger"
Дата:
Сообщение: [NOVICE] pgAdmin4 1.1 Import Dialog does nothing