Oracle

通過送出時刷新物化視圖對具有非唯一值的表進行 FK?

  • February 13, 2018

我想為沒有唯一值的表創建一個 FK。所以我不能創建一個簡單的 FK。

$$ to clarify, the parent table has duplicates - dont ask why, not in my control $$ 我嘗試創建一個物化視圖:

CREATE MATERIALIZED VIEW UniqueView
REFRESH ON COMMIT 
AS SELECT distinct currency from currencies

.. 然後為 UniqueView 創建一個 FK。但是,這可行,因為使用了“不同”功能,所以我不能使用 ON COMMIT 刷新。

我希望物化視圖始終與基礎表保持同步。

還有其他方法可以實現嗎?提前謝謝了!

您仍然可以使用GROUP BY.

create table t1 (c1 varchar2(3), c2 number);
insert into t1 values ('EUR', 100);
insert into t1 values ('EUR', 200);
insert into t1 values ('USD', 150);
commit;

select * from t1;
C1          C2
--- ----------
EUR        100
EUR        200
USD        150

create materialized view log on t1 
 with rowid (c1) including new values;

create materialized view v1
refresh fast on commit as
select c1 from t1 group by c1;

select *from v1;
C1
---
EUR
USD

insert into t1 values ('CHF', 180);
commit;

SQL> select * from v1;
C1
---
EUR
USD
CHF

引用自:https://dba.stackexchange.com/questions/197807