1、Oracle 用户及角色 介绍 一. 用户管理1.1 建立用户(数据库验证)CREATE USER DAVE IDENTIFIED BY pwdDEFAULT TABLESPACE usersTEMPORARY TABLESPACE tempQUOTA 5m ON users;1.2 修改用户ALTER USER DAVE QUOTA 0 ON SYSTEM;1.3 删除用户DROP USER DAVE; DROP USER DAVE CASCADE;1.4 显示用户信息SELECT * FROM DBA_USERSSELECT * FROM DBA_TS_QUOTAS二.系统权限系统权限 作
2、用CREATE SESSION 连接到数据库CREATE TABLE 建表CREATE TABLESPACE 建立表空间CREATE VIEW 建立视图CREATE SEQUENCE 建立序列CREATE USER 建立用户系统权限是指执行特定类型 SQL 命令的权利,用于控制用户可以执行的一个或一类数据库操作。(新建用户没有任何权限)2.1 授予系统权限GRANT CREATE SESSION,CREATE TABLE TO DAVE; GRANT CREATE SESSION TO DAVE WITH ADMIN OPTION; 选项:ADMIN OPTION 使该用户具有转授系统权限的权
3、限。2.2 显示系统权限查看所有系统权限:Select * from system_privilege_map;显示用户所具有的系统权限:Select * from dba_sys_privs;显示当前用户所具有的系统权限:Select * from user_sys_privs;显示当前会话所具有的系统权限:Select * from session_privs;2.3 收回系统权限REVOKE CREATE TABLE FROM DAVE;REVOKE CREATE SESSION FROM DAVE;三 .角色:角色是一组相关权限的命名集合,使用角色最主要的目的是简化权限管理。3.1 预
4、定义角色。CONNECT 自动建立,包含以下权限:ALTER SESSION、CREATE CLUSTER、CREATE DATABASELINK、CREATE SEQUENCE、CREATE SESSION、CREATE SYNONYM、CREATE TABLE、CREATEVIEW。RESOURCE 自动建立,包含以下权限:CREATE CLUSTER、CREATE PROCEDURE、CREATE SEQUENCE、CREATE TABLE、CREATE TRIGGR。3.2 显示角色信息,ROLE_SYS_PRIVSROLE_TAB_PRIVSROLE_ROLE_PRIVSSESSIO
5、N_ROLESUSER_ROLE_PRIVSDBA_ROLES四. Oracle 用户角色每个 Oracle 用户都有一个名字和口令,并拥有一些由其创建的表、视图和其他资源。Oracle 角色(role)就是一组权限(privilege)( 或者是每个用户根据其状态和条件所需的访问类型)。用户可以给角色授予或赋予指定的权限,然后将角色赋给相应的用户。一个用户也可以直接给其他用户授权。数据库系统权限(Database System Privilege)允许用户执行特定的命令集。例如,CREATE TABLE 权限允许用户创建表,GRANT ANY PRIVILEGE 权限允许用户授予任何系统权限
6、。数据库对象权限(Database Object Privilege)使得用户能够对各个对象进行某些操作。例如 DELETE 权限允许用户删除表或视图的行,SELECT 权限允许用户通过 select 从表、视图、序列( sequences)或快照 (snapshots )中查询信息。4.1 创建用户Oracle 内部有两个建好的用户:SYSTEM 和 SYS。用户可直接登录到 SYSTEM 用户以创建其他用户,因为 SYSTEM具有创建别的用户的权限。在安装 Oracle 时,用户或系统管理员首先可以为自己建立一个用户。例如:create user user01 identified by
7、u01;该命令还可以用来设置其他权限。要改变一个口令,可以使用 alter user 命令:alter user user01 identified by usr01;现在 user01 的口令已由“u01”改为“usr01”。除了 alter user 命令以外,用户还可以使用 password 命令。如果使用 password 命令,用户输入的新口令将不在屏幕上显示。有 dba 特权的用户可以通过 password 命令改变任何其他用户的口令;其他用户只能改变自己的口令。当用户输入 password 命令时,系统将提示用户输入旧口令和新口令,如下所示:passwordChanging pa
8、ssword for user01Old password:New password:Retype new password:当成功地修改了口令时,用户会得到如下的反馈:Password changed4.2 删除用户删除用户,可以使用 drop user 命令,如下所示:drop user user01;如果用户拥有对象,则不能直接删除,否则将返回一个错误值。指定关键字 CASCADE,可删除用户所有的对象,然后再删除用户。下面的例子用来删除用户与其对象:drop user user01 CASCADE;4.3 3 种标准角色Oracle 为了兼容以前的版本,提供了三种标准的角色(role)
9、:CONNECT、RESOURCE 和 DBA。4.3.1. CONNECT Role(连接角色)临时用户,特别是那些不需要建表的用户,通常只赋予他们 CONNECT role。CONNECT 是使用 Oracle 的简单权限,这种权限只有在对其他用户的表有访问权时,包括 select、insert 、 update 和 delete 等,才会变得有意义。拥有CONNECT role 的用户还能够创建表、视图、序列(sequence )、簇(cluster)、同义词(synonym )、会话(session)和与其他数据库的链(link)。4.3.2. RESOURCE Role(资源角色)更
10、可靠和正式的数据库用户可以授予 RESOURCE role。RESOURCE 提供给用户另外的权限以创建他们自己的表、序列、过程(procedure )、触发器( trigger)、索引(index)和簇(cluster )。4.3.3. DBA Role(数据库管理员角色)DBA role 拥有所有的系统权限-包括无限制的空间限额和给其他用户授予各种权限的能力。SYSTEM 由 DBA用户拥有。一些 DBA 经常使用的典型权限。1. grant(授权)命令grant connect, resource to user01;2. revoke(撤消)权限revoke connect, reso
11、urce from user01;一个具有 DBA 角色的用户可以撤消任何别的用户甚至别的 DBA 的 CONNECT、RESOURCE 和 DBA 的其他权限。当然,这样是很危险的,因此,除非真正需要,DBA 权限不应随便授予那些不是很重要的一般用户。撤消一个用户的所有权限,并不意味着从 Oracle 中删除了这个用户,也不会破坏用户创建的任何表;只是简单禁止其对这些表的访问。其他要访问这些表的用户可以象以前那样地访问这些表。五、创建角色除了前面讲到的三种系统角色-CONNECT、RESOURCE 和 DBA,用户还可以在 Oracle 创建自己的 role。用户创建的 role 可以由表或
12、系统权限或两者的组合构成。为了创建 role,用户必须具有 CREATE ROLE 系统权限。5.1 创建 rolecreate role STUDENT;这条命令创建了一个名为 STUDENT 的 role。5.2 对 role 授权一旦创建了一个 role,用户就可以给他授权。给 role 授权的 grant 命令的语法与对对用户的语法相同。在给 role 授权时,在 grant 命令的 to 子句中要使用 role 的名称,如下所示:grant select on CLASS to STUDENT;现在,拥有 STUDENT 角色的所有用户都具有对 CLASS 表的 select 权限。
13、5.3 删除角色要删除角色,可以使用 drop role 命令,如下所示:drop role STUDENT;指定的 role 连同与之相关的权限将从数据库中全部删除。六. oracle sys system 用户的区别sys 是 Oracle 数据库中权限最高的帐号,具有 create database 的权限,而 system 没有这个权限,sys 的角色是 sysdba,system 的角色是 sysoper。其余就是他们两个用户共有的权限了:startup/shutdown/dba 两个用户都是可以管理的。平时用 system 来管理数据库就可以了。这个用户的权限对于普通的数据库管理来
14、说已经足够权限了。七. 查看权限和角色ORACLE 中数据字典视图分为 3 大类, 用前缀区别,分别为:USER ,ALL 和 DBA,许多数据字典视图包含相似的信息。USER_*: 有关用户所拥有的对象信息,即用户自己创建的对象信息ALL_*: 有关用户可以访问的对象的信息,即用户自己创建的对象的信息加上其他用户创建的对象但该用户有权访问的信息DBA_*:有关整个数据库中对象的信息(这里的*可以为 TABLES, INDEXES, OBJECTS, USERS 等)。比如:只知道 scott 用户的密码,需要查看一下 scott 的一些信息7.1、查 scott 用户的创建时间、用户状态、使
15、用的默认表空间、临时表空间等信息SQL conn scott/admin已连接。SQLselect * from user_users;另:select * from all_users;(scott 用户可以访问其他数据库用户对信息的用户名)另:select * from all_users;(所有数据库的用户信息,各用户的密码、状态、默认表空间、临时表空间等)7.2、查看 scott 用户自己拥有什么角色SQL select * from user_role_privs;USERNAME GRANTED_ROLE ADM DEF OS_- - - - -SCOTT CONNECT NO Y
16、ES NOSCOTT RESOURCE NO YES NO注:“ADM”表示这个用户是否可以把该具有的角色赋予给其他的用户另:没有 all_role_privs 这个视图另:select * from dba_role_privs(所有数据库用户具有哪些角色,这个视图只有 dba 角色的权限才可以查询)7.3、查看 scott 用户自己具有什么的权限SQL select * from session_privs;7.4、查看 scott 用户具有什么的系统权限呢SQLselect * from user_sys_privs;另:没有 all_sys_privs 视图另:select * fro
17、m dba_sys_privs;(所有数据库用户、角色所用于的系统权限)7.5、查看 scott 用户中,都哪些用户把对象授予给 scott 用户呢(读取其他用户对象的权限)SQLselect * from user_tab_privs;另:select * from all_tab_privs; select * from dba_tab_privs; 7.6、查看 scott 用户中拥有的 resource 角色都具有什么权限呢SQL select * from role_sys_privs where role=RESOURCE;ROLE PRIVILEGE ADM- -RESOURCE
18、 CREATE SEQUENCE NORESOURCE CREATE TRIGGER NORESOURCE CREATE CLUSTER NORESOURCE CREATE PROCEDURE NORESOURCE CREATE TYPE NORESOURCE CREATE OPERATOR NORESOURCE CREATE TABLE NORESOURCE CREATE INDEXTYPE NO已选择 8 行。7.7、scott 用户自己拥有多少的表SQLselect * from user_tables;另:select * from all_tables; 其他用户所拥有的表另:sel
19、ect * from dba_tables;数据库中所有用户的表7.8、查看 scott 用户已经使用多大的空间,允许使用的最大空间是多少SQL select tablespace_name,bytes,max_bytes from user_ts_quotas;另:select * from dba_ts_quotas;(所有的数据库用户在每个表空间已使用的空间,最大空间)7.9、把自己的表赋予给其他用户SQLgrant select on emp to mzl;查看都把哪些表什么权限赋予了其他用户SQLselect * from user_tab_privs_made7.10、把表的某一列
20、操作权限赋予给其他用户SQLgrant update(job) on emp to mzl;注:查看数据库中所有的角色 select * form. dba_roles;7.11、sys 授予 scott 用户 dba 角色SQL conn /as sysdba已连接。SQL grant dba to scott;授权成功。另:如果这样SQL grant dba to scott with admin option;授权成功。scott 用户就可以把 dba 的权限授予给其他的用户了。7.12 sys 回收 scott 用户的 dba 角色SQL revoke dba from scott;撤
21、销成功。八Oracle 用户及作用介绍http:/ 官方文档对 Oracle 的用户分成了三类:(1) PredefinedAdministrative Accounts(2) PredefinedNon-Administrative User Accounts(3) PredefinedSample Schema User Accounts8.1 Predefined Administrative AccountsA default OracleDatabase installation provides a set of predefined administrative accounts
22、. These are accounts that have specialprivileges required to administer areas of the database, such as the CREATEANY TABLE or ALTER SESSION privilege, or EXECUTE privilegeson packages owned by the SYSschema. The default tablespace foradministrative accounts is either SYSTEM or SYSAUX.Table 3-1 Prede
23、fined Oracle DatabaseAdministrative User AccountsUser Account Description Status After InstallationANONYMOUS Account that allows HTTP access to Oracle XML DB. It is used in place of theAPEX_PUBLIC_USER account when the Embedded PL/SQL Gateway (EPG) is installed in the database.EPG is a Web server th
24、at can be used with Oracle Database. It provides the necessary infrastructure to create dynamic applications.Expired and lockedCTXSYS The account used to administer Oracle Text. Oracle Text enables you to build text query applications and document classification applications. It provides indexing, w
25、ord and theme searching, and viewing capabilities for text.See Oracle Text Application Developers Guide.Expired and lockedDBSNMP The account used by the Management Agent component of Oracle Enterprise Manager to monitor and manage the database.See Oracle Enterprise Manager Grid Control Installation
26、and Basic Configuration.OpenPassword is created at installation or database creation time.EXFSYS The account used internally to access the EXFSYS schema, which is associated with the Rules Manager and Expression Filter feature. This feature enables you to build complex PL/SQL rules and expressions.
27、The EXFSYS schema contains the Rules Manager and Expression Filter DDL, DML, and associated metadata.Expired and lockedUser Account Description Status After InstallationSee Oracle Database Rules Manager and Expression Filter Developers Guide.LBACSYS The account used to administer Oracle Label Securi
28、ty (OLS). It is created only when you install the Label Security custom option.See “Enforcing Row-Level Security with Oracle Label Security“ and Oracle Label Security Administrators Guide.Expired and lockedMDSYS The Oracle Spatial and Oracle Multimedia Locator administrator account.See Oracle Spatia
29、l Developers Guide.Expired and lockedMGMT_VIEW An account used by Oracle Enterprise Manager Database Control.OpenPassword is randomly generated at installation or database creation time. Users do not need to know this password.OLAPSYS The account that owns the OLAP Catalog (CWMLite). This account ha
30、s been deprecated, but is retained for backward compatibility.Expired and lockedOWBSYS The account for administrating the Oracle Warehouse Builder repository.Access this account during the installation process to define the base language of the repository and to define Warehouse Builder workspaces a
31、nd users. A data warehouse is a relational or multidimensional database that is designed for query and analysis.See Oracle Warehouse Builder Installation and Administration Guide.Expired and lockedORDPLUGINS The Oracle Multimedia user. Plug-ins supplied by Oracle and third-party, format plug-ins are
32、 installed in this schema.Oracle Multimedia enables Oracle Database to store, manage, and retrieve images, audio, video, DICOM format medical images and other objects, or other heterogeneous media data integrated with other enterprise information.See Oracle Multimedia Users Guide and Oracle Multimed
33、ia Reference.Expired and lockedUser Account Description Status After InstallationORDSYS The Oracle Multimedia administrator account.See Oracle Multimedia Users Guide, Oracle Multimedia Reference, and Oracle Multimedia DICOM Developers Guide.Expired and lockedOUTLN The account that supports plan stab
34、ility. Plan stability prevents certain database environment changes from affecting the performance characteristics of applications by preserving execution plans in stored outlines. OUTLN acts as a role to centrally manage metadata associated with stored outlines.See Oracle Database Performance Tunin
35、g Guide.Expired and lockedSI_INFORMTN_SCHEMA The account that stores the information views for the SQL/MM Still Image Standard.See Oracle Multimedia Users Guide and Oracle Multimedia Reference.Expired and lockedSYS An account used to perform database administration tasks.See Oracle Database 2 Day DB
36、A.OpenPassword is created at installation or database creation time.SYSMAN The account used to perform Oracle Enterprise Manager database administration tasks. The SYS and SYSTEM accounts can also perform these tasks.See Oracle Enterprise Manager Grid Control Installation and Basic Configuration.Ope
37、nPassword is created at installation or database creation time.SYSTEM A default generic database administrator account for Oracle databases.For production systems, Oracle recommends creating individual database administrator accounts and not using the generic SYSTEM account for database administrati
38、on operations.See Oracle Database 2 Day DBA.OpenPassword is created at installation or database creation time.TSMSYS An account used for transparent session migration (TSM). Expired and lockedWK_TEST The instance administrator for the default instance, WK_INST. After you unlock this account and assi
39、gn this user a password, then you must also update the cached schema password using the administration tool Edit Instance Page.Ultra Search provides uniform search-and-location capabilities over multiple repositories, such as Oracle databases, other ODBC compliant databases, IMAP mail servers, HTML
40、Expired and lockedUser Account Description Status After Installationdocuments managed by a Web server, files on disk, and more.See Oracle Ultra Search Administrators Guide.WKSYS An Ultra Search database super-user. WKSYS can grant super-user privileges to other users, such as WK_TEST. All Oracle Ult
41、ra Search database objects are installed in the WKSYS schema.See Oracle Ultra Search Administrators Guide.Expired and lockedWKPROXY An administrative account of Oracle9i Application Server Ultra Search.See Oracle Ultra Search Administrators Guide.Expired and lockedWMSYS The account used to store the
42、 metadata information for Oracle Workspace Manager.See Oracle Database Workspace Manager Developers Guide.Expired and lockedXDB The account used for storing Oracle XML DB data and metadata.Oracle XML DB provides high-performance XML storage and retrieval for Oracle Database data.See Oracle XML DB De
43、velopers Guide.Expired and locked8.2 PredefinedNon-Administrative User AccountsNon-administrativeuser accounts only have the minimum privileges needed to perform their jobs.Their default tablespace is USERS.Table 3-2 PredefinedOracle Database Non-Administrative User AccountsUser Account DescriptionS
44、tatus After InstallationAPEX_PUBLIC_USER The Oracle Database Application Express account. Use this account to specify the Oracle schema used to connect to the database through the database access descriptor (DAD).Oracle Application Express is a rapid, Web application development tool for Oracle Data
45、base.See Oracle Database Application Express Users Guide.Expired and lockedDIP The Oracle Directory Integration and Provisioning (DIP) account that is installed with Oracle Label Security. This profile is created automatically Expired and lockedUser Account DescriptionStatus After Installationas par
46、t of the installation process for Oracle Internet Directory-enabled Oracle Label Security.See Oracle Label Security Administrators Guide.FLOWS_30000 The account that owns most of the database objects created during the installation of Oracle Database Application Express. These objects include tables
47、, views, triggers, indexes, packages, and so on.See Oracle Database Application Express Users Guide.Expired and lockedFLOWS_FILES The account that owns the database objects created during the installation of Oracle Database Application Express related to modplsql document conveyance, for example, fi
48、le uploads and downloads. These objects include tables, views, triggers, indexes, packages, and so on.See Oracle Database Application Express Users Guide.Expired and lockedMDDATA The schema used by Oracle Spatial for storing Geocoder and router data.Oracle Spatial provides a SQL schema and functions
49、 that enable you to store, retrieve, update, and query collections of spatial features in an Oracle database.See Oracle Spatial Developers Guide.Expired and lockedORACLE_OCM The account used with Oracle Configuration Manager. This feature enables you to associate the configuration information for the current Oracle Database instance with OracleMetaLink. Then when y