可为可更新视图指定WITH CHECK OPTION子句,以防止向select_statement中的WHERE子句不为真的行插入数据。它还防止对WHERE子句为真但更新会导致其不为真的行进行更新(换句话说,它防止将可见行更新为不可见行)。The WITH CHECK OPTION clause can be given for
an updatable view to prevent inserts to rows for which the
WHERE clause in the
select_statement is not true. It also
prevents updates to rows for which the WHERE
clause is true but the update would cause it to be not true (in
other words, it prevents visible rows from being updated to
nonvisible rows).
在可更新视图的WITH CHECK OPTION子句中,LOCAL和CASCADED关键字决定在视图以另一个视图定义时检查测试的范围。当未给出任何关键字时,默认值为CASCADED。In a WITH CHECK OPTION clause for an updatable
view, the LOCAL and CASCADED
keywords determine the scope of check testing when the view is
defined in terms of another view. When neither keyword is given,
the default is CASCADED.
WITH CHECK OPTION测试符合标准:WITH CHECK OPTION testing is
standard-compliant:
使用
LOCAL时,会检查视图的WHERE子句,然后检查会递归到底层视图,并应用相同的规则。WithLOCAL, the viewWHEREclause is checked, then checking recurses to underlying views and applies the same rules.使用
CASCADED时,会检查视图的WHERE子句,然后检查会递归到底层视图,为它们添加WITH CASCADED CHECK OPTION(仅用于检查目的;它们的定义保持不变),并应用相同的规则。WithCASCADED, the viewWHEREclause is checked, then checking recurses to underlying views, addsWITH CASCADED CHECK OPTIONto them (for purposes of the check; their definitions remain unchanged), and applies the same rules.如果没有检查选项,则不会检查视图的
WHERE子句,然后检查会递归到底层视图,并应用相同的规则。With no check option, the viewWHEREclause is not checked, then checking recurses to underlying views, and applies the same rules.
考虑以下表和视图集合的定义:Consider the definitions for the following table and set of views:
CREATE TABLE t1 (a INT); CREATE VIEW v1 AS SELECT * FROM t1 WHERE a < 2 WITH CHECK OPTION; CREATE VIEW v2 AS SELECT * FROM v1 WHERE a > 0 WITH LOCAL CHECK OPTION; CREATE VIEW v3 AS SELECT * FROM v1 WHERE a > 0 WITH CASCADED CHECK OPTION;
这里 v2 和 v3 视图是基于另一个视图 v1 定义的。Here the v2 and v3 views are
defined in terms of another view, v1.
对于 v2 的插入会根据它的 LOCAL 检查选项进行检查,然后检查会递归到 v1 并再次应用规则。v1 的规则会导致检查失败。v3 的检查也失败:Inserts for v2 are checked against its
LOCAL check option, then the check recurses to
v1 and the rules are applied again. The rules
for v1 cause a check failure. The check for
v3 also fails:
mysql>INSERT INTO v2 VALUES (2);ERROR 1369 (HY000): CHECK OPTION failed 'test.v2' mysql>INSERT INTO v3 VALUES (2);ERROR 1369 (HY000): CHECK OPTION failed 'test.v3'