ITPub博客

首页 > IT基础架构 > 网络安全 > review "with check option"

review "with check option"

原创 网络安全 作者:liroc 时间:2007-03-06 14:33:55 0 删除 编辑
The “with check option”, it is used to prevent changes to a view that would produce rows that are not included in a sub-query.[@more@]

A “with check option” is designed for updatable views whereas a “check constraint” (coinstraint_type “V”) specifies valid values for an individual column:

region_name varchar(5),
CONSTRAINT check_region CHECK (region_name in ’north’,’south’,’east’,’west’)

The “with check option”, it is used to prevent changes to a view that would produce rows that are not included in a sub-query.
For example, here we create a view that will only allows updates to dept=’finance’:

CREATE OR REPLACE VIEW finance_dept
AS
SELECT name, salary
FROM emp
WHERE dept = ‘finance’WITH CHECK OPTION;

This means only the record of employees in the finance department can be updated using this finance_dept view.
At update time we see:

insert into (select * from emp where dept = ‘finance’ with check option)
values (...);

update
(select * from
(select sal, empno from emp, dept where dept = ‘finance’)
with check option)
set sal = 100000 where ename = ‘KING’;

A violation produces:

ORA-01402: view WITH CHECK OPTION where-clause violation

With an ORA-01402, the SQL tried to INSERT or UPDATE a record in a view that contained a WITH CHECK OPTION. The resulting INSERT or UPDATE violates the WHERE clause of the view.


如果视图定义包括条件(譬如 WHERE 子句)并且其意图是确保任何引用该视图的 INSERT 或 UPDATE 语句都应用 WHERE 子句,则必须使用 WITH CHECK OPTION 定义该视图。这个选项可以确保数据库中正在修改的数据的完整性。如果在 INSERT 或 UPDATE 操作期间违反了条件,则返回 SQL 错误。

下面是一个使用了 WITH CHECK OPTION 的视图定义的示例。要确保总是检查条件,WITH CHECK OPTION 是必需的。在这种情况下,我们希望确保 DEPT 总是 10。这会限制 DEPT 列的输入值(不能更改dept的值也不能插入其他的dept值,必须要符合where语句中定义的条件)。当使用视图插入新值时,总是强制执行 WITH CHECK OPTION。

CREATE VIEW EMP_VIEW2(EMPNO,EMPNAME,DEPTNO,JOBTITLE,HIREDATE)
AS SELECT ID,NAME,DEPT,JOB,HIREDATE FROM EMPLOYEE
WHERE DEPT=10
WITH CHECK OPTION;

如果这个子句不存在,那么某个使用该视图的用户就有可能更新某条记录,使其不再属于该视图。例如,下列 SQL 语句可能会引起一些问题。
UPDATE EMP_VIEW2 SET DEPT=20 WHERE DEPT=10;

这个语句的结果是该视图现在不包含任何记录,因为部门 10 不再有雇员了。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/71191/viewspace-902980/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论
  • 博文量
    4
  • 访问量
    42767