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 fails if the base table does not exist or the referenced object type does not exist. CREATE VIEW … FORCE also fails if the view definition names a constraint that does not exist.
force功能解读
在 Oracle 数据库中,使用 CREATE FORCE VIEW 语句可以创建一个视图,不论该视图所引用的基本表或引用的对象类型是否存在,也不论包含视图的模式的所有者是否对它们具有权限。换句话说,使用 FORCE 选项可以强制创建视图,即使一些依赖条件未满足也可以创建成功。
下面举一个例子来说明 FORCE 的含义:
假设有两张表 employees 和 departments,分别包含员工信息和部门信息。现在我们尝试创建一个视图 employee_details,该视图将包含来自 employees 表和 departments 表的信息。
如果我们使用以下的 CREATE VIEW 语句:
CREATE VIEW employee_details AS
SELECT e.employee_id, e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
如果此时 employees 表和 departments 表存在,并且创建视图的用户有权限访问它们,那么视图会成功创建。
但是,如果在执行上述语句时,employees 表或 departments 表不存在,或者创建视图的用户没有权限访问它们,那么会出现错误,视图创建失败。
现在,如果我们使用以下的 CREATE FORCE VIEW 语句:
CREATE FORCE VIEW employee_details AS
SELECT e.employee_id, e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
即使此时 employees 表或 departments 表不存在,或者创建视图的用户没有权限访问它们,视图也会被强制创建。但需要注意,如果视图定义中包含任何约束,并且基表不存在或引用的对象类型不存在,那么依然会导致创建失败。




