6

Is there a standardized way I can create a table in SQL with a column (lets call it ID) that is auto incremental so that I can basicly use it in all databases?

(e.g. standardized in SQL-92) If so - how? If not, why? I think auto_increment is a very often used property so I thought it is very important to standardize it...

Novellizator
  • 11,959
  • 9
  • 40
  • 62
  • http://www.w3schools.com/sql/sql_autoincrement.asp my-sql is auto increment . sql server is identity, access is AUTOINCREMENT – Gilad Feb 28 '13 at 23:27
  • 1
    Unfortunately, there is no standard for auto-increment. Oracle for example offers sequences only. You can create a `BEFORE INSERT` trigger to simulate auto-increment behaviour however there is no native implementation. [PostgreSQL is different yet again](http://stackoverflow.com/questions/787722/postgresql-autoincrement) – Phil Feb 28 '13 at 23:29

3 Answers3

2

Nope, sorry. There is AUTO_INCREMENT in MySQL, but e.g. in MS SQL this is called IDENTITY. Many things are not really standardized in SQL - and most are in the schema creating area.

It's a mess, but you can use stuff like e.g. Hibernate/NHibernate to try to use a single code base.

Nux
  • 8,068
  • 5
  • 52
  • 68
1

You can use so-called identity columns:

CREATE TABLE foo(id int GENERATED ALWAYS AS IDENTITY);

This is in the SQL standard and should be supported by PostgreSQL 10 and Oracle:

https://www.2ndquadrant.com/en/blog/postgresql-10-identity-columns/#comment-248607

Marko Knöbl
  • 140
  • 8
0

In Oracle you need to create a SEQUENCE

SQLite uses it for rowid and a synonym of it e.g. RowIdSyn INTEGER PRIMARY KEY AUTOINCREMENT

Klaas-Z4us-V
  • 141
  • 1
  • 4