Re: No serial type

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Simon Connah" <simon(dot)n(dot)connah(at)btopenworld(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: No serial type
Date: 2008-11-18 17:37:45
Message-ID: dcc563d10811180937r2f97bdd6ma9f94853ffa228c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 18, 2008 at 10:24 AM, Simon Connah
<simon(dot)n(dot)connah(at)btopenworld(dot)com> wrote:
> Hi,
> I've just started using PostgreSQL and have been reading up on it. Part of
> what I wanted to do was to have a column which automatically incremented
> itself by one every time I do an INSERT. From reading the manual I was lead
> to believe that the correct type for this was either serial or bigserial.
> But whenever I try and update my column to this type it says that it can not
> find that data type. Am I doing something wrong?

Serial is a "pseudotype". It represents creating an int or bigint and
a sequence then assigning a default value for the column and setting
dependency in the db so the sequence will be dropped when the table
gets dropped. If you don't want to recreate the table, you can do
this:

create table a (i int primary key, info text);
create sequence a_i_seq;
alter table a alter column i set default nextval('a_i_seq');

not sure the alter table syntax is spot on, haven't used it in a few months.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message DANG Trieu 2008-11-18 17:40:57 Re: Slow queries when ORDER BY ... DESC with table inheritance (no index scan backwards)
Previous Message Andrus 2008-11-18 17:33:47 Conversion of string to int using digits at beginning