10gocm->session5->數據庫管理實驗

Oracle數據庫管理實驗


一 傳輸表空間
二 創建分區表和分區索引
三 FGA細粒度審計
四 監控索引使用情況
五 創建含特殊字段類型的表
六 Flashback閃回技術


一 傳輸表空間,將ocmdb庫的tstrans表空間遷移到ocmdb02庫中
場合:1.適用於OLAP數據倉庫應用  2.數據遷移 比exp/imp速度快,不僅遷移數據同時遷移元數據
原理:只導出表空間的元數據(即結構信息),導入目標數據庫,把表空間設置爲只讀,把對應的數據文件複製到目標系統的目標目錄,掛載上導入的表空間,在把表空間設置爲讀寫。
官方文檔:Administrator’s Guide-> 8 Managing Tablespace -> Transport Tablespace Between databases
          SQL Reference -> CREATE DIRECTORY
清理環境
drop user tstrans cascade;
drop tablespace tstrans including contents and datafiles;
1.初始化實驗環境
sqlplus sys/sys@ocmdb1521 as sysdba
create tablespace tstrans datafile '/u01/oracle/oradata/ocmdb/tsport01.dbf' size 20m autoextend off;
create user tstrans identified by tstrans default tablespace tstrans;
grant connect,resource to tstrans;
conn tstrans/tstrans
create table t1 (a int) tablespace tstrans;
insert into t1 values (100); 
commit;
create index idx_t1 on t1 (a) tablespace tstrans;
select index_name,table_name,tablespace_name from user_indexes where table_name='T1';
conn / as sysdba
select tablespace_name,segment_name,segment_type from dba_segments where tablespace_name='TSTRANS';  查看tstrans表空間有哪些對象
2.檢查tstrans表空間是否違反約束條件
exec dbms_tts.transport_set_check('TSTRANS',true); 
例如表在A表空間,索引在B表空間,如果只傳輸A的話,就會違反自包含約束條件,表上的索引就會失效,傳輸不完整,解決方法:同時傳輸A和B兩個表空間。
select * from transport_set_violations;    查看違反約束列表,沒有記錄爲正常
3.設置TSTRANS爲只讀表空間,即把所有數據文件都設置成只讀狀態。
alter tablespace tstrans read only; 
4.使用exp或expdp的transport_tablespace=y參數導出表空間的元數據 即結構信息
exp -help  打開幫助文檔可以查看其選項說明
exp userid=\'/ as sysdba\' transport_tablespace=y tablespaces=tstrans file=/home/oracle/exp_tstrans.dmp
使用SYS用戶只導出tstrans表空間的元數據(結構信息),而不是真實數據,因此容量比較小
scp exp_tstrans.dmp ocm02:/home/oracle 傳輸到第二臺機器上
數據泵導出方式選做
【expdp -help 
 create directory dir_home as '/home/oracle'; 
 grant read,write on directory dir_home to public;
 expdp system/oracle directory=dir_home dumpfile=expdp_tstrans.dmp transport_tablespaces=tstrans transport_full_check=y   

5.拷貝數據文件(表空間真正的數據)只有數據文件爲只讀狀態纔可以複製,而且不用停庫
scp tstrans01.dbf ocm02:/u01/oracle/oradata/ocmdb02/
6.imp或impdp的傳輸表空間導入
LEO2庫的準備工作
create user tstrans identified by tstrans; 
grant connect,resource to tstrans; 
使用imp導入方式   默認以追加方式插入表空間元數據+數據文件
imp userid=\'/ as sysdba\' file=/home/oracle/exp_tstrans.dmp fromuser=tstrans  touser=tstrans transport_tablespace=y tablespaces=tstrans datafiles=/u01/oracle/oradata/ocm02/tstrans01.dbf


【使用impdp導入方法 選做】 
create directory dir_home as '/home/oracle';
grant read,write on directory dir_home to public;
impdp system/oracle directory=dir_home dumpfile=expdp_tstrans.dmp remap_schema=(tstrans:tstrans)    對象從一個schema加載到另一個schema
transport_datafiles=/u01/oracle/oradata/ocm02/tstrans01.dbf        導入哪個數據文件
檢查表空間是否導入成功
col tablespace_name for a15
col segment_name for a15
col segment_type for a15
select tablespace_name,segment_name,segment_type from dba_segments where tablespace_name='TSTRANS';
conn tstrans/tstrans
select * from t1;  
7.將ocmdb實例和ocmdb02實例表空間調整爲可讀寫狀態
sqlplus sys/sys@ocmdb1521 as sysdba
conn / as sysdba
select tablespace_name,status from dba_tablespaces;
alter tablespace tstrans read write; 
select tablespace_name,status from dba_tablespaces;
sqlplus sys/sys@ocmdb021521 as sysdba
conn / as sysdba
select tablespace_name,status from dba_tablespaces;
alter tablespace tstrans read write;
select tablespace_name,status from dba_tablespaces;


