Oracle insert with check option usage

Source: Internet
Author: User

Insert into (<select clause> with check option) values (...)

For example:

SQL >   Insert   Into ( Select   Object_id , Object_name , Object_type From Xxx Where   Object_id  <  1000   With   Check  Option  )  2   Values ( 999 , '  Testbyhao  ' , '  Testtype  ' );

This syntax looks very special. It is actually inserted into the table in subquery, but it is not allowed to be inserted if it does not meet the where condition in subquery.

If the column to be inserted is not in the where condition of the subquery check, insertion is not allowed.

If with check option is not added, it will not be checked during insertion.

Note that subquery is not actually executed.

For example:

SQL>   Insert   Into ( Select   Object_id , Object_name , Object_type From Xxx Where   Object_id  <  1000  )  2   Values ( 1001 , '  Testbyhao  ' , '  Testtype  '  );  1  Row created. SQL  >   Insert   Into ( Select   Object_id , Object_name , Object_typeFrom Xxx Where   Object_id  < 1000 With   Check   Option  )  2   Values ( 1001 , '  Testbyhao  ' , ' Testtype  '  );  Insert   Into ( Select   Object_id , Object_name , Object_type From Xxx Where   Object_id  <  1000   With   Check  Option  )  *  Error at line  1  : Ora  -  01402 : View   With   Check   Option   Where  - Clause Violation

The column inserted here does not contain object_id and cannot be inserted:

SQL >  Insert   Into ( Select   Object_name , Object_type From Xxx Where   Object_id  <  1000   With   Check   Option  )  2   Values ('  Testbyhao  ' , '  Testtype  '  );  Insert   Into ( Select   Object_name , Object_type From Xxx Where   Object_id  <  1000  With   Check   Option  )  *  Error at line  1  : Ora  -  01402 : View   With   Check   Option   Where  - Clause Violation

Why is subquery not actually executed? Check the statistics:

SQL >   Set Autotrace trace Exp  Statsql  >   Select   Object_id , Object_name , Object_type From Xxx Where   Object_id  <  1000  ; 955  Rows selected.  97  Consistent getssql  >   Insert   Into ( Select   Object_id , Object_name , Object_type From Xxx Where   Object_id  < 1000  )  2   Values ( 999 , '  Testbyhao  ' , '  Testtype  '  );  1  Row created.  1 Consistent gets

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.