Create view failed with ORA-01031:insufficient privileges

简介:

有时候在ORACLE数据库创建视图时会遇到:ORA-01031:insufficient privileges错误,我也多次碰到了各种创建视图出错的情况,很多时候也没有太在意,今天被一同事问起这个问题,顺便总结一下出错的各种场景。


场景1:使用sys或system账号登陆数据库,创建dm、ods账号(授予connect、resource角色)

   1: [oracle@DB-Server ~]$ sqlplus / as sysdba
   2:  
   3: SQL*Plus: Release 10.2.0.4.0 - Production on Fri Mar 14 10:28:49 2014
   4:  
   5: Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
   6:  
   7:  
   8: Connected to:
   9: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
  10: With the Partitioning, OLAP, Data Mining and Real Application Testing options
  11:  
  12:  
  13:  
  14: SQL> create user dm identified by dm default tablespace tbs_dm_data;
  15:  
  16: User created.
  17:  
  18:  
  19:  
  20: SQL> grant connect, resource to dm;
  21:  
  22: Grant succeeded.
  23:  
  24:  
  25:  
  26: SQL> create user ods identified by ods default tablespace tbs_ods_data;
  27:  
  28: User created.
  29:  
  30: SQL> grant connect ,resource to ods;
  31:  
  32: Grant succeeded.


在另外一个窗口,以dm账号登录数据库

   1: [oracle@DB-Server bdump]$ sqlplus /nolog
   2:  
   3: SQL*Plus: Release 10.2.0.4.0 - Production on Fri Mar 14 10:35:30 2014
   4:  
   5: Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
   6:  
   7: SQL> conn dm
   8: Enter password: 
   9: Connected.

创建测试表test,并插入数据。然后创建该表对应的视图v_dm_test时报ORA-01031: insufficient privileges

   1: SQL> create table dm.test
   2:   2  (
   3:   3     name  varchar2(12)
   4:   4  );
   5:  
   6: Table created.
   7:  
   8: SQL> insert into dm.test
   9:   2  select 'kerry' from dual;
  10:  
  11: 0 rows created.
  12:  
  13: SQL> commit;
  14:  
  15: SQL> create or replace view v_dm_test
  16:   2  as 
  17:   3  select * from dm.test;
  18: create or replace view v_dm_test
  19:                        *
  20: ERROR at line 1:
  21: ORA-01031: insufficient privileges
  22:  
  23:  
  24: SQL>

结论:在这个场景出现这个错误,是因为账号dm并没有授予创建视图的权限。需要授予dm账号创建视图的权限。以sys/system等具有DBA权限的账号登陆数据库,授予dm账号创建视图的权限。

   1: sys 账号:
   2:  
   3: SQL> show user;
   4: USER is "SYS"
   5: SQL> grant create view to dm;
   6:  
   7: Grant succeeded. 
   8:  
   9: dm 账号:
  10:  
  11: SQL> show user
  12: USER is "DM"
  13: SQL> create or replace view v_dm_test
  14:   2  as 
  15:   3  select * from dm.test;
  16:  
  17: View created.

场景2:在上面的场景中,在ods账号下创建test_ods表并插入数据。然后授权select给dm用户,然后在dm用户下创建视图

   1: ods login database
   2:  
   3: SQL> show user
   4: USER is "ODS"
   5: SQL> create table ods.test_ods
   6:   2  (
   7:   3     name  varchar2(12)
   8:   4  );
   9:  
  10: Table created.
  11:  
  12: SQL> insert into ods.test_ods
  13:   2  select 'jimmy' from dual;
  14:  
  15: 1 row created.
  16:  
  17: SQL> commit;
  18:  
  19: Commit complete.
  20:  
  21: SQL> grant select on ods.test_ods to dm;
  22:  
  23: Grant succeeded.
  24:  
  25:  
  26: dm login database
  27:  
  28: SQL> conn dm
  29: Enter password: 
  30: Connected.
  31: SQL> select * from ods.test_ods;
  32:  
  33: NAME
  34: ------------
  35: jimmy
  36:  
  37: SQL> create or replace view v_ods_test
  38:   2  as 
  39:   3     select * from ods.test_ods;
  40:  
  41: View created.

