Oracle - FORCE 视图选项在执行后未显示在视图中

2021-09-17 00:00:00 oracle view

我们创建了一个用于处理复杂 SQL 的视图,但是在使用 FORCE 选项.例如:

create or replace FORCE view testview as选择 1从双重;

FORCE 稍后查看视图时移除

创建或替换视图测试视图为选择 1从双重;

<块引用>

如果您想创建视图,而不管视图的基表或引用的对象类型是否存在,或者包含视图的架构的所有者是否对它们具有特权,请指定 FORCE.在对视图发出任何 SELECT、INSERT、UPDATE 或 DELETE 语句之前,这些条件必须为真.

如果视图定义包含任何约束,如果基表不存在或引用的对象类型不存在,CREATE VIEW ... FORCE 将失败.如果视图定义命名了一个不存在的约束,CREATE VIEW ... FORCE 也会失败.

是否仅在执行时使用 FORCE 选项而没有运行时影响?它是否仅用于强制覆盖先前的视图(即使出现错误)?

解决方案

是否仅在执行时使用 FORCE 选项而没有运行时影响?

是的.如果视图语句无效 - 说有一个错字并且 FROM 子句中的表名拼写错误 - 通常 Oracle 不会创建视图.通过使用 FORCE 关键字,视图被创建但具有 'INVALID' 状态.这在某些情况下很有用,例如我们有一个构建工具,它会在语句失败时停止运行,但我们希望整个构建完成,然后找出任何不一致之处.

<块引用>

是否仅用于强制覆盖之前的视图

没有.即使在此之前视图不存在,我们也可以选择使用 FORCE.当我们使用依赖项 ALTER 一个 TYPE 时,用法与 FORCE 的用法略有不同.我们可以随时创建或替换视图.

We created a view for handling complex SQL, But when creating a view using FORCE option. e.g:

create or replace FORCE view testview as
select 1
from dual;

FORCE is removed when viewing the view later

create or replace view testview as
select 1
from dual;

Specify FORCE if you want to create the view regardless of whether the base tables of the view or the referenced object types exist or the owner of the schema containing the view has privileges on them. These conditions must be true before any SELECT, INSERT, UPDATE, or DELETE statements can be issued against the view.

If the view definition contains any constraints, CREATE VIEW ... FORCE will fail if the base table does not exist or the referenced object type does not exist. CREATE VIEW ... FORCE will also fail if the view definition names a constraint that does not exist.

Is using FORCE option is only in execution time with no runtime effects? and is it only used for force overriding the previous view (even on error)?

解决方案

Is using FORCE option is only in execution time with no runtime effects?

Yes. If the view statement is invalid - say there's a typo and the table name in the FROM clause is misspelled - normally Oracle wouldn't create the view. By using the FORCE keyword the view is created but with an 'INVALID' status. This can be useful in certain situations, e.g. we have a build tool which stops running if a statement fails but we would prefer the whole build to finish then sort out any inconsistencies.

is it only used for force overriding the previous view

No. We can choose to use FORCE even if the view doesn't exist prior to this moment. The usage is slightly different from the use of FORCE when we ALTER a TYPE with dependencies. We can always create or replace a view.

相关文章