5

Is there an ANSI standard for auto-numbered columns.

Currently, we have a choice of SERIAL, AUTOINCREMENT, AUTO_INCREMENT, IDENTITY() and good old NEXTVAL() among others.

I read somewhere that there is a new standard using IDENTITY which I know that Oracle has recently implemented. I know that Oracle is hardly the last word on standards.

If there is a standard, then it’s a long time coming.

asked Feb 20, 2017 at 6:21

1 Answer 1

8

Yes there is, and it's definitely not "a new standard". It's been in the SQL standard since SQL:2003

The definition is:

<identity column specification> ::=
 GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY
 [ <left paren> <common sequence generator options> <right paren> ]

The optional part with the sequence generator options lets you define the characteristics of the underlying sequence (=generator), you could do something like:

id integer GENERATED ALWAYS AS IDENTITY (start with 42 increment by 10 cycle)

This syntax is supported (at least) by PostgreSQL 10, Oracle 12.1, DB2, Apache Derby, HSQLDB, Firebird and NuoDB.

answered Feb 20, 2017 at 6:41
5
  • 1
    Ah, thank you. The SQL:2003 was just what I needed to find more information. I found this link: sigmodrecord.org/publications/sigmodRecord/0403/…. I don’t think it’s widely supported, is it? Commented Feb 20, 2017 at 6:48
  • 1
    @Manngo: I know that Oracle 12c, DB2, Derby, HSQLDB, NuoDB and Firebird support that syntax. There is some discussion of adding this to Postgres, but it doesn't seem to be on the roadmap for version 10 Commented Feb 20, 2017 at 6:58
  • So, none of the databases I use myself. Thanks again! Commented Feb 20, 2017 at 7:00
  • 1
    @a_horse_with_no_name seems like it found its way into Postgres 10!: wiki.postgresql.org/wiki/New_in_postgres_10#Identity_Columns Commented Sep 25, 2017 at 10:45
  • 1
    I confirmed it works on postgres 10 Commented Aug 26, 2018 at 23:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.