oracle 檢視使用者所在的表空間

quanjima發表於2014-11-04

檢視當前使用者的預設表空間

  SQL>select username,default_tablespace from user_users;


  檢視當前使用者的角色

  SQL>select * from user_role_privs;

  檢視當前使用者的系統許可權和表級許可權

  SQL>select * from user_sys_privs;

  SQL>select * from user_tab_privs;

  檢視使用者下所有的表

  SQL>select * from user_tables;

  1、使用者

  檢視當前使用者的預設表空間

  SQL>select username,default_tablespace from user_users;

  檢視當前使用者的角色

  SQL>select * from user_role_privs;

  檢視當前使用者的系統許可權和表級許可權

  SQL>select * from user_sys_privs;

  SQL>select * from user_tab_privs;

  顯示當前會話所具有的許可權

  SQL>select * from session_privs;

  顯示指定使用者所具有的系統許可權

  SQL>select * from dba_sys_privs where grantee='GAME';

  2、表

  檢視使用者下所有的表

  SQL>select * from user_tables;

  檢視名稱包含log字元的表

  SQL>select object_name,object_id from user_objects

  where instr(object_name,'LOG')>0;

  檢視某表的建立時間

  SQL>select object_name,created from user_objects where object_name=upper('&table_name');

  檢視某表的大小

  SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

  where segment_name=upper('&table_name');

  檢視放在ORACLE的記憶體區裡的表

  SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;

  3、索引

  檢視索引個數和類別

  SQL>select index_name,index_type,table_name from user_indexes order by table_name;

  檢視索引被索引的欄位

  SQL>select * from user_ind_columns where index_name=upper('&index_name');

  檢視索引的大小

  SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

  where segment_name=upper('&index_name');

  4、序列號

  檢視序列號,last_number是當前值

  SQL>select * from user_sequences;

  5、檢視

  檢視檢視的名稱

  SQL>select view_name from user_views;

  檢視建立檢視的select語句

  SQL>set view_name,text_length from user_views;

  SQL>set long 2000; 說明:可以根據檢視的text_length值設定set long 的大小

  SQL>select text from user_views where view_name=upper('&view_name');

  6、同義詞

  檢視同義詞的名稱

  SQL>select * from user_synonyms;

  7、約束條件

  檢視某表的約束條件

  SQL>select constraint_name, constraint_type,search_condition, r_constraint_name

  from user_constraints where table_name = upper('&table_name');

  SQL>select c.constraint_name,c.constraint_type,cc.column_name

  from user_constraints c,user_cons_columns cc

  where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')

  and c.owner = cc.owner and c.constraint_name = cc.constraint_name

  order by cc.position;

  8、儲存函式和過程

  檢視函式和過程的狀態

  SQL>select object_name,status from user_objects where object_type='FUNCTION';

  SQL>select object_name,status from user_objects where object_type='PROCEDURE';

  檢視函式和過程的原始碼

  SQL>select text from all_source where owner=user and name=upper('&plsql_name');

 

-------------------------------------------------------------------------------------------------------------------------------------

 

ORACLE:


----常用資料字典

USER_   
  記錄使用者物件的資訊,如user_tables包含使用者建立的所有表; user_views,user_constraints等;
ALL_   
記錄使用者物件的資訊及被授權訪問的物件資訊;
DBA_
  記錄資料庫例項的所有物件的資訊,如DBA_USERS包含資料庫例項中
所有使用者的資訊,DBA的資訊包含user和all的資訊;
V$ 當前例項的動態檢視,包含系統管理和最佳化使用的檢視;
GV_ 分佈環境下所有例項的動態檢視,包含系統管理和最佳化使用的檢視, 這裡的GV表示 Global v$的意思;
 如當前分佈環境下啟動多個例項:
set line 160 (每行顯示160個字元)
select inst_id,sid,lmode from gv$lock where inst_id = 1 or inst_id=2;

oracle常用資料字典,oracle10增加了不少資料字典10g r1 有1713個,r2有1870個:
1 基本的資料字典:
DBA_TABLES 所有使用者的所有表的資訊;
DBA_TAB_COLUMNS 所有使用者的表的列(欄位)資訊;
DBA_VIEWS 所有使用者的所有檢視資訊;
DBA_SYNONYMS 所有使用者同義詞資訊;
DBA_SEQUENCES 所有使用者序列資訊;
DBA_CONSTRAINTS 所有使用者的表約束資訊;
DBA_INDEXES 所有使用者索引的簡要資訊;
DBA_IND_COLUMNS 所有使用者索引的列資訊;
DBA_TRIGGERS 所有使用者觸發器資訊 ;
DBA_SOURCE所有使用者儲存過程原始碼資訊;
DBA_PROCEDUS 所有使用者儲存過程;
DBA_SEGMENTS 所有使用者段(表,索引,Cluster)使用空間資訊;
DBA_EXTENTS 所有使用者段的擴充套件段資訊;
DBA_OBJECTS 所有使用者物件的基本資訊(包括素引,表,檢視,序列等);
CAT 當前使用者可以訪問的所有的基表 ;
TAB 當前使用者建立的所有基表,檢視,同義詞等;
DICT 構成資料字典的所有表的資訊;


