1 create tablespace caoke datafile 'D:\caoke.ora' size 100M ; 2 create tablespace test datafile 'c:oracleoradataorcl9test.dbf' size 50M default storage (initial 500K Next 500K minextents 1 maxextents unlimited pctincrease 0); 3 create tablespace tbs_name datafile '/dba/oradata/ORADEV/datafile/tbs_name01.dbf' size 100m autoextend on next 100m;
create user caoke123 identified by caoke123 default tablespace caoke quota 50M on users ; quota 配額 ,限制用戶對錶空間的使用 好比你限制用戶a在tablespace aa中的quota爲10m,當用戶a在tablespace aa中的數據量達到10m後, 不管你的tablespace aa中有多少空間,a都沒法再使用tablespace aa了。
一、首先,grant XXX to user; ,grant是受權的做用,這裏的XXX能夠是一個角色role,也能夠是權限,例如grant role to user;,或grant insert on table to user;。數據庫
MOS中給出的標準SQL語句:session
建立角色: create role <role name> [IDENTIFIED BY <password>/USING <package>/EXTERNALLY/GLOBALLY ]; 賦予角色權限: grant <object/system privilege> to <role name>; 從角色收回權限: revoke <privilege> from <role name>; 將角色賦予另外一個角色或用戶: grant <role> to <username or role> ;
二、其次,connect和resource是兩個系統內置的角色,和dba是並列的關係。oracle
參考一些帖子的說法,權限能夠分爲兩類:ide
系統權限:系統規定用戶使用數據庫的權限。(系統權限是對用戶而言)。 spa
實體權限:某種權限用戶對其它用戶的表或視圖的存取權限。(是針對表或視圖而言的)。code
接下來看系統權限:對象
DBA:擁有所有特權,是系統最高權限,只有DBA才能夠建立數據庫結構。索引
RESOURCE:擁有Resource權限的用戶只能夠建立實體,不能夠建立數據庫結構。it
CONNECT:擁有Connect權限的用戶只能夠登陸Oracle,不能夠建立實體,不能夠建立數據庫結構。io
對於普通用戶:授予connect, resource權限。
對於DBA管理用戶:授予connect,resource, dba權限。
且系統權限只能由DBA用戶授出:sys, system(最開始只能是這兩個用戶)。 普通用戶經過受權能夠具備與system相同的用戶權限, 但永遠不能達到與sys用戶相同的權限,system用戶的權限也能夠被回收。
另外,對於WITH ADMIN OPTION級聯受權的問題,
1)若是使用WITH ADMIN OPTION爲某個用戶授予系統權限,那麼對於被這個用戶授予相同權限的全部用戶來講,取消該用戶的系統權限並不會級聯取消這些用戶的相同權限。
2)系統權限無級聯,即A授予B權限,B授予C權限,若是A收回B的權限,C的權限不受影響;系統權限能夠跨用戶回收,即A能夠直接收回C用戶的權限。
三、證實下爲何resource和connect的角色不能建立視圖。
SQL> select role, count(*) from role_sys_privs group by role; ROLE COUNT(*) ------------------------------ ---------- EXP_FULL_DATABASE 8 DBA 160 SCHEDULER_ADMIN 6 RESOURCE 8 IMP_FULL_DATABASE 68 CONNECT 1 能夠看到RESOURCE和CONNECT具備的權限個數。 下面看看各自都有什麼權限: SQL> select grantee,privilege from dba_sys_privs where grantee='RESOURCE' order by privilege; GRANTEE PRIVILEGE ------------------------------ ---------------------------------------- RESOURCE CREATE CLUSTER RESOURCE CREATE INDEXTYPE RESOURCE CREATE OPERATOR RESOURCE CREATE 'PROCEDURE'(代碼格式會亂,加個引號,這個詞的問題) RESOURCE CREATE SEQUENCE RESOURCE CREATE TABLE RESOURCE CREATE TRIGGER RESOURCE CREATE TYPE 8 rows selected SQL> select grantee,privilege from dba_sys_privs where grantee='CONNECT' order by privilege; GRANTEE PRIVILEGE ------------------------------ ---------------------------------------- CONNECT CREATE SESSION
結果不言自明瞭,
CREATE VIEW權限並不在這兩個角色中,所以須要額外grant CREATE VIEW to user;,才能讓這用戶能夠建立視圖。每每實驗是最好的老師,此次又證實了這點。
grant all privileges to caoke123 ; grant dba to caoke123;--授予DBA權限 grant unlimited tablespace to caoke123;--授予不限制的表空間 grant select any table to caoke123;--授予查詢任何表 grant select any dictionary to caoke123;--授予 查詢 任何字典 grant create session,create table to zzg; --給用戶賦權(多個採用逗號間隔) oracle 正在鏈接的用戶不能刪除,確實要刪除的話 一、select sid,serial#,username from v$session where user='USERNAME'; 二、alter system kill session 'sid,serial#'; 三、drop user username cascade;
查詢用戶相關信息
查詢當前用戶下有多少表 select * from tab ; select * from tabs ; select * from user_tables ; 查看用戶所擁有的視圖 select view_name from user_views ; 查看用戶所擁有的觸發器 select trigger_name from user_triggers ; 查看用戶擁有的序列 select sequence_name from user_sequence; 查看用戶擁有的索引 select index_name from user_indexs; 顯示當前用戶 show user; 修改用戶密碼 alter user zzg identified by zzg123; 給用戶加鎖 , 解鎖 alter user scott account lock; alter user scott account unlock; 刪除用戶及相關對象 drop user zzg cascade; 當前用戶所具備的權限 select * from session_privs;
表空間相關
查看當前數據庫的全稱 select * from global_name ; 查看當前數據庫的用戶信息 select * from dba_users ; 分配表空間給用戶 alter user zzg default tablespace ts_zzg; 查詢用戶的配額 select tablespace_name,username,max_bytes from DBA_TS_QUOTAS ; 用戶在其餘表空間的操做權限 select * from user_ts_quotas; 查看用戶所具備的系統權限 select * from dba_sys_privs where grantee = '用戶名稱'; 查看用戶所具備的角色 select * from dba_roles ; select * from dba_role_privs where grantee = '用戶名稱'; 查詢當前用戶能夠訪問的全部數據字典視圖 select * from dict where comments like '%grant%' ; 查詢數據庫中存在的表空間 select * from dba_tablespaces ; 查詢表空間名下全部的表名 select * from all_tables where tablespace_name = '' ; 查詢表屬於哪一個表空間 select * from user_tables where table_name = '' ;
查看錶空間剩餘
SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name FROM dba_free_space GROUP BY tablespace_name; SELECT a.tablespace_name, a.bytes total, b.bytes used, c.bytes free, (b.bytes * 100) / a.bytes "% USED ", (c.bytes * 100) / a.bytes "% FREE " FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c WHERE a.tablespace_name = b.tablespace_name AND a.tablespace_name = c.tablespace_name;
查詢表空間的用量
SELECT a.tablespace_name "表空間名", total "表空間大小", free "表空間剩餘大小", (total - free) "表空間使用大小", total / (1024 * 1024 * 1024) "表空間大小(G)", free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", (total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", round((total - free) / total, 4) * 100 "使用率 %" FROM (SELECT tablespace_name, SUM(bytes) free FROM dba_free_space GROUP BY tablespace_name) a, (SELECT tablespace_name, SUM(bytes) total FROM dba_data_files GROUP BY tablespace_name) b WHERE a.tablespace_name = b.tablespace_name ;
顯示錶空間的信息
select * from dba_tablespaces ;
查詢表空間所包含的數據文件,數據保存地址
select * from dba_data_files ;
使表空間脫機
Alter tablespace 表空間名 offline ;
使表空間聯機
Alter tablespace 表空間名 online ;
只讀表空間 若是不但願對錶空間進行insert,update,delete操做,能夠將表空間修改成只讀
Alter tablespace 表空間名 read only ;
使表空間可讀可寫
Alter tablespace 表空間名 read write ;
刪除表空間
由特權用戶或者dba來操做,若是是其餘用戶,要求有 drop tablespace 權限 drop tablespace 表空間名 including contents and datafiles Including contents 表示刪除表空間的時候刪除該空間的全部數據對象,而datafiles表示將數據庫文件也刪除。
增長表空間
1 增長數據文件 Alter tablespace 表空間名add datafile ‘d:/aaa.ora’ size 20M 2 修改數據文件的大小 Alter tablespace 表空間名 ‘d:/aaa.ora’ resize 20m 3 設置文件的自動增加 Alter tablespace 表空間名 ‘d:/aaa.ora’ autoextend on next 10m maxsize 500m ;