Note: Index article on some 12c features can be seen here
Identity Columns
What is an Identity column?
This is also known as auto-number column. This implies that the data in the column should increase when data is inserted.
In previous version of Oracle 12c, this was accomplished in the one of the following two ways:
Trigger created:
CREATE OR REPLACE TRIGGER test_trg
BEFORE INSERT ON testseq
FOR EACH ROW
BEGIN
SELECT test_seq.nextval INTO :new.id FROM dual;
END;
In Oracle 12c, the same thing can be achieved without the use of sequence and triggers in two ways:
Column default clause (with sequences)
Create a sequence and use it as part of the table creation.
One can specify sequences (NEXTVAL or CURRVAL) as default column values. When a new row is inserted into the table, the sequence value is automatically inserted.
Look at the screen shot below where I have created a sequence and a table.
You can see that we can also put in out own data (data with value like 100 in the example or null) and it overrides the value from the sequence.
I have made the column not null, and we can see on inserting null, it throws an error.
Using GENERATED as IDENTITY in Column definition:
Here I will show example of identity columns which can be specified in the CREATE TABLE and ALTER TABLE statements.
Create a table using the IDENTITY clause.
Create table test_identity
(
id NUMBER GENERATED as IDENTITY,
name VARCHAR2(100)
);
When you do a describe, you shall see that the column is by default NOT NULL;
Running a query on DBA_SEQUENCES shows a new SEQUENCE created by the name ISEQ$$_92446 (system-generated name in the format ISEQ$$_<objectID> where objectID is the object id of the table)
Also note that you cannot alter an existing non-identity column to become an identity column.
Difference between "Column default clause" and "Using GENERATED as IDENTITY"
Identity Columns
What is an Identity column?
This is also known as auto-number column. This implies that the data in the column should increase when data is inserted.
In previous version of Oracle 12c, this was accomplished in the one of the following two ways:
- Create a sequence
- Insert the sequence.nextval in the INSERT STATEMENT
OR
- Create a sequence
- Create a Trigger "BEFORE INSERT ON TABLE"
Trigger created:
CREATE OR REPLACE TRIGGER test_trg
BEFORE INSERT ON testseq
FOR EACH ROW
BEGIN
SELECT test_seq.nextval INTO :new.id FROM dual;
END;
In Oracle 12c, the same thing can be achieved without the use of sequence and triggers in two ways:
Column default clause (with sequences)
Create a sequence and use it as part of the table creation.
One can specify sequences (NEXTVAL or CURRVAL) as default column values. When a new row is inserted into the table, the sequence value is automatically inserted.
Look at the screen shot below where I have created a sequence and a table.
You can see that we can also put in out own data (data with value like 100 in the example or null) and it overrides the value from the sequence.
I have made the column not null, and we can see on inserting null, it throws an error.
Using GENERATED as IDENTITY in Column definition:
Here I will show example of identity columns which can be specified in the CREATE TABLE and ALTER TABLE statements.
Create a table using the IDENTITY clause.
Create table test_identity
(
id NUMBER GENERATED as IDENTITY,
name VARCHAR2(100)
);
When you do a describe, you shall see that the column is by default NOT NULL;
Running a query on DBA_SEQUENCES shows a new SEQUENCE created by the name ISEQ$$_92446 (system-generated name in the format ISEQ$$_<objectID> where objectID is the object id of the table)
Also note that you cannot alter an existing non-identity column to become an identity column.
Difference between "Column default clause" and "Using GENERATED as IDENTITY"
No comments:
Post a Comment