1. 程式人生 > >Oracle_高級功能(5) 用戶、角色、權限

Oracle_高級功能(5) 用戶、角色、權限

syn 並不會 eat ant lte toe 權限 設置密碼 lec

一、用戶(模式)
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:\app\oradata\orcl\find.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;

Oracle_高級功能(5) 用戶、角色、權限