加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – 使用主键创建视图

发布时间:2020-12-12 12:42:54 所属栏目:百科 来源:网络整理
导读:这个问题在意义上是重复的,我还是要澄清一下. Oracle文档特别指出 says可以在CREATE VIEW子句中指定主键(11g docs具有相同的概念).然而,当我尝试这样做时: create or replace view ABC(A,B,C,CONSTRAINT A_PK PRIMARY KEY (A)) .... 我得到ORA-00922:指向
这个问题在意义上是重复的,我还是要澄清一下. Oracle文档特别指出 says可以在CREATE VIEW子句中指定主键(11g docs具有相同的概念).然而,当我尝试这样做时:

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

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

解决方法

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

NOVALIDATE禁用主键验证,在视图中这是默认值,因此自动包含;但是如果你使用它会更清楚,因为在可爱的双重否定中,禁用novalidate会禁用禁用主键的功能.

依赖是可选的;它指定在创建视图时是否考虑主键.依赖的反义词是非常的.

创建视图约束有很多限制,因为它依赖于下表,它并不像@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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读