Auto Increment Identity Column in Various Databases

We can define columns in databases which will auto increment its values whenever we add a new row. Different databases have different ways to do so and even call them differently like auto increment, identity column etc. We will discuss the auto increment for some databases here. Please feel free to add other database details that you know through the comments.

 

Auto Increment in MySQL

 The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows:

CREATE TABLE empAutoInc(

id INT PRIMARY KEY AUTO_INCREMENT,

name varchar(20));

There can be only one auto column and it must be defined as a key. You will get an error otherwise; even you can't use NOT NULL.

You can test this by inserting few rows without providing value for the auto increment column, and then doing a select:

insert into empAutoInc(name) values('abc');

insert into empAutoInc(name) values('def');

select * from empAutoInc;

Output will be:

1    abc
2    def

I have tested this with MySQL 5.0.45.

 

Auto Increment (IDENTITY) in MSSQL

The IDENTITY attribute can be used to generate a unique identity for new rows:

create table AutoEmp(

id INT IDENTITY,

name varchar(20));

Unlike MySQL auto increment (identity) field doesn't have to be defined as primary key.

You can test this by inserting few rows without providing value for the auto increment column, and then doing a select:

insert into AutoEmp values ('hjk');

insert into AutoEmp values ('asd');

select * from AutoEmp;

Unlike MySQL, you don't have to specify columns after table name and still pass only values for non-identity fields.

Output will be:

1    hjk
2    asd

I have tested this with MS SQL 2012.

 

Auto Increment in Oracle

There is no identity column in Oracle, but we can use sequence to get a similar effect.

First, create the table

create table AutoEmp (

id number(10),

name varchar2(25),

constraint pk_id PRIMARY KEY(id)

);

Then, create the sequence:

Create sequence emp_seq

start with 1

increment by 1

minvalue 1

maxvalue 100;

Now, use the sequence to insert data and then do a select to verify:

insert into AutoEmp (id,name) values(emp_seq.nextval,'HJK');

insert into AutoEmp (id,name) values(emp_seq.nextval,'HJK');

select * from AutoEmp;

I have not tested it yet myself.

 

Auto Increment in PostgreSQL

There is no identity column, but we can use sequence to get a similar effect as in Oracle.

 

Auto Increment in Sybase

The IDENTITY attribute can be used to generate a unique identity for new rows simular to MSSQL.

I have not tested it yet myself.

 

Auto Increment in Informix

You can use serial columns in Informix for the auto increment column.

create table AutoEmp (

id serial,

name varchar(20),

);

An automatic new value will only be created when if we insert a new entry without specifying the serial field, or when we insert a value "0" to serial field.

I have not tested it yet myself.

 

Auto Increment in DB2

We can use 'GENERATED ALWAYS AS IDENTITY' to generate a unique identity for new rows:

create table AutoEmp (

id INT NOT NULL GENERATED ALWAYS AS IDENTITY,

name varchar(20),

);

I have not tested it yet myself.

 

Auto Increment in Terradata

We can use 'GENERATED ALWAYS AS IDENTITY' to generate a unique identity for new rows similar to DB@:

create table AutoEmp (

id NUMERIC(15, 0) NOT NULL GENERATED ALWAYS AS IDENTITY,

name varchar(20),

);

 

Tags: 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (52) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)