Oracle_高级功能(5) 用户、角色、权限

一、用户(模式)
1.定义
用户:对数据库的访问,需要以适当用户身份通过验证,并具有相关权限来完成一系列动作
模式(schema):是某个用户所拥有的对象的集合。
具有创建对象权限并创建了对象的用户称为拥有某个模式
注意:
创建数据库对象(视图,表等)的任一用户都拥有一个与该用户名称相同的模式,
且被视为模式用户。

2.用户分类:
用户分为:系统预定义用户、自定义用户。
系统预定义用户包括:
sys用户,缺省始终创建,且未被锁定,拥有数据字典及其关联的所有对象
system用户,缺省始终创建,且未被锁定,可以访问数据库内的所有对象
scott用户等。
自定义用户:
使用者自己创建的用户。

3.创建用户
条件:需要具有创建用户的权限,如sys,system等
语法:
create user user_name
identified by password
[default tablespace tablespace_name]
[temporary tablespace tablespace_name]
[quota {n {[k|m] | unlimited } on tablespace_name,
quota {n {[k|m] | unlimited } on tablespace_name ... ]
[password expire]
[account { lock | unlock }]
举例:
create user find identified by find default tablespace ts_find;
说明:
user_name:是创建的用户名
identified by password:设置用户的密码
[default tablespace tablespace_name]:用户默认的表空间,可以省略。
[temporary tablespace tablespace_name]:用户默认的临时表空间,可以省略。
[quota..:用户表空间限额,可以省略。
[password expire]:设置密码的有效期,可以省略。
[account { lock | unlock }]:账户锁定和解锁,可以省略。

查看用户基本信息:
select * from dba_users where username='FIND';
查看用户表空间配额(dba_ts_quotas):
select username,tablespace_name,max_bytes/1024/1024 "max mb"
from dba_ts_quotas where username='FIND';

4.修改用户
修改用户的语法同创建用户,仅仅是将关键字create替换为alter。
alter user可以修改除用户名之外的任一属性。
--alter user find rename to find1;

4.1.修改密码
dba 可以创建用户和修改密码
用户本人可以使用alter user 语句修改密码
sql> alter user find identified by 123;

4.2 用户锁定和解锁
alter user find account lock;
alter user find account unlock;

4.3 改变用户在表空间上的配额:
alter user find quota 8M on ts_find;

查看用户表空间配额(dba_ts_quotas):
select username,tablespace_name,max_bytes/1024/1024 "max mb"
from dba_ts_quotas where username='FIND';
alter user find quota unlimited on ts_find;

5.登录、使用用户
5.1 登录
connect find/123;
ora-01045
connect sys/123@orcl as sysdba;
grant create session to find;
=>
grant connect to find;
connect find/123@orcl;

grant resource to find;

5.2 复制表
create table emp as select * from scott.emp;
ORA-00942: 表或视图不存在
grant create any table,alter any table,drop any table to find;
grant select any table,insert any table,update any table,delete any table to find;
create table emp as select * from scott.emp;
select * from emp;

5.3 查看用户所拥有的对象
select * from user_objects;
sys用户可以使用dba_objects视图查看每个用户所拥有的对象
select owner,object_name,object_type from dba_objects where lower(owner)='find';

6.删除用户:
drop user username [cascade]; --级联
举例:
drop user find;
ORA-01940: 无法删除当前连接的用户
ORA-01922: 必须指定 CASCADE 以删除 'FIND'
drop user find cascade;
说明:
casecade:删除用户时连同用户创建的对象一并删除。
如果用户之前创建了对象,在删除时必须加cascade,否则删除不掉。
不能删除当前正在与oracle服务器相连的用户。

7.角色:拥有一组权限的集合,称为角色。
--角色视图
select * from dba_roles;
--角色权限视图
select * from role_sys_privs order by role

select * from role_sys_privs where role='CONNECT';
select * from role_sys_privs where role='RESOURCE';
select * from role_sys_privs where role='DBA';

--用户拥有的角色
--sys
select * from dba_role_privs where grantee='SCOTT';
--scott
select * from user_role_privs;

--用户拥有的角色权限
select * from role_sys_privs rp,user_role_privs ur
where rp.role=ur.granted_role order by role

二、oracle权限
系统权限: 允许用户执行特定的数据库动作,如创建表、创建索引、连接实例等
对象权限: 允许用户操纵一些特定的对象,如读取视图,可更新某些列、执行存储过程等

1.系统权限
系统权限共有208个:
select * from system_privilege_map
如:
CREATE类62个:
select name from system_privilege_map where lower(name) like '%create%' order by privilege desc
DROP类35个:
select name from system_privilege_map where lower(name) like '%drop%' order by privilege desc
ALTER类33个:
select name from system_privilege_map where lower(name) like '%alter%' order by privilege desc
SELECT类7个:
select name from system_privilege_map where lower(name) like '%select%' order by privilege desc
INSERT类3个:
select name from system_privilege_map where lower(name) like '%insert%' order by privilege desc
UPDATE类4个:
select name from system_privilege_map where lower(name) like '%update%'order by privilege desc
DELETE类3个:
select name from system_privilege_map where lower(name) like '%delete%'order by privilege desc
EXECUTE类12个:
select name from system_privilege_map where lower(name) like '%execute%'order by privilege desc
其它类49个:
select name from system_privilege_map
where lower(name) not like '%create%'
and lower(name) not like '%alter%'
and lower(name) not like '%drop%'
and lower(name) not like '%select%'
and lower(name) not like '%insert%'
and lower(name) not like '%update%'
and lower(name) not like '%delete%'
and lower(name) not like '%execute%'
order by name

1.1 常用的系统权限:
create session 创建会话
create sequence 创建序列
create synonym 创建同名对象
create table 在用户模式中创建表
create any table 在任何模式中创建表
drop table 在用户模式中删除表
drop any table 在任何模式中删除表
create procedure 创建存储过程
execute any procedure 执行任何模式的存储过程
create user 创建用户
drop user 删除用户
create view 创建视图

1.2 授予用户系统权限
grant privilege to [user|role|public] [with admin option];
说明:
public 所有用户
with admin option 使用户同样具有分配权限的权利,可将此权限授予别人
例1:
sql> grant create user to scott;
sql> conn scott/123;
create user find02 identified by find default tablespace ts_find;
--验证
select grantee,privilege,admin_option from dba_sys_privs
where lower(grantee)='scott' order by grantee
--用户拥有的所有系统权限
select privilege from user_sys_privs
union all
select privilege from role_sys_privs rp,user_role_privs ur
where rp.role=ur.granted_role
==>
select privilege from user_sys_privs
union
select privilege from role_sys_privs;

例2:
sql> grant execute any procedure to scott with admin option;
sql> conn scott/123; --scott具有with admin option,故可以将execute any procedure授予find
sql> grant execute any procedure to find;
sql> grant execute any procedure to public; --将execute any procedure授予所有用户
--验证
select grantee,privilege,admin_option from dba_sys_privs
where lower(grantee) in ('scott','find','find02') order by grantee

例3:
sql> grant create any table to find02;
select grantee,privilege,admin_option from dba_sys_privs
where lower(grantee) in ('scott','find','find02') order by grantee

1.3 使用系统权限
--使用find具有创建会话、创建表
sql> create table tb1 as select * from user_tables;
--下面提示没有权限在users表空间创建对象
sql> conn sys as sysdba; --使用sys帐户登陆并为find在users表空间指定配额后可以创建表tb1
sql> alter user find quota 10m on users;
sql> conn find/find;
sql> create table tb1 as select * from user_tables;

1.4 查看系统权限
dba_sys_privs --针对所有用户被授予的系统权限
select * from dba_sys_privs;
user_sys_privs --针对当前登陆用户被授予的系统权限
select * from user_sys_privs;
--
select grantee,privilege,admin_option from dba_sys_privs
where lower(grantee) in ('scott','find') order by grantee;

1.5 回收系统权限
revoke {privilege|role} from {user_name|role_name|public}

例1:回收权限
revoke execute any procedure from scott;
select grantee,privilege,admin_option from dba_sys_privs
where lower(grantee) in ('scott','find') order by grantee;
注意:对于使用with admin option 为某个用户授予系统权限,
那么对于被这个用户授予相同权限的其它用户来说,
取消该用户的系统权限并不会级联取消其它的相同权限。

例2:回收角色
revoke connect from find;
select * from dba_role_privs where grantee='FIND';
grant connect to find;

2、对象权限
不同的对象具有不同的对象权限。
对象的拥有者拥有所有权限。
对象的拥有者可以向外分配权限。

revoke insert any table from find;
revoke update any table from find;
revoke delete any table from find;
revoke select any table from find;

--对象授权示例
grant select on emp to find;
grant update(sal,comm) on emp to find;

oracle一共有种对象权限

对象权限 表 视图 序列 过程
选择(select) √ √ √
插入(insert) √ √
更新(update) √ √
删除(delete) √ √
创建(create) √ √ √ √
修改(alter) √ √
丢弃(drop) √ √ √ √
索引(index) √
关联(references) √ √
执行(execute) √

2.1 对象授权
grant object_priv|all [(columns)] on object to {user|role|public} [with grant option];
说明:
all:所有对限象权
public:授给所有的用户
with grant option:允许用户再次给其它用户授权

2.2 授予系统权限与授予对象权限的语法差异:
授予对象权限时需要指定关键字on,从而能够确定权限所应用的对象。
对于表和视图可以指定特定的列来授权。


--新创建一个用户john,使用find账户授予更新scott.emp(sal,mgr)的权限
--create user john identified by john;
--grant create session to john;
--conn find/lion
grant update(sal,mgr) on scott.emp to find02; --授予scott.emp(sal,mgr)的更新权限

update scott.emp set sal = sal + 100 where ename = 'scott'; --成功更新
--向数据库中所有用户分配权限
grant select on dept to public;

2.3 查询权限分配情况
数据字典视图 描述
role_sys_privs 角色拥有的系统权限
role_tab_privs 角色拥有的对象权限

user_tab_privs_made 查询授出去的对象权限(通常是属主自己查)
user_tab_privs_recd 用户拥有的对象权限

user_col_privs_made 用户分配出去的列的对象权限
user_col_privs_recd 用户拥有的关于列的对象权限

user_sys_privs 用户拥有的系统权限
user_tab_privs 用户拥有的对象权限
user_col_privs 用户拥有的对象列权限
user_role_privs 用户拥有的角色
select * from dba_tab_privs where grantee='PUBLIC' and owner='SCOTT';

--查询已授予的对象权限(即某个用户对哪些表对哪些用户开放了对象权限)
select * from user_tab_privs_made; --下面是scott用户开放的对象权限
--查询列上开放的对象权限
select * from user_col_privs_made;
--查询已接受的对象特权(即某个用户被授予了哪些表上的哪些对象特权)
select * from user_tab_privs_recd;
--查询用户已接受列的对象权限
select * from user_col_privs_recd;

2.4 收回对象权限
使用revoke 语句收回权限
使用with grant option 子句所分配的权限同样被收回

revoke privilege|all on object from {user|role|public} [cascade constraints];
说明:
cascade constraints 为处理引用完整性时需要

revoke update(sal,mgr) on emp from find;
ORA-01750: UPDATE/REFERENCES 只能从整个表而不能按列 REVOKE
--
revoke update on emp from find;


--收回权限示例
conn scott/tiger;
revoke select on emp from find;
--回收public对象权限
revoke select on dept from public;


--注意此处的提示revoke的是整个表,而非列
--revoke update(sal,mgr) on emp from find;
revoke update on emp from find;

--用户find的update 权限被revoke,曾级联赋予john的权限也被收回,
--如下提示表、视图不存在,user_col_privs_recd中无记录
conn john/john;
--update scott.emp set sal = sal - 100 where ename = 'scott';
select * from user_col_privs_recd;

connect scott/123;
grant select on scott.emp to find with grant option;
connect find/find;
grant select on scott.emp to find02;
connect scott/123;
revoke select on scott.emp from find;
注意:如果取消某个用户的对象权限,对于该用户使用with grant option授予其它用户相同权限来说,
将级联删除这些用户权限

2.5 其它
检查dba权限的用户
select * from dba_role_privs where granted_role='DBA';

查看用户具有的系统权限:
select * from session_privs;

四、总结
1.使用create user语句创建用户,alter user语句修改用户,其语法大致相同
drop user username [cascade] 会删除用户所拥有的所有对象及数据
2.系统权限允许用户在数据库中执行特定的操作,如执行ddl语句。
with admin option 使得该用户具有将自身获得的权限授予其它用户的功能;
但收回系统权限时,不会从其它帐户级联取消曾被授予的相同权限。

3.对象权限允许用户对数据库对象执行特定的操作,如执行dml语句。
with grant option 使得该用户具有将自身获得的对象权限授予其它用户的功能;
但收回对象权限时,会从其它帐户级联取消曾被授予的相同权限。
4.系统权限与对象权限授予时的语法差异为对象权限使用了on object_name 子句
5. public 为所有的用户
6. all:对象权限中的所有对象权限
7.实际的例子
connect system/123@orcl;
--删除用户--
drop user find cascade;
--删除表空间--
drop tablespace ts_find including contents;
--创建表空间及数据文件--
create tablespace ts_find datafile 'D:apporadataorclfind.DBF' size 100M reuse autoextend on next 10M;
--创建用户并授权--
create user find identified by find_password default tablespace ts_find;
grant resource,connect to find;
grant select any sequence to find;
grant create any table,alter any table,drop any table to find;
grant select any table,insert any table,update any table,delete any table to find;
grant create any trigger,alter any trigger,drop any trigger to find;
grant create any procedure,alter any procedure,drop any procedure,execute any procedure to find;
grant create any view,drop any view to find;
grant create any synonym to find;

原文地址:https://www.cnblogs.com/BradMiller/p/9279822.html