檢視目前使用者的預設表空間
sql>select username,default_tablespace from user_users;
檢視目前使用者的角色
sql>select * from user_role_privs;
檢視目前使用者的系統權限和表級權限
sql>select * from user_sys_privs;
結果可以是:
username privilege adm
------------------------------ ---------------------------------------- ---
misps create trigger no
misps create procedure no
misps create sequence no
misps create view no
misps unlimited tablespace no
misps create table no
misps create session no
sql>select * from user_tab_privs;
檢視使用者下所有的表
sql>select * from user_tables;
此外:關于查詢表資訊的還可以有:
select * from all_tables;系統裡有權限的表
select * from dba_tables; 系統表
select * from user_tables; 目前使用者下的表
1、使用者
檢視目前使用者的預設表空間
顯示目前會話所具有的權限
sql>select * from session_privs;
顯示指定使用者所具有的系統權限
sql>select * from dba_sys_privs where grantee='game';
2、表
此外還可以通過:sql>select * from tab; --找到使用者下的所有表
此外還可以通過:sql>drop table ***; --用drop 指令一個個删除即可。
檢視名稱包含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');
檢視索引的大小
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 記錄系統控制運作的基本資訊;
資料檔案:
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表空間了。
如果我們再次删除表空間會出現什麼後果?
删除一個不存在的表空間:
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 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 第五步:查詢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 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;