zoukankan      html  css  js  c++  java
  • insert into (select...WITH CHECK OPTION) values(...)

    insert into (<subquery> WITH CHECK OPTION) values (...)

    语法看起来很特殊,其实是insert进subquery的这张表里:

    1. 只有插入的值在subquery列表中,且满足where条件时才能插入成功。

    2. 如果不满足subquery里的where条件的话,就不允许插入。

    3. 如果插入的列不在subquery里,那么也会不允许插入。

    4. 如果不加WITH CHECK OPTION则在插入时不会检查。

    5. subquery其实是不会实际执行的。

    //满足条件,成功插入

    SQL> insert into (select deptno,dname from dept where deptno>50 with check option) values (90,'Tough');
    已创建 1 行。

    //deptno=45不满足where子句条件

    SQL> insert into (select deptno,dname from dept where deptno>50 with check option) values (45,'Tough');

    insert into (select deptno,dname from dept where deptno>50 with check option) values (45,'Tough')
                                          *
    第 1 行出现错误:
    ORA-01402: 视图 WITH CHECK OPTIDN where 子句违规

    //插入的值不在subquery列表中,多值或少值都不行

    SQL> insert into (select deptno,dname from dept where deptno>50 with check option) values (90,'Tough','TH');

    insert into (select deptno,dname from dept where deptno>50 with check option) values (90,'Tough','TH')
                *
    第 1 行出现错误:
    ORA-00913: 值过多


    SQL> insert into (select deptno,dname from dept where deptno>50 with check option) values (90);

    insert into (select deptno,dname from dept where deptno>50 with check option) values (90)
                *
    第 1 行出现错误:
    ORA-00947: 没有足够的值

    //不加WITH CHECK OPTION则在插入时不会检查

    SQL> insert into (select deptno,dname from dept where deptno>50) values (45,'Tough');
    已创建 1 行。

    //subquery其实是不会实际执行的。从执行计划可以看出来

    SQL> insert into (select deptno,dname from dept where deptno>50 with check option) values (91,'Tough');
    已创建 1 行。

    执行计划
    ----------------------------------------------------------

    -------------------------------------------------------------------------
    | Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    -------------------------------------------------------------------------
    |   0 | INSERT STATEMENT |      |     1 |    20 |     1   (0)| 00:00:01 |
    -------------------------------------------------------------------------

     


     

     

  • 相关阅读:
    解题:POI 2006 Periods of Words
    解题:NOI 2014 动物园
    1483. 最高平均分
    1438. 较大分组的位置(回顾)
    1258. 漂亮子数组
    1903. 部门统计(回顾)
    1509. 柠檬水找零
    1451. 到最近的人的最大距离
    1425. 比较含退格的字符串
    1394. 山羊拉丁文
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778799.html
Copyright © 2011-2022 走看看