Oracle Insert And Return Auto Generated Primary Key

Posted : admin On 16.12.2020

Following is a simple TRIGGER just as an example for you that inserts the primary key value in a specified table based on the maximum value of that column. You can modify the schema name, table name etc and use it. Just give it a try. /.Create a database trigger that generates automatically primary key values on the CITY table using the max. For a multiple-row insert, LASTINSERTID and mysqlinsertid actually return the AUTOINCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup.

Summary: in this tutorial, you will learn how to use the Oracle identity column to easily define an automatic generated numeric column for a table.

Introduction to Oracle identity column

Oracle 12c introduced a new way that allows you to define an identity column for a table, which is similar to the AUTO_INCREMENT column in MySQL or IDENTITY column in SQL Server.

The identity column is very useful for the surrogate primary key column. When you insert a new row into the identity column, Oracle auto-generates and insert a sequential value into the column.

To define an identity column, you use the identity clause as shown below:

First, the GENERATED keyword is mandatory.

Second, you can specify an option to generate identity values:

  • GENERATED ALWAYS: Oracle always generates a value for the identity column. Attempt to insert a value into the identity column will cause an error.
  • GENERATED BY DEFAULT: Oracle generates a value for the identity column if you provide no value. If you provide a value, Oracle will insert that value into the identity column. For this option, Oracle will issue an error if you insert a NULL value into the identity column.
  • GENERATED BY DEFAULT ON NULL: Oracle generates a value for the identity column if you provide a NULL value or no value at all.

Third, you can have a number of options for the identity column.

  • START WITH initial_value controls the initial value to use for the identity column. The default initial value is 1.
  • INCREMENT BY internval_value defines the interval between generated values. By default, the interval value is 1.
  • CACHE defines a number of values that Oracle should generate beforehand to improve the performance. You use this option for the column that has a high number of inserts.

Oracle identity column examples

Let’s take some examples of using the Oracle identity columns.

A) GENERATED ALWAYS example

The following statement creates a table named identity_demo that consists of an identity column:

The following statement inserts a new row into the identity_demo table:

Because we did not specify a value for the id column, Oracle automatically generated a sequential value starting from 1.

The following statement attempts to insert a value into the id identity column:

Oracle issued an error:

Because the id column was defined as GENERATED ALWAYS, it could not accept any provided value.

B) GENERATED BY DEFAULT example

Let’s change the id column to GENERATED BY DEFAULT:

The following statement inserts a new row into the identity_demo table:

It worked as expected. /fifa-12-cd-key-generator.html.

The following statement inserts a new row into the identity_demo table with a provided value for the id column:

In this example, Oracle used the provided value and inserted it to the table.

The following example attempts to insert a null value into the id column:

Oracle issued an error:

C) GENERATED BY DEFAULT ON NULL example

First, change the id column of the identity_demo table to GENERATED BY DEFAULT ON NULL:

The following statement provides no value for the id column, Oracle will automatically generate a value for insert:

The following statement inserts a NULL value into the id column because the id column has been defined as GENERATED BY DEFAULT ON NULL, Oracle generates a sequential value and uses it for insert:

D) START WITH option example

First, recreates the identity_demo table whose the id column is defined as identity column with the initial value starts from 100:

Second, insert a row into to the identity_demo table:

Oracle Insert And Return Auto Generated Primary Key Of India

Third, query data from the identity_demo table:

As you can see, the initial value of the id column is 100 as specified in identity clause.

E) INCREMENT BY option example

First, change the id column of the identity_demo table that includes both START WITH and INCREMENT BY options.

Second, insert two rows into the identity_demo table:

Third, query data from the table to verify the inserts:

As you can see, the first row has the id value 10. The second row has the id value 20. This is what we defined for the id column that should start with 10 and increase by 10 for the new row.

Oracle identity column restrictions

The identity columns are subject to the following restrictions:

  • Each table has one and only one identity column.
  • The data type of the identity column must be a numeric data type. the user-defined data type is not allowed to use with the identity clause.
  • The identity column is not inherited by the CREATE TABLE AS SELECTstatement.
  • The identity column cannot have another DEFAULT constraint.
  • The encryption algorithm for encrypted identity columns can be inferred therefore you should use a strong encryption algorithm.
  • The inline constraint of the identity column must not conflict with the NOT NULL and NOT DEFERRABLE constraint stated by the identity clause.

In this tutorial, you have learned how to use the Oracle identity column that allows you easily define an automatic generated numeric column for a table.

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.

Creating a Sequence

Oracle Insert And Return Auto Generated Primary Key 2017

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:

Oracle Insert And Return Auto Generated Primary Key Of Life

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

Oracle Insert And Return Auto Generated Primary Key Excel

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

Oracle Insert And Return Auto Generated Primary Key Mean

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.