oracle數據庫的權限系統分為系統權限與對象權限。系統權限( database system privilege )可以讓用戶執行特定的命令集。例如,create table權限允許用戶創建表,grant any privilege 權限允許用戶授予任何系統權限。對象權限( database object privilege )可以讓用戶能夠對各個對象進行某些操作。例如delete權限允許用戶刪除表或視圖的行,select權限允許用戶通過select從表、視圖、序列(sequences)或快照(snapshots)中查詢信息。
每個oracle用戶都有一個名字和口令,並擁有一些由其創建的表、視圖和其他資源。oracle角色(role)就是一組權限(privilege)(或者是每個用戶根據其狀態和條件所需的訪問類型)。用戶可以給角色授予或賦予指定的權限,然后將角色賦給相應的用戶。一個用戶也可以直接給其他用戶授權。
一、創建用戶
oracle內部有兩個建好的用戶:system和sys。用戶可直接登錄到system用戶以創建其他用戶,因為system具有創建別 的用戶的 權限。 在安裝oracle時,用戶或系統管理員首先可以為自己建立一個用戶。
語法[創建用戶]: create user 用戶名 identified by 口令[即密碼];
例子: create user test identified by test;
語法[更改用戶]: alter user 用戶名 identified by 口令[改變的口令];
例子: alter user test identified by 123456;
二、刪除用戶
語法:drop user 用戶名;
例子:drop user test;
若用戶擁有對象,則不能直接刪除,否則將返回一個錯誤值。指定關鍵字cascade,可刪除用戶所有的對象,然后再刪除用戶。
語法: drop user 用戶名 cascade;
例子: drop user test cascade;
三、授權角色
oracle為兼容以前版本,提供三種標准角色(role):connect/resource和dba.
(1)講解三種標准角色:
1》. connect role(連接角色)
--臨時用戶,特指不需要建表的用戶,通常只賦予他們connect role.
--connect是使用oracle簡單權限,這種權限只對其他用戶的表有訪問權限,包括select/insert/update和delete等。
--擁有connect role 的用戶還能夠創建表、視圖、序列(sequence)、簇(cluster)、同義詞(synonym)、回話(session)和其他 數據的鏈(link)
2》. resource role(資源角色)
--更可靠和正式的數據庫用戶可以授予resource role。
--resource提供給用戶另外的權限以創建他們自己的表、序列、過程(procedure)、觸發器(trigger)、索引(index)和簇(cluster)。
3》. dba role(數據庫管理員角色)
--dba role擁有所有的系統權限
--包括無限制的空間限額和給其他用戶授予各種權限的能力。system由dba用戶擁有
(2)授權命令
語法: grant connect, resource to 用戶名;
例子: grant connect, resource to test;
(3)撤銷權限
語法: revoke connect, resource from 用戶名;
列子: revoke connect, resource from test;
四、創建/授權/刪除角色
除了前面講到的三種系統角色----connect、resource和dba,用戶還可以在oracle創建自己的role。用戶創建的role可以由表或系統權限或兩者的組合構成。為了創建role,用戶必須具有create role系統權限。
1》創建角色
語法: create role 角色名;
例子: create role testRole;
2》授權角色
語法: grant select on class to 角色名;
列子: grant select on class to testRole;
注:現在,擁有testRole角色的所有用戶都具有對class表的select查詢權限
3》刪除角色
語法: drop role 角色名;
例子: drop role testRole;
注:與testRole角色相關的權限將從數據庫全部刪除
權限:
create session
create table
unlimited tablespace
connect
resource
dba
例:
#sqlplus /nolog
SQL> conn / as sysdba;
SQL>create user username identified by password
SQL> grant dba to username;
SQL> conn username/password
SQL> select * from user_sys_privs;
我們將從創建Oracle用戶權限表開始談起,然后講解登陸等一般性動作,使大家對Oracle用戶權限表有個深入的了解。
一、創建
sys;//系統管理員,擁有最高權限
system;//本地管理員,次高權限
scott;//普通用戶,密碼默認為tiger,默認未解鎖
二、登陸
sqlplus / as sysdba;//登陸sys帳戶
sqlplus sys as sysdba;//同上
sqlplus scott/tiger;//登陸普通用戶scott
三、管理用戶
create user zhangsan;//在管理員帳戶下,創建用戶zhangsan
alert user scott identified by tiger;//修改密碼
四,授予權限
1、默認的普通用戶scott默認未解鎖,不能進行那個使用,新建的用戶也沒有任何權限,必須授予權限
grant create session to zhangsan;//授予zhangsan用戶創建session的權限,即登陸權限
grant unlimited tablespace to zhangsan;//授予zhangsan用戶使用表空間的權限
grant create table to zhangsan;//授予創建表的權限
grante drop table to zhangsan;//授予刪除表的權限
grant insert table to zhangsan;//插入表的權限
grant update table to zhangsan;//修改表的權限
grant all to public;//這條比較重要,授予所有權限(all)給所有用戶(public)
2、oralce對權限管理比較嚴謹,普通用戶之間也是默認不能互相訪問的,需要互相授權
grant select on tablename to zhangsan;//授予zhangsan用戶查看指定表的權限
grant drop on tablename to zhangsan;//授予刪除表的權限
grant insert on tablename to zhangsan;//授予插入的權限
grant update on tablename to zhangsan;//授予修改表的權限
grant insert(id) on tablename to zhangsan;
grant update(id) on tablename to zhangsan;//授予對指定表特定字段的插入和修改權限,注意,只能是insert和update
grant alert all table to zhangsan;//授予zhangsan用戶alert任意表的權限
五、撤銷權限
基本語法同grant,關鍵字為revoke
六、查看權限
select * from user_sys_privs;//查看當前用戶所有權限
select * from user_tab_privs;//查看所用用戶對表的權限
七、操作表的用戶的表
select * from zhangsan.tablename
八、權限傳遞
即用戶A將權限授予B,B可以將操作的權限再授予C,命令如下:
grant alert table on tablename to zhangsan with admin option;//關鍵字 with admin option
grant alert table on tablename to zhangsan with grant option;//關鍵字 with grant option效果和admin類似
九、角色
角色即權限的集合,可以把一個角色授予給用戶
create role myrole;//創建角色
grant create session to myrole;//將創建session的權限授予myrole
grant myrole to zhangsan;//授予zhangsan用戶myrole的角色
drop role myrole;刪除角色
Oracle建立表空間和用戶
- 建立表空間和用戶的步驟:
- 用戶
- 建立:create user 用戶名 identified by "密碼";
- 授權:grant create session to 用戶名;
- grant create table to 用戶名;
- grant create tablespace to 用戶名;
- grant create view to 用戶名;
- 表空間
- 建立表空間(一般建N個存數據的表空間和一個索引空間):
- create tablespace 表空間名
- datafile ' 路徑(要先建好路徑)\***.dbf ' size *M
- tempfile ' 路徑\***.dbf ' size *M
- autoextend on --自動增長
- --還有一些定義大小的命令,看需要
- default storage(
- initial 100K,
- next 100k,
- );
- 例子:創建表空間
- create tablespace DEMOSPACE
- datafile 'E:/oracle_tablespaces/DEMOSPACE_TBSPACE.dbf'
- size 1500M
- autoextend on next 5M maxsize 3000M;
- 刪除表空間
- drop tablespace DEMOSPACE including contents and datafiles
- 用戶權限
- 授予用戶使用表空間的權限:
- alter user 用戶名 quota unlimited on 表空間;
- 或 alter user 用戶名 quota *M on 表空間;
完整例子:
- --表空間
- CREATE TABLESPACE sdt
- DATAFILE 'F:\tablespace\demo' size 800M
- EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
- --索引表空間
- CREATE TABLESPACE sdt_Index
- DATAFILE 'F:\tablespace\demo' size 512M
- EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
- --2.建用戶
- create user demo identified by demo
- default tablespace std;
- --3.賦權
- grant connect,resource to demo;
- grant create any sequence to demo;
- grant create any table to demo;
- grant delete any table to demo;
- grant insert any table to demo;
- grant select any table to demo;
- grant unlimited tablespace to demo;
- grant execute any procedure to demo;
- grant update any table to demo;
- grant create any view to demo;
- --導入導出命令
- ip導出方式: exp demo/demo@127.0.0.1:1521/orcl file=f:/f.dmp full=y
- exp demo/demo@orcl file=f:/f.dmp full=y
- imp demo/demo@orcl file=f:/f.dmp full=y ignore=y