You are on page 1of 20

Identity Column & Sequences

Identity Columns and Sequences


• Generate an auto-incrementing number
• While identity columns and sequences are similar in many ways,
there are also differences

2
Identity Columns and Primary Keys
• Identity columns and primary keys are two very distinct things
– Identity columns provide an auto-incrementing number
– A primary key is a constraint that guarantees each row in a table is
uniquely defined
• Quite often, identity columns are used to auto-generate the
value for primary keys, especially with surrogate keys
• A column can be an identity column without being a PK

3
Identity Column Characteristics
• Defined as a table column property - table dependent
• Cannot be shared among multiple tables
• Allowed only one identity column per table
• Automatically generates values for a single column
• Can be defined as part of a table only when the table is created
– After a table is created, the table cannot be altered to add an identity
column
– Existing identity column characteristics can be altered

4
GENERATED ALWAYS AS IDENTITY clause
• Generates a unique value for the identity column for each row
inserted into the table
• Applications are not allowed to provide their own values when
changing the contents of the table
• The DBMS issues an error if a value is specified in an INSERT
statement for the identity column
• Only columns of type SMALLINT, INTEGER, BIGINT, DECIMAL, or
NUMERIC can be created as identity columns

5
GENERATED ALWAYS AS IDENTITY clause
• Generates a value for the identity column
• Defaults starting number at 1
• An attempt to insert a value into the identity column will return
an error

6
GENERATED AS IDENTITY clause

7
GENERATED ALWAYS AS IDENTITY clause

8
IDENTITY_VAL_LOCAL function
• Returns the most recently assigned value for an identity column
where the assignment occurred as a result of a single INSERT
statement using a VALUES clause

9
SEQUENCE
• A user-defined independent system object
• Not table dependent
• Can be shared by multiple tables
• Generates sequential values that can be used in SQL statements

10
SEQUENCE
• Generates a sequence of numeric values
– Values generated in an ascending or descending order
– At a defined interval
– Can be configured to restart (cycle) when exhausted
– Can be cached and are not guaranteed to be in sequential order
– Can be defined as any integer data type
– If the data type is not specified, a sequence defaults to big integer

11
SEQUENCE

12
Sequence Characteristics
• The PREVIOUS VALUE expression returns the most recently
generated value for the specified sequence for a previous
statement within the current session
• The NEXT VALUE expression returns the next value for the
specified sequence
• The use of these expressions allows the same value to be used
across several SQL statements within several tables

13
SEQUENCE – Create Tables

14
SEQUENCE – Create Sequence

15
SEQUENCE – Insert Data for Order 1

16
SEQUENCE – Display Data
• P_ORDERS

• P_ORDER_LINES

17
SEQUENCE – Insert Data for Order 2

18
SEQUENCE – Display Data
• P_ORDERS

• P_ORDER_LINES

19
20

You might also like