Showing posts with label identity. Show all posts
Showing posts with label identity. Show all posts

Thursday, August 28, 2014

Some 12C Features

Will try to describe some of the 12C features and will keep editing this post.

Note:
  • If you want to install Oracle 12 on Windows 7, refer the steps here 
  • To troubleshoot invalid common user or role name after installation, go here

Feature List

 Identity Columns:
  • Also known as auto-number column. This implies that the data in the column should increase when data is inserted).
  • Read article on the same here.

 Invisible Columns:
  • Columns can be made invisible
  • It gives one the ability to introduce a change while minimizing any negative side effects of that change.
  • Read article on the same here 
With Clause:
  • Instead of using a stored PL/SQL function, we can include the body of the function in the SQL query
  • Read article on the same here 
 Truncate statements with the cascade option
  • We could not truncate table with foreign key constraints.
  • Now in 12c, one can execute the truncate table command with the cascade option (to cascade through the child tables).
  • Note: You would need to create the foreign key with the "on delete cascade" option
Columns can have a default value in place of NULL
  • You can define a column to have a default value when NULL is inserted
  • e.g. create table ......  product_id NUMBER ON NULL 0,.....
  • So if one explicity inserts NULL, it will be populated by value 0
Multiple Index - same columns
  • You can create multiple indexes (different types) on a same set of columns.
  • Assume you need to change from B*tree to Bitmap or vice versa.
  • You need not drop the index.
  • Just make the first index invisible and make a new index.
  • Conditions:
    • Different types of indexes
    • Different uniqueness
    • Different partitions are used
 



Thursday, August 14, 2014

12 C Feature - Identity Columns

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:
  • 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"