What is the use and syntax of SEQUENCE in DB2?


A sequence is used to generate the unique ID automatically. We can define a column in the DB2 table as type SEQUENCE. This will enable DB2 to automatically generate a unique ID for every new row inserted in the table. The auto ID generated by the DB2 remains unique throughout the database.

For example, the practical implementation of SEQUENCE comes in case there are separate ORDERS tables assigned for each region North, East, West and South, then we can declare ORDER_ID as SEQUENCE. This will always generate a unique value for ORDER_ID irrespective of the ORDER table assigned for North, East, West and South.

The above SEQUENCE can be defined as follows.

Example

CREATE SEQUENCE ORDER_SEQ AS INTEGER
   START WITH 1
   INCREMENT BY 1
   NOMAXVALUE;

Updated on: 30-Nov-2020

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements