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 |
-------------------------------------------------------------------------