http://stackoverflow.com/questions/4668146/how-to-add-a-primary-key-on-a-oracle-view
It allows them, but only in a disabled state. They provide information to the optimiser but cannot be enforced. – Tony Andrews Jan 12 '11 at 12:55 | |
| Exactly what Tony said. And some tools that do object-relational mapping want those disabled primary and foreign keys to exist for views, or they generate horrid execution plans. We had that experience with one of the versions of Hibernate, don't remember which one. – Jim Hudson Jan 12 '11 at 15:58 |
The only thing that comes in my mind is using a materialized view and then create a unique index on it:
drop materialized view tq84_mat_view;
drop table tq84_table;
create table tq84_table (
a number,
b number
);
create materialized view tq84_mat_view
refresh on commit as
select
a,
sum(b) sum_b
from
tq84_table
group by
a;
create unique index tq84_mat_view_uix on tq84_mat_view (sum_b);
insert into tq84_table values (1, 1);
insert into tq84_table values (2, 2);
insert into tq84_table values (1, 4);
commit;
insert into tq84_table values (2, 3);
commit;
--> ORA-12008: error in materialized view refresh path
--> ORA-00001: unique constraint (SPEZMDBA.TQ84_MAT_VIEW_UIX) violated
drop materialized view tq84_mat_view;
drop table tq84_table;
create table tq84_table (
a number,
b number
);
create materialized view tq84_mat_view
refresh on commit as
select
a,
sum(b) sum_b
from
tq84_table
group by
a;
create unique index tq84_mat_view_uix on tq84_mat_view (sum_b);
insert into tq84_table values (1, 1);
insert into tq84_table values (2, 2);
insert into tq84_table values (1, 4);
commit;
insert into tq84_table values (2, 3);
commit;
--> ORA-12008: error in materialized view refresh path
--> ORA-00001: unique constraint (SPEZMDBA.TQ84_MAT_VIEW_UIX) violated
While this might be useful, it must be kept in mind that the materialized view, as opposed to a "normal" view occupies space in a tablespace. And of course, the index needs space, too.