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.
1 Answer 1
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.
-
1Ah, 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?Manngo– Manngo2017年02月20日 06:48:46 +00:00Commented 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 10user1822– user18222017年02月20日 06:58:32 +00:00Commented Feb 20, 2017 at 6:58
-
So, none of the databases I use myself. Thanks again!Manngo– Manngo2017年02月20日 07:00:37 +00:00Commented 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_Columnsypercubeᵀᴹ– ypercubeᵀᴹ2017年09月25日 10:45:32 +00:00Commented Sep 25, 2017 at 10:45
-
1I confirmed it works on postgres 10deFreitas– deFreitas2018年08月26日 23:09:51 +00:00Commented Aug 26, 2018 at 23:09