二 創建分區表和分區索引
官方文檔:
Administrator’s Guide –> 17 Managing Partitioned Tables and Indexs 
Data Warehousing Guide -> 5 Partitioning in Data Warehouses


場合:數據量很大,要求檢索範圍小,效率高
優點:DBA管理靈活性高,基於分區刪除、插入
缺點:跨分區檢索效率很低,但可創建一個全局索引global來改善性能
全局索引global:默認刪除分區,則全局索引失效,一個分區表只有一個全局索引
本地索引local:一個分區一個索引,有幾個分區就有幾個索引
要求:我們創建一個分區表,共有4個分區,每個分區獨立使用一個表空間
      使用非標準塊,塊大小16k


1.設置非標準塊
ocm01


alter system set db_16k_cache_size=80M;      設置非標準塊16K緩衝區,用於存放非標準塊
show parameter db_16k_cache_size
作用:用於減少物理I/O讀寫次數,原本讀100次可以完成的數據,現在讀50次就完成了
create user ocm01 identified by ocm01;
grant dba to ocm01;


2.創建4個表空間,一個分區對應一個表空間
conn ocm01/ocm02


drop table t2_part;
drop index idx_t2_part;
drop tablespace part1 including contents and datafiles;
drop tablespace part2 including contents and datafiles;
drop tablespace part3 including contents and datafiles;
drop tablespace part4 including contents and datafiles;
create tablespace part1 datafile '/u01/oracle/oradata/ocmdb/disk1/part1_01.dbf' size 50M
extent management local      
blocksize 16k; 
create tablespace part2 datafile '/u01/oracle/oradata/ocmdb/disk2/part2_01.dbf' size 50M
extent management local      
blocksize 16k; 
create tablespace part3 datafile '/u01/oracle/oradata/ocmdb/disk3/part3_01.dbf' size 50M
extent management local      
blocksize 16k; 
create tablespace part4 datafile '/u01/oracle/oradata/ocmdb/disk4/part4_01.dbf' size 50M
extent management local      
blocksize 16k; 
select * from v$tablespace;
2.構造分區表數據
conn tstrans/tstrans
drop table t2 purge;
create table t2 (itemid number(10),name varchar2(10),itemdate date);
create index idx_t2 on t2(itemid);
insert into t2 values (1,'apple1',to_date('2000-02-01','yyyy-mm-dd'));
insert into t2 values (2,'apple2',to_date('2000-03-01','yyyy-mm-dd'));
insert into t2 values (3,'apple3',to_date('2002-04-01','yyyy-mm-dd'));
insert into t2 values (4,'apple4',to_date('2002-05-01','yyyy-mm-dd'));
insert into t2 values (5,'apple5',to_date('2002-06-01','yyyy-mm-dd'));
insert into t2 values (6,'apple6',to_date('2010-07-01','yyyy-mm-dd'));
insert into t2 values (7,'apple7',to_date('2010-08-01','yyyy-mm-dd'));
insert into t2 values (8,'apple8',to_date('2012-09-01','yyyy-mm-dd'));
insert into t2 values (9,'apple9',to_date('2012-10-01','yyyy-mm-dd'));
insert into t2 values (10,'apple10',to_date('2013-11-01','yyyy-mm-dd'));
commit;
select * from t2;                                插入10條記錄,顯示出來
3.ocm01用戶下創建分區表
conn ocm01/ocm01
CREATE TABLE t2_part
   PARTITION BY RANGE (itemdate)
     ( PARTITION p1 VALUES LESS THAN (to_date('2002-01-01','yyyy-mm-dd'))
        TABLESPACE part1,          
       PARTITION p2 VALUES LESS THAN (to_date('2010-01-01','yyyy-mm-dd'))
        TABLESPACE part2,         
       PARTITION p3 VALUES LESS THAN (to_date('2012-01-01','yyyy-mm-dd'))
        TABLESPACE part3,         
       PARTITION p4 VALUES LESS THAN (to_date('2013-01-01','yyyy-mm-dd'))
        TABLESPACE part4,          
       PARTITION other VALUES LESS THAN (maxvalue)
        TABLESPACE part4)         
   as select * from tstrans.t2;      