先删除视图v_ods_test,然后收回用户dm创建视图的权限。

   1: sys  login database
   2: SQL> show user
   3: USER is "SYS"
   4: SQL> revoke create view from dm;
   5:  
   6: Revoke succeeded.
   7:  
   8: SQL> 

然后在dm下创建视图时会出现场景一的错误,

   1: SQL> show user
   2: USER is "DM"
   3: SQL> create or replace view v_ods_test
   4:   2  as 
   5:   3     select * from ods.test_ods;
   6: create or replace view v_ods_test
   7:                        *
   8: ERROR at line 1:
   9: ORA-01031: insufficient privileges

但是即使dm没有创建视图的权限了,我依然可以在sys用户下创建dm下视图

   1: SQL> show user;
   2: USER is "SYS"
   3: SQL> create or replace view dm.v_ods_test
   4:   2  as
   5:   3     select * from ods.test_ods;
   6:  
   7: View created.

场景3: 在上面场景中,我们依然给予DM账号创建视图的权限,然后按如下步骤去测试

   1: SQL> show user
   2: USER is "ODS"
   3: SQL> create table ods.test_view
   4:   2  (
   5:   3     name varchar2(12)
   6:   4  )
   7:   5  ;
   8:  
   9: Table created.
  10:  
  11: SQL> insert into ods.test_view
  12:   2  select 'kkk' from dual;
  13:  
  14: 1 row created.
  15:  
  16: SQL> commit;
  17:  
  18: Commit complete.


创建角色role_select_test,然后将表test_view的查询权限授予该角色,最后将该角色授予dm用户

   1: sys user login
   2:  
   3: SQL> show user
   4: USER is "SYS"
   5: SQL> create role role_select_test;
   6:  
   7: Role created.
   8:  
   9: SQL> grant select on ods.test_view to role_select_test;
  10:  
  11: Grant succeeded.
  12:  
  13: SQL> grant role_select_test to dm;
  14:  
  15: Grant succeeded.

但是在dm用户下,创建视图时报错。

   1: SQL> conn dm
   2: Enter password: 
   3: Connected.
   4: SQL> select * from ods.test_view;
   5:  
   6: NAME
   7: ------------
   8: kkk
   9:  
  10: SQL> create or replace view dm.v_ods_test2
  11:   2  as
  12:   3     select * from ods.test_view;
  13:         select * from ods.test_view
  14:                           *
  15: ERROR at line 3:
  16: ORA-01031: insufficient privileges

这时,如果显示将表ods.test_view的查询权限授予dm后,就可以创建视图。

   1: SQL> show user
   2: USER is "ODS"
   3: SQL> grant select on ods.test_view to dm;
   4:  
   5: Grant succeeded.
   6:  
   7:  
   8:  
   9: SQL> show user  
  10: USER is "DM"
  11: SQL> create or replace view dm.v_odst_test2
  12:   2  as
  13:   3     select * from ods.test_view;
  14:  
  15: View created.

结论:

创建create view 的时候,是不可以利用相应的role隐式授权的,必须显式的授予这个对象相应的权限。metalink解释如下:
    reason:Under SQL, if a user can select another user's table and has the privilege to create a view, then the create view  works. Yet, a create view on the other user's table generates ORA-01031 if the select privilege has been granted to a role and not directly.

官方文档关于创建视图的权限:

Privileges Required to Create Views

To create a view, you must meet the following requirements:

You must have been granted the CREATE VIEW (to create a view in your schema) or CREATE ANY VIEW (to create a view in another user's schema) system privilege, either explicitly or through a role.

You must have been explicitly granted the SELECT, INSERT, UPDATE, or DELETE object privileges on all base objects underlying the view or the SELECT ANY TABLE, INSERT ANY TABLE, UPDATE ANY TABLE, or DELETE ANY TABLE system privileges. You may not have obtained these privileges through roles.

Additionally, in order to grant other users access to your view, you must have received object privilege(s) to the base objects with the GRANT OPTION option or appropriate system privileges with the ADMIN OPTION option. If you have not, grantees cannot access your view."

相关文章
|
移动开发 JavaScript
H5唤起手机打电话(拨号)和发短信功能
H5唤起手机打电话(拨号)和发短信功能
917 0
|
监控 Java 测试技术
实战:Springboot集成Sentinel实现流量控制、熔断降级、负载保护
实战:Springboot集成Sentinel实现流量控制、熔断降级、负载保护
|
6月前
|
存储 SQL Java
数据存储使用文件还是数据库,哪个更合适?
数据库和文件系统各有优劣:数据库读写性能较低、结构 rigid,但具备计算能力和数据一致性保障;文件系统灵活易管理、读写高效,但缺乏计算能力且无法保证一致性。针对仅需高效存储与灵活管理的场景,文件系统更优,但其计算短板可通过开源工具 SPL(Structured Process Language)弥补。SPL 提供独立计算语法及高性能文件格式(如集文件、组表),支持复杂计算与多源混合查询,甚至可替代数据仓库。此外,SPL 易集成、支持热切换,大幅提升开发运维效率,是后数据库时代文件存储的理想补充方案。
|
10月前
|
算法 数据处理
《当朴素贝叶斯遇上模糊:解锁不确定性数据处理新姿势》
模糊朴素贝叶斯算法在处理模糊性和不确定性数据方面表现出色。它基于传统朴素贝叶斯算法,引入模糊集理论,通过隶属度处理特征的模糊性,不再要求特征独立。该算法在情感分析、医疗诊断、图像识别等领域能精准处理模糊语义和相关特征,提供更准确且具解释性的结果,为决策者提供更多有价值的信息。
260 22
|
存储 Java 调度
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
这篇文章回顾了Java中FileInputStream、FileOutputStream、FileReader和FileWriter类的基本使用方法,包括读取和写入文件的操作,以及字符流和字节流的区别和应用场景。
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
|
12月前
|
机器学习/深度学习 人工智能 算法
UCLA、MIT数学家推翻39年经典数学猜想!AI证明卡在99.99%,人类最终证伪
近日,加州大学洛杉矶分校和麻省理工学院的数学家团队成功推翻了存在39年的“上下铺猜想”(Bunkbed Conjecture),该猜想由1985年提出,涉及图论中顶点路径问题。尽管AI在研究中发挥了重要作用,但最终未能完成证明。人类数学家通过深入分析与创新思维,找到了推翻猜想的关键证据,展示了人类智慧在数学证明中的不可替代性。成果发表于arXiv,引发了关于AI在数学领域作用的广泛讨论。
378 89
|
11月前
|
人工智能 物联网 Python
VMix:即插即用!字节联合中科大推出增强模型生成美学质量的开源适配器,支持多源输入、高质量视频处理
VMix 是一款创新的即插即用美学适配器,通过解耦文本提示和交叉注意力混合控制,显著提升图像生成的美学质量,支持多源输入和高质量视频处理。
453 11
VMix:即插即用!字节联合中科大推出增强模型生成美学质量的开源适配器,支持多源输入、高质量视频处理
|
存储 NoSQL 关系型数据库
可以存储文件的数据库有哪些?
可以存储文件的数据库有哪些?
1512 6
|
C++
VS IIS Express 启动项目后,绑IP让别人可以访问你的网站
VS IIS Express 启动项目后,绑IP让别人可以访问你的网站
700 0
|
机器学习/深度学习 数据采集 搜索推荐
打造个性化新闻推荐系统
【8月更文挑战第31天】在这个信息爆炸的时代,个性化新闻推荐系统成为了连接用户与海量资讯的桥梁。本文将引导你通过Python编程语言和机器学习技术,搭建一个简单的新闻推荐模型。我们将从数据预处理开始,逐步深入到模型的训练与评估,最终实现一个能够根据用户兴趣推荐新闻的系统。无论你是编程新手还是有一定基础的学习者,这篇文章都将为你打开一扇通往智能推荐世界的大门。