Oracle:用戶、特權和角色

一:用戶

1.創建用戶

語法:

GRANT USER user_name IDENTIFIED BY password
[DEFAULT TABLESPACE default_tablespace]
[TEMPORARY TABLESPACE temporary_tablespace]

user_name指定要創建的數據庫的名稱
password 指定數據庫用戶的密碼
default_tablespace指定存儲數據庫對象時使用的默認表空間
temporary_tablespace指定保存臨時對象時使用的默認表空間

例子:
如果要運行,需要以特權身份連接到數據庫

create user temp identified by temp
default tablespace temp001
temporary tablespace temp002;

注意:表中必須有temp001和temp002纔可以。

查看數據庫中所有表空間

select tablespace_name from dba_tablespaces

授權:

GRANT CREATE SESSION TO temp;

2.修改用戶密碼

使用下面這條語句

ALTER USER temp IDENTIFIED BY green;
CONNECT temp/green
PASSWORD

Changing password for TEMP
Old password: *******
New password: *******
Retype new password: *******
Password changed;

3.刪除用戶

CONNECT temp/green
DROP USER temp;

注意:如果要刪除的用戶模式中包含任何表或其他項,就必須在drop user語句中,再要刪除的用戶名後面加上關鍵字cascade。但是這樣做的時候,要保證其他用戶不需要訪問這些對象。

二:系統特權

常用特權:

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.向用戶授予系統特權

GRANT CREATE SESSION, CREATE USER, CREATE TABLE TO temp;

加上WITH ADMIN OPTION,就可以把這種特權再授權給其他用戶

GRANT EXECUTE ANY PROCEDURE TO temp WITH ADMIN OPTION

通過授權給PUBLIC,就是所有用戶有了某種特權

CONNECT temp/green
GRANT EXECUTE ANY PROCEDURE TO PUBLIC;

2.查看授權用戶的用戶特權

CONNECT temp/green
select username,provilege,admin_option
from user_sys_privs
order by username, privilege;

USER       PRIVILEGE               ADM
-------------------------------------------
PUBLIC   EXECUTE ANY PROCEDURE     NO 
TEMP     CREATE SESSION            NO
TEMP     CREATE USER               NO
......

3.撤銷用戶的特權

CONNECT temp/green
REVOKE EXECUTE ANY PROCEDURE FROM temp;

三:對象特權

常用的對象特權:

SELECT 執行查詢操作
INSERT 執行插入操作
UPDATE 執行更新操作
DELETE 執行刪除操作
EXECUTE 執行存儲過程

1.向用戶對象授權

create user test001 identified by test001;

conncet test001/test001
grant select, insert, update on test001.test to temp;

2.查看已授予的對象特權

查詢某個用戶對那些表向其他用戶開放了哪些對象特權

select grantee, table_name, grantor, privilege, grantable, hierarchy
from user_tab_privs_made
where table_name='test';

查詢某個用戶對哪些列對象開放了哪些特權

select grantee, table_name, column_name, grantor, privilege, grantable
from user_col_privs_made
order by column_name;

查詢某個用戶被授予了哪些表上的哪些對象特權

select owner, table_name, grantor, privilege, grantable, hierarchy
from user_tab_privs_recd
order by table_name, privilege;

查詢某個用戶被授予了哪些列的對象特權

select owner, table_name, column_name, grantor, privilege, grantable
from user_col_privs_recd;

3.創建同義詞

同義詞:同義詞用於引用其他模式中的表。

創建同義詞

connect system/system
grant create synonym to test001;

connect temp/green
create synonym testTemp for temp.test;

如果其他用戶使用temp模式下的test表,直接使用同義詞testTemp就可以
例如:

select * from testTemp;

創建公共同義詞

所有的用戶都可以看到這個同義詞

connect system/system
grant create public synonym to temp;
connect temp/green
create public synonym testTemp for temp.test;

注意:雖然所有用戶都可以看到這個同義詞,但是隻有當用戶具有test表的select特權,纔可以使用同義詞,否則會報錯

撤銷用戶的對象特權

revoke insert on test from temp;

四:角色

角色就是一組特權

connect system/system
grant create role to vivi;
grant create user to vivi with admin option;

1.創建角色

connect vivi/vivivi
create role type_manager;
create role overall_manger identified by manager_password;

2.爲角色授權

grant select, insert, update, delete on test to type_manager;
grant create user to type_manager;
grant type_manager to overall_manger;

3.將角色授予用戶

connect system/system
create user Zoey identified by zoey;
grant create session to zoey;

grant type_manager to zoey;

4.查詢授予用戶的角色

connect Zoey/zoey
select username, granted_role, admin_option, default_role
from user_role_privs;

5.查詢授予角色的系統特權

select role, privilege, admin_optionfrom role_sys_privsorder by role, privilege;

6.查詢授予角色特權

select role, owner, table_name, column_name, privilege, grantablefrom role_tab_privswhere role=type_manager;

7.啓用和禁用角色啓用角色:

set role type_manager;

修改角色不再爲默認角色

alter user zoey default role all except type_manager;

8.撤銷角色

revoke type_manager from zoey;

9.從角色中撤銷特權

revoke all on test from type_manager;

10.刪除角色

drop role type_manager;

五:審計

connect system/system
grant audit system to zoey;
grant audit any to zoey;

audit create table;
audit select table by zoey;
audit update table by zoey whenever not successful;

noaudit update on zoey.test;
發佈了180 篇原創文章 · 獲贊 335 · 訪問量 64萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章