这几天听一个同事说他写了一个物化视图,要求是创建一个fast on commit的。环境是在同一个实例下的两个用户间进行数据即时刷新,但是总是报了一个权限不够的错误。但是在同样环境下却可以创建on demand的物化视图。于是我就帮他看看,详细地问了一下创建的过程,发现他少了一个比较关键的步骤,就是没有将在另外用户对象的on commit refresh的权限授予给物化视图所有者,从而导致无法创建commit机制的物化视图,下面来重现一下过程:
hongsy@test>conn test/test@report
已连接。
hongsy@test>select count(*) from ttt;
COUNT(*)
----------
2
hongsy@test>desc ttt;
名称是否为空?类型
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
VAL NUMBER
PADDING VARCHAR2(100)
物化视图所依赖的源表需要有一个primary key。所以就在其上加一个。
hongsy@test>alter table ttt add constraint ttt_con primary key(id);
表已更改。
创建物化视图日志。
hongsy@test>create materialized view log on ttt with primary key;
实体化视图日志已创建。
将源表的查询权限授予给物化视图的owner
hongsy@test>grant select on ttt to test1;
授权成功。
hongsy@test>conn test1/test@report
已连接。
hongsy@test>create materialized view hhhh
2 refresh fast on commit
3 as select * from test.ttt;
as select * from test.ttt
*
ERROR位于第3行:
ORA-01031:权限不足
创建refresh on commit机制的物化视图不成功。
hongsy@test>create materialized view hhhh
2 refresh fast on demand
3 as select * from test.ttt;
实体化视图已创建。
但是创建on demand物化视图没有问题。
测试版本信息:
hongsy@test>select * from v$version;
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
PL/SQL Release 9.2.0.1.0 - Production
CORE 9.2.0.1.0 Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production
增加一个on commit refresh的权限,即可创建成功。
hongsy@test>show user
USER为"test1"
hongsy@test>conn test/test@report
已连接。
hongsy@test>grant on commit refresh on ttt to test1;
授权成功。
hongsy@test>conn test1/test@report
已连接。
hongsy@test>create materialized view pp
2 build immediate
3 refresh fast on commit
4 as select * from test.ttt;
实体化视图已创建。
总结:在同实例的两用户间的创建on commit刷新机制的物化视图需要授予一个特定的权限,不然就不能成功创建。而在分布式数据库中就不能创建on commit刷新机制的物化视图,哪怕是授予了这个特定的权限,他都会报ora-12054错误,无法为实体化视图设置on commit刷新属性。这个将在随后的《物化视图所涉及到的权限问题(2)中谈到》。
记录下来,便于查阅!