查看分區表數據
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
select * from t2_part; 
查看某一個分區
select * from t2_part partition (p1);
4.創建hash全局分區索引
hash分區索引:均勻打散後存放數據,拿一列作hash打散,均勻分佈在4個分區上,每個分區在不同表空間上的記錄數都差不多,併發讀併發寫
官方文檔:SQL Reference -> CREATE TABLE and CREATE INDEX
conn ocm01/ocm01
注:把全局分區索引(按照name列做hash打散)均勻分成4份,每份都保存在LEOINDEX表空間裏
create unique index idx_t2_part on t2_part (name,itemid)global partition by hash (name) partitions 4 tablespace LEOINDEX parallel 4;
select index_name,index_type,table_name from user_indexes where table_name='T2_PART';
截斷一個分區,需要保證全局索引可用
1.默認情況下,增加、刪除分區>全局索引失效
2.truncate  操作會影響全局索引   delete  操作不會影響全局索引
alter table t2_part truncate partition p1 update global indexes;
select * from t2_part partition (p1);
【update global indexes 這個關鍵字可以在操作之後重建全局索引】
檢查全局索引是否有效
select index_name,status,partitioned from dba_indexes where table_name='T2_PART';


三 FGA細粒度審計  
場合:可以查出誰 什麼時候 使用什麼語句 刪除的表
Oracle 細粒度審計是安全領域的一個分支,它可以追溯數據庫的歷史操作,從而保證所有的操作都是安全可靠可控,FGA是基於包來實現的
官方文檔:Security Guide -> 12 Configuring and Administering Auditing -> Fine-Grained Auditing
PL/SQL Packages and Types Reference –> 40 DBMS_FGA
要求:使用FGA技術對錶進行審計
1.清理環境
只有管理員纔可以刪除審計
DBMS_FGA.DROP_POLICY(
   object_schema  VARCHAR2,    用戶名(如果爲空默認當前登陸用戶)
   object_name    VARCHAR2,    待審計的對象名(表名字)
   policy_name    VARCHAR2 );   審計名(必須是唯一值)
execute DBMS_FGA.DROP_POLICY(object_schema=>'ocm01',object_name=>'t',policy_name=>'audit_t');
conn ocm01/ocm01
drop table t purge;           刪除待審計的表
2.創建待審計的T表
conn ocm01/ocm01
create table t (x number(10),y varchar2(20));   創建待審計的表t    
3.創建審計策略
conn / as sysdba
begin
dbms_fga.add_policy (
   object_schema      =>  'ocm01',           審計誰
   object_name        =>  't',              審計誰的表
   policy_name        =>  'audit_t',        審計策略的名字
   audit_condition    =>  'x >= 100',       觸發審計的條件 x>=100
   audit_column       =>  'x',              審計表中的哪個列‘x,y’
   enable             =>   TRUE,            審計立刻生效
   statement_types    =>  'INSERT,UPDATE,DELETE,SELECT');  觸發審計的語句對這些語句都啓動審計
end;                     
/
4.查詢確認FGA策略是否生效
col object_schema for a20
col object_name for a15
col policy_name for a13
col enabled for a3
select object_schema,object_name,policy_name,enabled from dba_audit_policies;
5.插入測試記錄符合觸發審計的條件
conn leo1/leo1
insert into t values (10,'first');
insert into t values (100,'dfs');
insert into t values (200,'dsf');      
insert into t values (300,'sdf'); 
insert into t values (400,'sdgdg');      
insert into t values (500,'sdg');
insert into t values (600,'sdgsdgs');
select * from t;          查看審計表的內容  
6.查看審計結果,默認會把審計結果放在SYS.FGA_LOG$基表中
注:審計會對INSERT,UPDATE,DELETE,SELECT這四種語句都做檢查,並且記錄誰 操作的哪個表 執行的什麼語句  
select OBJ$SCHEMA,OBJ$NAME,POLICYNAME,LSQLTEXT from SYS.FGA_LOG$;
set lines 200
col sql_text for a35
col object_schema for a15
select object_schema,object_name,policy_name,sql_text from dba_common_audit_trail;     
顯示所有審計結果


四 監控索引使用情況        
場合:監控表中無用索引刪除之
官方文檔:Administrator’s Guide -> 16 Managing Indexes -> Monitoring Index Usage
conn ocm01/ocm01
drop table t4;
create table t4 as select * from dba_objects;
create index idx_t4 on t4(object_id);
開啓LEO1下idx_t4索引的監控
alter index idx_t4 monitoring usage; 
停止LEO1下idx_t4索引的監控
alter index idx_t4 nomonitoring usage; 
select * from t4 where object_id=5000;
查看v$object_usage視圖獲得索引被使用情況
set linesize 400                      設置環境
col index_name for a10
col table_name for a10
col start_monitoring for a20
col end_monitoring for a20
select * from v$object_usage;   
說明: monitoring字段爲YES 表示此索引已經被監控,NO未被監控
       used字段爲YES  表示此索引已經被使用,NO未被使用
       start_monitoring與end_monitoring 表示上次監控區間


