带主键的 Oracle 物化 View

标签 oracle primary-key materialized-views

我在下面创建了 Oracle 物化 View :

CREATE MATERIALIZED VIEW MyMV
REFRESH COMPLETE ON DEMAND
AS

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

table1有主键,mv创建成功,但物化 View 表中没有创建主键。

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

最佳答案

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

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.

关于带主键的 Oracle 物化 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46295406/

相关文章:

oracle - 是否可以部分刷新 Oracle 中的物化 View ?

.net - 使用 NHibernate 加速批量插入操作

php - 外键约束

postgresql - 将 SHA1 签名作为主键存储在 Postgresql 中

mysql - 如何在sql中添加同时具有字符串和自动递增数字的ID

database - 许多物化 View 的自动刷新

postgresql - 重命名 View 中的列 : bug or feature?

sql - 在SQL中将列转换为行

Java Web 应用程序和数据库设计

oracle - 如何仅显示错误消息Oracle引发应用程序错误