2 與資料庫元件相關的資料字典:
資料庫:
  V$DATABASE 同義詞 V_$DATABASE,記錄系統的執行情況;
表空間:  
DBA_TABLESPACES 記錄系統表空間的基本資訊;
DBA_DATA_FILES 記錄系統資料檔案及表空間的基本資訊;
DBA_FREE_SPACE 記錄系統表空間的剩餘空間的資訊;
控制檔案:
V$CONTROLFILE 記錄系統控制檔案的路徑資訊;
V$PARAMETER 記錄系統各引數的基本資訊;
v$CONTROLFILE_RECORD_SECTION 記錄系統控制執行的基本資訊;
資料檔案:
DBA_DATA_FILES 記錄系統資料檔案及表空間的基本資訊;
v$DATAFILE 記錄來自控制檔案的資料檔案資訊;
v$FILESTAT 記錄資料檔案讀寫的基本資訊 ;




    
--檢視VGA資訊:

show sga;

select * from v$sgastat;

--可以透過以下幾個動態效能檢視檢視資訊:
V$sysstat 系統統計資訊
V$sesstat 使用者會話統計資訊  
V$pgastat 顯示記憶體使用統計資訊
V$sql_workarea SQL遊標所用工作區的資訊
V$ sql_workarea_active 當前系統工作區的資訊

--在V$process動態效能檢視中可以查詢到每個Oracle程式的PGA分配的記憶體和已使用的記憶體情況,
--其中PGA_used_mem表示已使用的,pag_alloc_mem表示已分配的,pga_max_men表示PGA的最大值。

SQL> select pid,pga_used_mem,pga_alloc_mem,pga_max_mem from v$process;

--檢視後臺程式:

SELECT * FROM v$bgprocess WHERE paddr <> '00';

--檢視所有的表空間;

SQL> select tablespace_name from dba_data_files order by tablespace_name;

--檢視錶空間的名字及大小:
SQL> select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size   
from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name   
group by t.tablespace_name;

--建立表空間的語法是:
CREATE TABLESPACE tablespacename
DATAFILE 'filename' [SIZE integer [K|M]]  
[AUTOEXTEND [OFF|ON]];
--建立有多個資料檔案的表空間:
SQL> create tablespace SALES
datafile 'd:/sales/SALES_DATA01.dbf' size 10m autoextend on next 10m maxsize 100m,
'd:/sales/SALES_DATA02.dbf' size 10m autoextend on next 10m maxsize
unlimited,'d:/sales/SALES_DATA03.dbf' size 10m;

--檢視錶空間的大小;

SQL> SELECT TABLESPACE_NAME,SUM(BYTES)/1024/1024 MB FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME;

--檢視錶空間中資料檔案存放的路徑:

SQL> SELECT TABLESPACE_NAME, BYTES/1024/1024 FILE_SIZE_MB, FILE_NAME FROM DBA_DATA_FILES;


刪除表空間:
SQL> drop tablespace worktbs including contents;
表空間已丟棄。

再次查詢表空間會發現已經沒有worktbs表空間了。
如果我們再次刪除表空間會出現什麼後果?
刪除一個不存在的表空間:
SQL> drop tablespace worktbs including contents;
drop tablespace worktbs including contents
*
ERROR 位於第 1 行:
ORA-00959: 表空間'WORKTBS'不存在
o 為表空間增加資料檔案
SQL> alter tablespace sales add datafile 'd:/oracle/oradata/test/testtablespace/
sales_data04.dbf' size 10m autoextend on next 10m maxsize 100m,
'c:/oracle/oradata/test/testtablespace/sales_data05.dbf' size 10m autoextend on next 10m maxsize unlimited, 'c:/oracle/oradata/test/testtablespace/sales_data06.dbf' size 10m;

表空間已更改。
o 修改資料檔案的大小
o 在資料檔案中的可用空間未使用完時,允許對資料檔案進行壓縮,當壓縮空間大小超過未使用空間大小時,將會產生錯誤。
SQL> alter database datafile 'c:/oracle/oradata/test/testtablespace/SALES_DATA04.dbf' resize 30m;

資料庫已更改。
o 關閉表空間資料檔案的自動擴充套件屬性
o alter database
o datafile 'c:/SALES_DATA04.dbf' ,
o 'c:/SALES_DATA05.dbf',
o 'c:/SALES_DATA06.dbf'
o autoextend off;
o 開啟表空間資料檔案的自動擴充套件屬性
o alter database
o datafile 'c:/SALES_DATA04.dbf' ,
o 'c:/SALES_DATA05.dbf',
o 'c:/SALES_DATA06.dbf'
o autoextend on;