五 創建含特殊字段類型的表
創建具有ROWID及時間戳類型字段的表並插入數據
官方文檔:SQL Reference -> 2 Basic Elements of Oracle SQL -> Datatypes ->搜索“ROWID”和“TIMESTAMP” WITH LOCAL TIME ZONE Datatype
1.創建LEONARDING_R表並初始化數據
conn ocm01/ocm01
構造數據環境
create table ocm01_text 
(
text1 varchar2(10),
text2 varchar2(10),
text3 date,
text4 varchar2(50)
);
插入含有‘hugh’關鍵字的記錄
insert into ocm01_text values ('hugh','name',sysdate,'hugh');
insert into ocm01_text values ('hugh2','name',sysdate,'hugh');
insert into ocm01_text values ('hugh3','name',sysdate,'hugh');
insert into ocm01_text values ('hugh4','name',sysdate,'hugh');
commit;
select * from ocm01_text;
create table ocm01_r (text rowid,insert_time timestamp with local time zone) tablespace users;  
rowid 字段類型
timestamp with local time zone 時間戳和本地時區字段類型


2.向LEONARDING_R表插入記錄
在leonarding_text表中檢索記錄,如果1條記錄中有包含3個或者以上的“Leonarding”關鍵字,就把這條記錄rowid和時間戳插入leonarding_r表
insert into ocm01_r (text,insert_time) select rowid,current_timestamp from ocm01_text;


注:current_timestamp函數:返回根據時區轉換過的“日期”和“時間”,返回的秒是系統的
    sysdate          函數:返回操作系統的日期和時間
    length  字符串長度函數:取字段長度
commit; 
select * from ocm01_r;
drop table ocm01_r;


六 Flashback閃回技術
場景:當誤刪除時如何恢復數據
官方文檔:Application Developer’s Guide - Fundamentals -> 10 Developing Flashback Application -> Using Flashback Query (SELECT … AS OF)
1.Flashback Query閃回查詢數據
原理:閃回查詢使用的是undo表空間裏存放的前映像
構造環境
drop table t5 purge;
create table t5 (x int);
insert into t5 values(1);
insert into t5 values(2);
insert into t5 values(3);
commit;
select * from t5;
2.爲構造後續的閃回查詢查詢當前的時間和scn號
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
查詢當前系統時間
select sysdate from dual;
SYSDATE
-------------------
2014-01-14 21:27:23


查詢當前系統SCN號
select dbms_flashback.get_system_change_number from dual;      331299
3.刪除數據
delete from t5 where x=1;
commit;
select * from t5;
4.兩種方法創建視圖構造閃回查詢刪除之前的數據
1)第一種方法:使用時間戳來構造閃回查詢視圖
create view v_t5_timestamp as select * from t5 
as of timestamp to_timestamp('2014-01-14 21:27:23','yyyy-mm-dd hh24:mi:ss');


2)第二種方法:使用SCN構造閃回查詢視圖
create view v_t5_scn as select * from t5 as of scn 331299;
注:scn 比 timestamp 更精確
查詢視圖閃回內容
select * from v_t5_timestamp;   
select * from v_t5_scn;
drop view v_t5_timestamp;
drop view v_t5_scn;
到此,兩種構造視圖的方法都順利的獲得了閃回查詢的數據
2.一張表被反覆多次刪除,要求恢復到指定的數據版本
原理:清空回收站
purge recyclebin;        
create table t6 (x int); 
insert into t6 values (1); 
commit;
select * from t6;  
drop table t6; 
create table t6 (x int); 
insert into t6 values (1); 
insert into t6 values (2);
commit; 
select * from t6; 
drop table t6; 
查詢回收站數據字典
select object_name,original_name,type from recyclebin; 
show recyclebin
獲得t6表被drop的兩個版本中哪個是我們需要恢復的對象,恢復有1條記錄的t6表
select * from "BIN$7+8KsHBFHoPgQKjAZRUk9w==$0";
select * from "BIN$7+8KsHBEHoPgQKjAZRUk9w==$0"; 
閃回指定的版本->閃回同時重命名
flashback table "BIN$7+8KsHBEHoPgQKjAZRUk9w==$0" to before drop rename to t6_new;
drop table t6_new;


總結自ocm實驗選講








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章