Oracle Materialized Views with primary key

淺唱寂寞╮ 提交于 2019-12-02 02:52:08

问题


I created the Oracle Materialized View below:

CREATE MATERIALIZED VIEW MyMV
REFRESH COMPLETE ON DEMAND
AS

SELECT t1.*
  FROM table1 t1, table2 t2 where t1.id=t2.id;

The table1 has a primary key and the MV was created succesfully but the primary key was not created in the materialized view table.

Is there any other way to create MVs with primary keys?


回答1:


it's because your materialized view is based on two tables, if you create your view based on a single table with a primary key, then the primary key is created on you Materialized view. You can still create the index afterwards if you need one:

SQL> create table t1(id number);

Table created.

SQL> create table t2(id number);

Table created.

SQL> alter table t1 add primary key (id);

Table altered.

SQL> alter table t2 add primary key (id);

Table altered.

SQL> CREATE MATERIALIZED VIEW MyMV
REFRESH COMPLETE ON DEMAND
AS
SELECT t1.*
  FROM t1, t2 where t1.id=t2.id;  2    3    4    5

Materialized view created.

SQL> create unique index myindex on MyMV(id);

Index created.

EDIT

create a primary key instead of the unique index:

SQL> alter materialized view MyMV add constraint PK_ID primary key (id);

Materialized view altered.

SQL> alter table t3 add constraint FK_TABLE3_MyMV foreign key (id) references MyMV (id);

Table altered.


来源:https://stackoverflow.com/questions/46295406/oracle-materialized-views-with-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!