Db2 Primary Key Auto Generated

Much to the frustration of database administrators worldwide, prior to Oracle version 12c in mid-2014, Oracle simply had no inherent ability to inherently generate auto incrementing columns within a table schema. While the reasons for this design decision can only be guessed at, the good news is that even for users on older Oracle systems, there is a possible workaround to circumnavigate this pitfall and create your own auto incremented primary key column.

How can you tell if a table in DB2 has a Primary Key? Tables in our DB2 database to have a Primary Key defined. Vs auto incrementing integer as Primary key. Auto numbering and identifier columns An identity column provides a way for DB2® to automatically generate a unique numeric value for each row that is added to the table. When creating a table in which you must uniquely identify each row that will be added to the table, you can add an identity column to the table.

Creating a Sequence

The first step is to create a SEQUENCE in your database, which is a data object that multiple users can access to automatically generate incremented values. As discussed in the documentation, a sequence in Oracle prevents duplicate values from being created simultaneously because multiple users are effectively forced to “take turns” before each sequential item is generated.

For the purposes of creating a unique primary key for a new table, first we must CREATE the table we’ll be using:

Next we need to add a PRIMARY KEY constraint:

Finally, we’ll create our SEQUENCE that will be utilized later to actually generate the unique, auto incremented value.

Adding a Trigger

While we have our table created and ready to go, our sequence is thus far just sitting there but never being put to use. This is where TRIGGERS come in.

Similar to an event in modern programming languages, a TRIGGER in Oracle is a stored procedure that is executed when a particular event occurs.

Typically a TRIGGER will be configured to fire when a table is updated or a record is deleted, providing a bit of cleanup when necessary.

In our case, we want to execute our TRIGGER prior to INSERT into our books table, ensuring our SEQUENCE is incremented and that new value is passed onto our primary key column.

Here we are creating (or replacing if it exists) the TRIGGER named books_on_insert and specifying that we want the trigger to fire BEFORE INSERT occurs for the books table, and to be applicable to any and all rows therein.

The ‘code’ of the trigger itself is fairly simple: We SELECT the next incremental value from our previously created books_sequenceSEQUENCE, and inserting that into the :new record of the books table in the specified .id field.

Note: The FROM dual part is necessary to complete a proper query but is effectively irrelevant. The dual table is just a single dummy row of data and is added, in this case, just so it can be ignored and we can instead execute the system function of our trigger rather than returning data of some kind.

IDENTITY Columns

IDENTITY columns were introduced in Oracle 12c, allowing for simple auto increment functionality in modern versions of Oracle.

Using the IDENTITY column is functionally similar to that of other database systems. Recreating our above books table schema in modern Oracle 12c or higher, we’d simply use the following column definition.

AUTO INCREMENT Field

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.

Primary

Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Syntax for MySQL

The following SQL statement defines the 'Personid' column to be an auto-increment primary key field in the 'Persons' table:

CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:

Alter Primary Key Db2

To insert a new record into the 'Persons' table, we will NOT have to specify a value for the 'Personid' column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen');

The SQL statement above would insert a new record into the 'Persons' table. The 'Personid' column would be assigned a unique value. The 'FirstName' column would be set to 'Lars' and the 'LastName' column would be set to 'Monsen'.

Syntax for SQL Server

The following SQL statement defines the 'Personid' column to be an auto-increment primary key field in the 'Persons' table:

CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.

In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.

Tip: To specify that the 'Personid' column should start at value 10 and increment by 5, change it to IDENTITY(10,5).

To insert a new record into the 'Persons' table, we will NOT have to specify a value for the 'Personid' column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen');

The SQL statement above would insert a new record into the 'Persons' table. The 'Personid' column would be assigned a unique value. The 'FirstName' column would be set to 'Lars' and the 'LastName' column would be set to 'Monsen'.

Syntax for Access

The following SQL statement defines the 'Personid' column to be an auto-increment primary key field in the 'Persons' table:

Key
CREATE TABLE Persons (
Personid AUTOINCREMENT PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);

The MS Access uses the AUTOINCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each new record.

Db2 Create Primary Key Syntax

Tip: To specify that the 'Personid' column should start at value 10 and increment by 5, change the autoincrement to AUTOINCREMENT(10,5).

To insert a new record into the 'Persons' table, we will NOT have to specify a value for the 'Personid' column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen');

The SQL statement above would insert a new record into the 'Persons' table. The 'Personid' column would be assigned a unique value. The 'FirstName' column would be set to 'Lars' and the 'LastName' column would be set to 'Monsen'.

Syntax for Oracle

In Oracle the code is a little bit more tricky.

You will have to create an auto-increment field with the sequence object (this object generates a number sequence).

Db2 Add Primary Key

Use the following CREATE SEQUENCE syntax:

CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;

The code above creates a sequence object called seq_person, that starts with 1 and will increment by 1. It will also cache up to 10 values for performance. The cache option specifies how many sequence values will be stored in memory for faster access.

To insert a new record into the 'Persons' table, we will have to use the nextval function (this function retrieves the next value from seq_person sequence):

INSERT INTO Persons (Personid,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');

The SQL statement above would insert a new record into the 'Persons' table. The 'Personid' column would be assigned the next number from the seq_person sequence. The 'FirstName' column would be set to 'Lars' and the 'LastName' column would be set to 'Monsen'.

Db2 Primary Key Auto Generated Tool