o 修改表空間屬性(離線)
o alter tablespace sales offline;   
o 修改表空間屬性(線上)
o alter tablespace sales online;
o 修改表空間屬性(只讀)
o alter tablespace sales read only;
o 修改表空間屬性(讀寫)
o alter tablespace sales read write;
o 如下系統表空間不得設定為 offline 或者 read only
o system , temp,undo ,undotbs  

o 移動表空間的資料檔案
o 1. 使表空間離線(系統表空間不得離線,故不得移動)
o 2. 修改資料檔案的名稱
o 3. 執行 alter tablespace rename datafile 命令
o 4. 使表空間聯機
o 第一步:alter tablespace sales offline;   
o 第二步:物理移動資料檔案到目的地(可以是表空間的
o 部分資料檔案,可以修改資料檔案的名稱)
o 第三步: 邏輯移動,修改控制檔案的內容

o alter tablespace sales rename  
o datafile 'c:/sales01.dbf' to 'd:/sales02.dbf'
o --可以有多個資料檔案,但是原始檔
o 要在to的左邊,目的檔案要在to 右邊,檔名稱之間逗號分割.
o 第四步: 將表空間聯機
o alter tablespace sales online;
o 第五步:查詢dba_data_files確認

o 建立使用者,指定預設表空間,磁碟配額
o create user rose identified by rose default
o tablespace sales quota 10m on sales;
o 給使用者授權
o grant connect,resource,dba to rose;
o 使用者登入
o connect rose/rose
o 建立表
o create table emp(eid number)  
o 該表預設放在表空間 sales 中
o 查詢使用者的儲存限額dba_ts_quotas  
o 刪除表空間(如果該表空間為空)
o drop tablespace sales ;
o 刪除表空間的同時刪除資料檔案(不為空)
o drop tablespace sales including contents and datafiles;
o 建立表的同時指定該表的儲存位置
o create table mytab(tid int ) tablespace sales;

o 刪除表空間(如果該表空間為空)
o drop tablespace sales ;
o 刪除表空間的同時刪除資料檔案(不為空)
o drop tablespace sales including contents and datafiles;
o 建立表的同時指定該表的儲存位置
o create table mytab(tid int ) tablespace sales;


o 檢視當前使用者每個表佔用空間的大小:
o select segment_name,sum(bytes)/1024/1024 from user_extents group by segment_name  
o 檢視每個表空間佔用空間的大小:
o select tablespace_name,sum(bytes)/1024/1024 from dba_segments group by tablespace_name  


只有用合法的使用者帳號才能訪問Oracle資料庫
Oracle 有幾個預設的資料庫使用者   
Scott/tiger

建立一個名稱為 martin 的使用者,其密碼為 martinpwd
CREATE USER MARTIN IDENTIFIED BY martinpwd
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;

GRANT 命令可用於為使用者分配許可權或角色;
CONNECT角色允許使用者連線至資料庫,並建立資料庫物件。
GRANT CONNECT TO MARTIN;  

RESOURCE角色允許使用者使用資料庫中的儲存空間。
GRANT RESOURCE TO MARTIN;  

此係統許可權允許使用者在當前模式中建立序列,此許可權包含在CONNECT角色中。
GRANT CREATE SEQUENCE TO MARTIN;  

GRANT CREATE SESSION TO MARTIN;

GRANT CREATE TABLE TO MARTIN;

GRANT CREATE VIEW TO MARTIN;

GRANT CREATE SEQUENCE TO MARTIN;



授予使用者 MARTIN 操作emp表物件的許可權 :
允許使用者查詢 TEST 表的記錄
GRANT SELECT ON EMP TO MARTIN;  

允許使用者更新 TEST 表中的記錄
GRANT UPDATE ON EMP TO MARTIN;  

允許使用者插入、刪除、更新和查詢 TEST 表中的記錄
GRANT ALL ON EMP TO MARTIN;  


ALTER USER 命令可用於更改口令:
修改 MARTIN 使用者的密碼:
ALTER USER MARTIN IDENTIFIED BY martinpass;
DROP USER 命令用於刪除使用者:  
刪除 MARTIN 使用者模式:
DROP USER MARTIN CASCADE;


alter session set nls_date_format='yyyy-mm-dd';



1開始建立資料庫:

----------------使用工具建立 (步驟省略)

2登陸新資料庫  
run/sqlplus sys/sys@ACCP
(此時如果登陸老資料庫使用 sqlplus sys/sys@orac9i)
2建立表空間:sales (用來儲存accp資料庫的資料)
create tablespace TestTBS datafile 'd:/sales/sales001.dbf' size 10m autoextend on next 10m maxsize 30m,'d:/sales/sales002.dbf' size 10m autoextend on next 10m maxsize 30m
3建立使用者accp並授予許可權
grant connect to accp;
grant resource to accp;
4以accp使用者身份登陸accp資料庫
create table person(pid number(5),pname varchar2(20)) tablespace testtbs;
insert into person values(1001,'TOM');
commit;
select table_name,tablespace_name from user_tables;
select * from person;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29913993/viewspace-1320026/,如需轉載,請註明出處,否則將追究法律責任。

相關文章