oracle - 创建带有主键的 View

标签 oracle oracle10g ddl

这个问题的意思是重复的,我仍然必须澄清它。特别是 Oracle 文档 says可以在 CREATE VIEW 子句中指定主键(11g 文档具有相同的概念)。然而,当我尝试这样做时:

create or replace view ABC(A, B, C, CONSTRAINT A_PK PRIMARY KEY (A)) ....

我得到ORA-00922:缺少或无效选项指向“主键”短语。问题是,是我的问题还是Oracle文档有问题?

最佳答案

简单的答案是您的语法不正确。您必须指定DISABLE

NOVALIDATE 禁用主键验证,在 View 中这是默认设置,因此会自动包含在内;但如果您使用它,就会更清楚,因为在一个可爱的双重否定中,disable novalidate 禁用了禁用主键的功能。

依赖是可选的;它指定创建 View 时是否考虑主键。 rely 的反义词是 norely

创建 View 约束有很多限制,并且由于它依赖于下表,因此正如 @RC 已经指出的那样,它并不真正值得。但如果您仅需要它作为文档,那么您可以:

SQL> create table tmp_test ( a number(10), b varchar2(120) );

Table created.

SQL>
SQL> insert into tmp_test
  2   select level, 'b'
  3     from dual
  4  connect by level <= 20
  5          ;

20 rows created.

SQL>  commit ;

Commit complete.

SQL>
SQL> alter table tmp_test
  2    add constraint tmp_test_pk
  3        primary key (a)
  4        using index;

Table altered.

SQL>
SQL> create or replace view v_tmp_test (a, b
  2     , constraint v_tmp_test_pk primary key (a) rely disable novalidate) as
  3   select a, b
  4     from tmp_test
  5          ;

View created.

SQL>

来自documentation :

View Constraints

Oracle does not enforce view constraints. However, operations on views are subject to the integrity constraints defined on the underlying base tables. This means that you can enforce constraints on views through constraints on base tables.

Notes on View Constraints View constraints are a subset of table constraints and are subject to the following restrictions:

You can specify only unique, primary key, and foreign key constraints on views. However, you can define the view using the WITH CHECK OPTION clause, which is equivalent to specifying a check constraint for the view.

View constraints are supported only in DISABLE NOVALIDATE mode. You cannot specify any other mode. You must specify the keyword DISABLE when you declare the view constraint. You need not specify NOVALIDATE explicitly, as it is the default.

The RELY and NORELY parameters are optional. View constraints, because they are unenforced, are usually specified with the RELY parameter to make them more useful. The RELY or NORELY keyword must precede the DISABLE keyword. Please refer to "RELY Clause" for more information.

Because view constraints are not enforced directly, you cannot specify INITIALLY DEFERRED or DEFERRABLE.

You cannot specify the using_index_clause, the exceptions_clause clause, or the ON DELETE clause of the references_clause.

You cannot define view constraints on attributes of an object column.

关于oracle - 创建带有主键的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160182/

相关文章:

java - 使用 Weblogic 10.3 记录 JPA SQL

oracle - 在 Microsoft Windows 2008 Server 64 位上安装 Oracle 10 ODP.NET

SQL Server DDL 脚本为数据库中的每个表追加(或删除)同一组列?

mysql - 如何指定表的字段的值只能取自一组特定的值?

sql - Oracle 解释计划中的成本后缀

自治事务中的 Oracle DDL

oracle - 破折号导致 DBI 中的 SQL 问题

sql - Oracle中是否需要 'as'关键字来定义别名?

sql - 添加索引后查询性能下降

Oracle - 在单个查询中删除多个表