Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有主键的Oracle物化视图_Oracle_Primary Key_Materialized Views - Fatal编程技术网

具有主键的Oracle物化视图

具有主键的Oracle物化视图,oracle,primary-key,materialized-views,Oracle,Primary Key,Materialized Views,我在下面创建了Oracle物化视图: CREATE MATERIALIZED VIEW MyMV REFRESH COMPLETE ON DEMAND AS SELECT t1.* FROM table1 t1, table2 t2 where t1.id=t2.id; 表1有一个主键,MV已成功创建,但主键未在物化视图表中创建 有没有其他方法可以使用主键创建MVs?这是因为您的物化视图基于两个表,如果您基于具有主键的单个表创建视图,则主键将在您的物化视图上创建。 如果需要,您仍然可以在

我在下面创建了Oracle物化视图:

CREATE MATERIALIZED VIEW MyMV
REFRESH COMPLETE ON DEMAND
AS

SELECT t1.*
  FROM table1 t1, table2 t2 where t1.id=t2.id;
表1有一个主键,MV已成功创建,但主键未在物化视图表中创建


有没有其他方法可以使用主键创建MVs?

这是因为您的物化视图基于两个表,如果您基于具有主键的单个表创建视图,则主键将在您的物化视图上创建。 如果需要,您仍然可以在以后创建索引:

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.
编辑

创建主键而不是唯一索引:

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.

但是我需要一个主键,因为我想向这个MV表添加一个新的外键。在这种情况下,我需要创建主键而不是索引。这是一个主键。当您在创建表时使用主键语法时,它会在后台创建一个唯一索引,请进行测试。当我尝试创建FK
时,“alter table 3 add constraint FK_table 3_MyMV外键(id)引用MyMV(id);”
我遇到错误
“ORA-02270:此列列表没有匹配的唯一或主键”
。如果我更改创建主键的表,有没有办法从原始表“克隆”所有约束?