Re: auto-increment field : in a simple way

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Vineet Deodhar <vineet(dot)deodhar(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: auto-increment field : in a simple way
Date: 2012-10-11 07:26:59
Message-ID: CAOR=d=0KTVeEY_HLYO-ga4z_mt+AFH2UEfuYBTg-fOVN9S635g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Oct 11, 2012 at 1:04 AM, Vineet Deodhar
<vineet(dot)deodhar(at)gmail(dot)com> wrote:
> I wish to know regarding auto-increment field.
> I learn that the required table definition would be something like --
>
> CREATE TABLE tablename (
> colname SERIAL
> );
>
> For more granular control over the size of field, I need to do the
> following---
>
> CREATE SEQUENCE user_id_seq;
>
> CREATE TABLE user (
>
> user_id smallint NOT NULL DEFAULT nextval('user_id_seq')
>
> );
> ALTER SEQUENCE user_id_seq OWNED BY user.user_id;
>
>
>
> I am not pinpointing MySQL v/s Postgres; but since I am accustomed to using
> simply "AUTOINCREMENT" in MySQL,
> I find this a bit cumbersome procedure.
>
> 1] Is there any simpler way of doing this?
>
> 2] Whether the development team has this point in their TO DO list for
> future release of postgres?

Can't you just add this to your create table:

CREATE TABLE tablename (
colname SERIAL
, check (colname>0 and colname < 32768));
);

Also I can't imagine this problem being common enough to justify much
work to provide a "smallserial" type etc. What about when we want a
sequence with a different increment by, start, min/max, cycle, or
cache value? Any idea for making things a bit different here should
probably address all of those possibilites.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2012-10-11 07:42:58 Re: moving from MySQL to pgsql
Previous Message Vineet Deodhar 2012-10-11 07:04:30 auto-increment field : in a simple way