New Features of Oracle 11g: Virtual Columns

Source: Internet
Author: User

Today, I will explain the new features of Oracle 11g Database-virtual Columns

Introduction

In the old Oracle version, when we need expressions or some calculation formulas, we will create a database view. If we need to use indexes on this view, we will create a function-based index.

Oracle 11g now allows us to store expressions directly on tables using virtual columns.

Let's look at a simple example:

 
 
  1. CREATE TABLE EMP  
  2. (  
  3.   EMPNO     NUMBER(6),  
  4.   SAL       NUMBER(8,2),  
  5.   COMM      NUMBER(8,2),  
  6.   SAL_PACK  GENERATED ALWAYS AS ( SAL + NVL(COMM,0) ) VIRTUAL  
  7. ); 

The preceding VIRTUAL column SAL_PACK is created by a simple expression. The keywords used include VIRTUAL, but this keyword is optional ), the value of this field is calculated by the COMM field through the expression.

Virtual column values are not stored on disks. They are calculated temporarily based on the defined expression during query.

We cannot insert data into a virtual column:

 
 
  1. SQL> INSERT INTO emp VALUES (10, 1500, 500,2000);  
  2.  
  3. ERROR at line 1:  
  4. ORA-54013: INSERT operation disallowed on virtual columns 

We cannot implicitly add data to a virtual column either:

 
 
  1. SQL> INSERT INTO t VALUES (10, 1500, 500);  
  2.             *  
  3. ERROR at line 1:  
  4. ORA-00947: not enough values  

We can only use physical columns to insert data.

 
 
  1. SQL> INSERT INTO t (empno, sal,comm) VALUES (10, 1500 , 500);  
  2. 1 row created. 

You can then query the values of virtual columns:

 
 
  1. SQL> select * from emp;  
  2. EMPNO    SAL        COMM       SAl_PACK  
  3. -----   ------      -----      --------  
  4. 10      1500        500        2000  
  5.  
  6. 1 row selected. 

The expression is calculated in real time during query, and the above results are output.

Indexes and constraints of virtual Columns

Indexes and constraints can also be applied to virtual columns. You can use the following SQL statement to create an index:

 
 
  1. SQL> create index sal_pack_idx on emp(sal_pack);  
  2.            Index Created. 

You can also create a foreign key for a virtual column.

Use PLSQL functions to process virtual Columns

The PLSQL function can be used to define virtual columns, but the function must be determined:

 
 
  1. CREATE OR REPLACE FUNCTION sum_num (in_num1 NUMBER, in_num2 NUMBER)  
  2.    RETURN NUMBER DETERMINISTIC  
  3. AS 
  4. BEGIN 
  5.    RETURN in_num1 + in_num2;  
  6. END; 

You can then use the above functions in the virtual column:

 
 
  1. SQL>ALTER TABLE emp ADD sal_pack_temp GENERATED ALWAYS AS ( sum_num(SAL,COMM) ):  
  2. Table Altered 

Comments of virtual Columns

To create a comment for a virtual column:

 
 
  1. SQL> COMMENT ON COLUMN emp.sal_pack IS 'Virtual column [sal+ comm]';  
  2. Comment created. 

In the above example, the virtual column function is much simpler than the view itself.

I hope this will also work for you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.