How to create virtual columns and composite triggers for Oracle databases

Source: Internet
Author: User

This article mainly introducesOracle DatabaseOfVirtual ColumnAndCompound triggerRelated knowledge, including the creation of virtual columns and composite triggers, and examples of creation are provided. Let's take a look at this process.

I. Virtual Columns

Oralce's virtual column solves a lot of data information that can be generated only when triggers or code is used for calculation and statistics. In the past, when other columns were counted and new columns were generated, the new columns were added through Statistical Computation in the select statement, resulting in low execution efficiency, in addition, the query SQL statements become lengthy and complex and prone to errors. It seriously reduces the development efficiency and program execution efficiency. The introduction of Oralce virtual columns solves this problem.

There are also some problems with the virtual columns of Oralce. The insert into talbe_name values (). statement cannot be used. When adding data to a table containing virtual columns, the column name of the added table must be written in the insert statement. The insert into table_name (list1, list2,... listend) column name cannot contain virtual column names. Otherwise, an error is prompted.

The instance for creating a virtual column is as follows:

 
 
  1. Create table sales
  2.  
  3. (
  4.  
  5. Sales_id number,
  6.  
  7. Cust_id number,
  8.  
  9. Sales_amt number,
  10.  
  11. Sale_category varchar2 (6)/* virtual column name and data type */
  12.  
  13. Generated always
  14.  
  15. (
  16.  
  17. Case when sales_amt <= 10000 then 'low'
  18.  
  19. When sales_amt> 10000 and sales_amt <= 100000 then 'medium'
  20.  
  21. When sales_amt> 100000 and sales_amt <= 1000000 then 'high' else' ULTRA 'end
  22.  
  23. ) Virtual/* virtual column value function content */
  24.  
  25. );

The virtual column is specified as "generated always as", which means that the column value is generated at runtime rather than stored as part of the table. The clause is followed by a detailed CASE statement to calculate the value. Finally, the fact that "virtual" is specified to enhance this is a virtual column.

Ii. Compound triggers

Composite triggers are actually four different triggers defined as a whole. For example, the UPDATE composite trigger combines before statement, before row, after statement, and after row into a composite trigger. This is a single code snippet, so you can pass variables like any other single PL/SQL code.

Here is an example to help illustrate. Note: A sales_log table with the same sales table is created.

 
 
  1. Create or replace trigger tri_sales
  2.  
  3. For update of sales_amt on SALES
  4.  
  5. Compound trigger
  6.  
  7. Type ty_sales_log is table of sales_log % rowtype
  8.  
  9. Index by pls_integer;
  10.  
  11. Coll_sales_log ty_sales_log;
  12.  
  13. Ctr pls_integer: = 0;
  14.  
  15. Before statement is
  16.  
  17. Begin
  18.  
  19. Dbms_output.put_line ('in before statement ');
  20.  
  21. End before statement;
  22.  
  23. Before each row is
  24.  
  25. Begin
  26.  
  27. Dbms_output.put_line ('in before each row ');
  28.  
  29. End before each row;
  30.  
  31. After each row is
  32.  
  33. Begin
  34.  
  35. Ctr: = ctr + 1;
  36.  
  37. Dbms_output.put_line ('in after each row. sales_amt '|: new. sales_amt );
  38.  
  39. Coll_sales_log (ctr). sales_id: =: old. sales_id;
  40.  
  41. Coll_sales_log (ctr). cust_id: =: old. cust_id;
  42.  
  43. Coll_sales_log (ctr). sales_amt: =: new. sales_amt;
  44.  
  45. End after each row;
  46.  
  47. After statement is
  48.  
  49. Begin
  50.  
  51. Dbms_output.put_line ('in after statement ');
  52.  
  53. Forall counter in 1 .. coll_sales_log.count ()
  54.  
  55. Insert into sales_log (sales_id, cust_id, sales_amt)
  56.  
  57. Values (coll_sales_log (counter). sales_id, coll_sales_log (counter). cust_id, coll_sales_log (counter). sales_amt );
  58.  
  59. End after statement;/* The Insert into sales_log values cannot be used.
  60.  
  61. (Coll_sales_log (counter); add */
  62.  
  63. End tri_sales;

The above example contains four different trigger forms, which are executed independently.

Note that:If a virtual Column exists in the sales_log table, Insert into sales_log values (coll_sales_log (counter) cannot be used. To add a virtual column directly, you must specify the column name in the table. Otherwise, an error occurs.

This article introduces the knowledge of Oracle Database Virtual columns and composite triggers. If you want to learn more about Oracle databases, please visit http://database.51cto.com/oracle/, and I believe you can bring them to us!

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.