Oracle INSERT WITH CHECK OPTION的用法

來源:互聯網
上載者:User

insert into (<select clause> WITH CHECK OPTION) values (...)

例如:

SQL> insert into (select object_id,object_name,object_type from xxx where object_id<1000 WITH CHECK OPTION)2 values(999,'testbyhao','testtype');

這樣的文法看起來很特殊,其實是insert進subquery裡的這張表裡,只不過如果不滿足subquery裡的where條件的話,就不允許插入。

如果插入的列有不在subquery作為檢查的where條件裡,那麼也會不允許插入。

如果不加WITH CHECK OPTION則在插入時不會檢查。

這裡注意,subquery其實是不會實際執行的。

例如:

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_type from xxx where object_id<1000with 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

這裡插入的列中沒有object_id,也是不允許插入的:

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

為什麼說subquery沒有實際執行呢?看統計資訊吧:

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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.