一、日志管理
1.強制日志切換(forcing log switches) alter system switch logfile; 2.強制執行檢查點(forcing checkpoints) alter system checkpoint; 3.增加一個重做日志組 (adding online redo log groups) alter fatabases add logfile [ group 4 ] ('/disk3/log4a.rdo','/disk4/log4b.rdo') size 1M; 4.增加一個重做日志文件( adding online redo log memebers ) alter databases add logfile member '/disk3/log1b.rdo' to group 1, '/disk4/log2b.rdo' to group 2; 5.改變重做日志文件名 (changes the name of the online redo logfile) alter databases rename file 'c:/oracle/oradata/oradb/redo01.log' to 'c:/oracle/oracdata/redo01.log'; 6.刪除重做日志 (drio online redo log groups) alter database drop logfile groupp 3; 7.刪除重做日志組(drop online redo log members) alter database drop logfile member 'c:/oracle/readata/red01.log'; 8.清空重做日志(clearing online redo log files) alter database clear [unarchived] logfile 'c:/oracle/log2a.rdo'; 9.使用logminer分析重做日志文件(using logminer analyzing redo logfiles) a. in the init.ora specify utl_file_dir = ' ' b. execute dbms_logmnr_d.build('oradb.ora','c:\oracle\oradb\log'); c. execute dbms_logmnr_add_logfile('c:\oracle\oradata\oradb\redo01.log',dbms_logmnr.new); d. execute dbms_logmnr.add_logfile('c:\oracle\oradata\oradb\redo02.log',dbms_logmnr.addfile); e. execute dbms_logmnr.start_logmnr(dictfilename=>'c:\oracle\oradb\log\oradb.ora'); f. select * from v$logmnr_contents(v$logmnr_dictionary,v$logmnr_parameters v$logmnr_logs); g. execute dbms_logmnr.end_logmnr;
二、表空間管理
1.創建表空間(create tablespaces) create tablespace table_name datafile 'c:\oracle\oradata\file1.dbf' size 100M minimum extent 550k [logging/nologging] default storage (initial 500k next 500k maxextents 500 pctinccease 0) [online/offline] [permanent/temporary] [extent_management_clause] 2.創建本地管理的表空間( locally managed tabalespace ) create tablespace user_data datafile 'c:\oracle\oradata\user_data001.dbf' size 500M extent management local uniform size 10M; 3. 創建臨時表空間(temporary tablespace) create temporary tablespace temp tempfile 'c:\oracle\oradata\temppp01.dbf' size 500M extent management local uniform size 10M; 4.改變表空間的存儲參數(change the storage setting) alter tablespace app_data minimum extent 2M; or alter tablespace app_date default storage( initial 2M next 2M maxextents 999); 5.使表空間離線或連線(taking tablespace offline or online) alter tablespace app_data offline; or alter tablespace app_data online; 6.設置表空間為只讀、可寫模式(read_only tablespace) alter tablespace app_data read only | write; 7.刪除表空間(droping tablespace) drop tablespace app_data including contents; 8.允許數據文件自動擴張(enableing automatic extension of data files) alter tablespace app_data add datafile 'c:\oracle\oradata\app_data01.dbf' size 200M autoextend on next 10M maxsize 500M; 9.手動改變數據文件大小(change the size fo data files manually) alter database datafile 'c:\oracle\oradata\app_data.dbf' resize 200M; 10.改變表空間中的數據文件(mocing data files:alter tablespace) alter tablespace app_data rename datafile 'c:\oracle\oradata\app_data.dbf' to 'c:\oracle\app_data.dbf'; 11.修改數據庫中的數據文件(moving data files:alter database) alter database rename file 'c:\oracle\oradata\app_data.dbf' to 'c:\oracle\app_data.dbf';
三、表
1.創建表 create table table_name ( column datatype, [column datatype] ..... ); tablespace tablespace_name [ ppctfree integer ] [ pctused integer ] [ initrans integer ] [ maxtrans integer ] storage( initial 200k next 200k ppctincrease 0 maxextents 50) [ logging | nologging ] [ cache | nocaahe ] 2.復制一個已存在的表(copy an existing table) create table table_name [ logging | nologging ] as subquery 3.創建一個臨時表(create temporary table) create global temporary table xay_temp as select * from xay; on commint preserve rows/on commit delete rows 4.pctfree 和 pctused 參數計算公式 pctfree = (average row size - initial row size)*100/average row size pctused = 100-pctfree-(average row size * 100/available data space) pctfree : 指定表內每個數據塊中空間的百分比。pctfree的值必須介於0和99之間。如果為零,表示可以通過插入新行來填充整個塊。
缺省值為10.此值表示每個塊中保留着10%的空間,用於更新現有的行以及插入新行,每個塊最多可以填充到90% pctused : 指定為表內每個數據塊的已用空間的最小百分比。如果一個塊的已用空間低於pctused,則可在該塊中插入行。pctused的值介於0和99之間的整數,缺省值為40. 結合pctfree 和 pctused 就可以確定將新行插入到現有數據塊中,還是插入到新塊中。這兩個參數值的和必須小於或等於100.使用這兩個參數可以更有效地利用表內的空間。 設置 pctfree 和pctused pctfree 值越高,可為數據庫塊內的更新提供的空間就越大。如果表存在下面兩種情況,則應設置一個更高的值:?某些列最初為null,后來更新為某個值?
某些列由於更新,大小可能增加pctfree的值越高,塊密度就越低,即每個塊容納的行數就越少。上面的公式確保塊中有足夠的空間供行增長使用。 pctused 以確保只有在具備足夠空間來容納一個平均大小的行時才將塊返回到空閑列表中。
如果空閑列表中的某個塊沒有足夠的空間來插入一行,oracle服務器將查找空閑列表中的下個塊、直到找到具備足夠空間的塊或者到達列表的末尾,這種線性掃描才會結束。
使用給定的公式可以增加找到具有所有需空閑空間的塊的概率,從而縮短掃描空閑列表時間。 注:可以使用 analyze table 命令估算平均行大小的值。 注:oracle9i “自動段空間管理” 功能可替代 pctused、freelists 和 freelist groups. 5.改變存儲和塊利用率參數(change storage and block utilization parameter) alter table table_name pctfree=30 pctused=50 storage(next 500k minextents 2 maxextents 100); 6.手工分配區間(extents)(manually allxoating extents) alter table table_name allocate extent (size 500k datafile 'c:/oracle/data.dbf'); 7.改變表的所屬表空間(move tablespace) alter table employee move tablespace users; 8.釋放表中未用空間(deallocate of unused space) alter table table_name deallocate unused [ keep integer ] 9.截斷表(truncate)(truncate a table ) truncate table table_name; 截斷一個表將刪除表中所有行,從而釋放已使用的空間。 對應的索引將被截斷 注:truncate table 不是DML語句,是DDL語句。另外truncate與delete的區別是嗎,delete不釋放空間,truncate釋放空間。 10.刪除表(drop a table) drop table table_name [cascade constraints]; 11.刪除列(drop a cloumn) alter table table_name drop column commentd cascade constraints checkppoint 1000; or alter table table_name drop columns continue; 12.表示某一列為未使用(unused)(mark a column sa unused) alter table table_name set unused column comments xascade constraints; or alter table table_name dropp unused columns checkpoint 1000; or alter table orders drop columns continue checkpoint 1000 data_dictionary : dba_unused_col_tabs; 除將列從表中刪除以外,還可以先將列標記為 “未使用”,以后再刪除。因為沒有刪除數據,所以此操作不回收磁盤空間, 因而具有速度比較快的優點。被標為 “未使用” 的列可在以后系統活動較少時從表中刪除。 未使用的列就像不屬於表一樣。查詢時看不到未使用列中的數據。此外,在執行 DESCRIBE 命令時, 也不會顯示這些列的名稱和數據類型。用戶可以添加與未使用的列同名的新列。 如果想刪除同一表中的兩列,則可先將列設置為 “未使用” 然后再刪除。在刪除兩列時, 表中的所有行都會更新兩次;但如果將這些列設置為 “未使用” 然后再刪除,則所有的行僅更新一次。
四、索引
1.創建一個基於函數的索引(creating function-based indexes) create index summit.item_quantity on summit.item(quantity-quantity_shipped); 基於函數的索引(function-based indexes):如果在表中要建立索引的一列或多列上使用了函數或表達式,則創建的是基於函數的索引。
基於函數的索引預先計算函數或表達式的值,病將結果存儲在索引中。可以將基於函數的索引創建為B樹或位圖索引。 2.創建一個B樹索引(create a B-tree index) creae [ unique ] index index_name on table_name (column,.. asc/desc) tablespace tablespace_name [ pctfree integer ] [ initrans integer ] [ maxtrans integer ] [ logging | nologging ] [ nosort ] storage(initial 200k next 200k pctincrease 0 maxextents 50); B樹:平衡二叉樹,oracle中用的最多的索引模式,使用與取值唯一性高的情況。只有兩層,非葉級、葉級(指針所在級) 3.索引中pctfree參數計算公式 pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows 4.創建一個反向鍵索引(creating reverse key indexes) create unique index xay_id on xay(a) reverse pctfree 30 storage (initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx; 5.創建位圖索引(creating reverse key indexes) create unique index say_id on xay(a) reverse pctfree 30 storage (initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx; 在下列情況中,位圖索引比 B 樹索引更有利: ? 當表包含數百萬行且鍵列的基數很低(即,該列中重復的值很多)時。例如,對於包含護照記錄的表的性別列和婚姻狀況列而言,位圖索引比 B 樹索引更適合 ? 當查詢經常使用涉及 OR 運算符的多個 WHERE 條件組合時 ? 當鍵列上存在只讀或很少的更新操作時 (位圖索引適用於取值的唯一性很低的情況) 6.改變索引的存儲參數(change storage parameter of index) alter index xay_id storage ( next 400k maxextents 100 ) 7.為索引分配空間(allocating index space) alter index xay_id allocate extent (size 200k datafile 'c:/oracle/index.dbf') 8. alter index xay_id deallocate unused; 手動分配索引空間: 在表上進行頻繁的插入操作前,可能需要向索引添加區。添加區可防止索引動態擴展並導致性能降低。 (其中指定的數據文件一定是索引所在表空間的數據文件)
五、約束(constraints)
1.將約束定義為立即(immediate)或延遲(deferred)(define constraints as immediate or deferred) alter session set constraint[s]=immediate/deferred/default; set constraint[s] constraint_name/all immediate/deferred; set constraints 語句用於將特定事務的約束設置為 deferred 或 immediate.可以使用此語句設置約束名稱列表或約束的模式。 set constraints模式將一直持續到事務處理完成或者另一個set constraints語句重置模式 set constraints語句還包含將約束設置為immediate或deferred的子句set constraints.此命令缺省為設置所有(all) 可延遲的約束(不能指定約束名稱列表)。
alter session set constraints 語句僅適用於當前會話 2.刪除表或表空間時連帶刪除其上的外檢(約束) drop table table_name cascade constraints or drop tablespace tablespace_name including contents cascade constraints 在刪除父表之前,必須先刪除外鍵。可以使用以下一條語句同事執行這兩個操作: drop table table_name cascade constraints 在未刪除或禁用外鍵之前無法(truncated)父表。 在刪除包含附表的表空間之前,必須先刪除外鍵。可使用下列命令完成該操作: drop tablespace tablespace_name including contents cascade constraints 如果從父表中刪除行時沒有使用 delete cascade 選項,oracle 服務器必須確保子表中的行不包含相應的外鍵。同樣,僅當子行中不包含舊鍵值時,才允許更新父鍵。 如果字表的外鍵上沒有索引,則oracle服務器索引子表並禁止更改以確保引用完整性。 如果表上有索引,則通過鎖定索引項並避免子表上有更具限制性的鎖來維護引用完整性。 如果必須從不同的事務處理同時更新兩個表,則在外鍵列上創建索引。 3.在創建表時定義約束(define constraints while create a table ) create table xay(id number(7) constraint xay_id pprimary key deferrable using index storage(initial 100k next 100k ) tablespace indx); primary key / unique / references table(column) / check 4.啟用當前禁用的約束(enable constraints) alter table xay enable novalidate constraint xay_id; 啟用novalidate:對於當前已有索引的 primary key 和 unique 約束,啟用novalidate 約束比啟用validate約束要快的多,這是應為,如果使用該選項啟用約束,則不要求鎖定表。
這種方法適合表上有許多DML活動的情況,如在oltp環境中。 但是,如果需要創建索引,使用這中啟用約束的方法並不能比enable validate帶來更多的好處,因為oracle服務器在建立索引時鎖定表。 5.啟用約束(enable constraints) alter table xay enable validate constraint xay_id;
六、加載(load)數據
1.使用insert語句從另一張表中“直接加載”數據到新表(loading data using direct_load insert) insert //*+append*/into emp nologging select * from emp_old; 2.使用sql*loader加載數據(using sql*loader) sqlldr scott/tiger \ control = ulcase6.ctl \ log = ulcase6.log direct = true
七、重整數據(reorganizing data)
1.使用export導出數據(using export) $exp scott/tiger tables(dept,emp) file=c:\emp.dmp log=exp.log compress=n direct=y 2.使用import導入數據(using impport) alter tablespace sales_ts read only; $expp sys/.. file=xay.dmp transpport_tablespace=y tablespace=sales_ts triggers=n constraints=n $copy datafile $imp sys/.. file=xay.dmp transpport_tablespppace=y datafiles=(/disk/sles01.dbf,/disk2/sles02.dbf) alter tablespace sales_ts read write; 4.checking transport set DNMS_tts.transport_set_check(ts_list =>'sales_ts' ..,incl_constraints=>true); 在表transport_set_violations中查看 dbms_tts.isselfcontained 為true 是,表示自包含
八、管理口令及相關資源(managing password security and resourecs)
1.修改用戶賬號、解鎖及口令(controlling account lock and password) alter user jumcky identified by oracle account unlock; 2.user_privided password function function_name(userid in varchar2(30),password in varchar2(30),old_password in varchar2(30)) return boolean 3.創建概要文件:設置口令參數(create a profile : password setting) create profile grace_5 limit failed_login_attempts 3 password_lock_time unlimited password_life_time 30 password_reuse_time 30 password_verify_function verify_function password_grace_time 5; 4.修改概要文件(altering a profile) alter profile default limit failed_lofin_attemppts 3 password_life_time 60; 5.刪除概要文件(drop a profile) drop profile grace_5 [cascade]; 6.創建概要文件:設置資源限制(create a profile : resource limit) create profile developer_prof limit sessions_per_user 2 cpu_per_session 10000 idle_time 60 connect_time 480; 7.view =>resource_cost : alter resource cost dba_users,dba_profiles 8.允許資源限制(enable resource limits) alter system set resource_limit = true;
九、管理用戶(managing users)
1.創建用戶(數據庫認真方式)(create a user : database authentication) create user juncky
identified by oracle
default tablespace users
temporary tablespace
temp quota 10m/unlimited
on data password expire [ account lock | unock ] [ profile profilename | default ]; 2.修改用戶的表空間限額(change user quota on tablespace) alter user juncky quota 0 on users; 3.刪除用戶(drop a user ) drop user juncky [ cascade ]; 4.監控用戶的視圖(monitor user) view:dba_users, dba_ts_quotas
十、管理權限(managing privileges)
1.系統權限(managing privileges) view =>system_privilege_map ,dba_sys_privs,session_privs 2.授權系統權限(grant system privilege) grant create session,create table to managers; or grant create sessionto scott with admin option; with admin option can gran grant or revoke privilege from any user or role; 3.sysdba和sysoper的權限(sysdba and sysoper privileges:) sysoper:startup,shtdown,alter database open | mount,alter database backup controlfile,alter tablespace sysdba:sysoper privileges with admin option,create database,recover database until 4.口令文件成員視圖(password file members:) view:=v$pwfile_users 5.07_dictionary_accessibility = true restriction access to view or tables in other schema 6.撤銷系統權限(revoke system privilege) revoke create table from karen; or revoke create session from scott; 7.授權對象權限(grant object privilege) grant execute on dbms_pipe to public; grant update(first_name,salary) on employee to karen with grant option; 8.顯示對象權限的視圖(display object privilege) view =>dba_tab_privs,dba_col_privs 9.撤銷用戶的對象權限(revoke object privilege) revoke execute on dbms_pipe from scott [ cascade constraints ]; 10.審計記錄視圖(audit record view) sys.aud$ 11.保護審計線索(protecting the audit trail) audit delete on sys.aud$ by access; 保護審計線索:應保護審計線索,以防添加,修改或刪除審計信息。 發布以下,命令: audit delete on sys.aud$ by access; 可防止審計線索未經授權即被刪除;只有DBA才擁有delete_catalog_role角色 12.語句審計(statement auditing) audit user; 13.權限審計(privilege auditing) audit select any table by summit by access; 權限審計:該種審計執行操作應具有的相應系統權限進行選擇性審計,如audit create any trigger. 可以設置權限審計對數據庫中的所選用戶或每個用戶進行審計。 14.方案對象審計(schema object auditing) sudit lock on summit.employee by access whenever successful; 方案對象審計:該種審計對待特定方案對象上的特定語句進行選擇性審計,如 audit select on hr.employees.方案對象審計始終適用於所有數據庫用戶。 15.審計選項視圖(view audit option) view =>all_def_audit_opts,dba_stmt_audit_opts,dba_priv_audit_opts,dba_obj_audit_opts 16.設計結果視圖(view audit result) view=>dba_audit_trail,dba_audit_exists,dba_audit_object,dba_audit_session,dba_audit_statement
十一、管理角色
1.創建角色(create roles) create role sales_clerk; or create role hr_clerk identified by bonus; or create role hr_manager identified externally; 2.修改角色(modify role) alter role sales_clerk identified by commission; or alter role hr_clerk identified externally; or alter role hr_manager not identified; 3.分配角色(assigning roles) grant sales_clerk to scott; oe grant hr_clerk to hr_manager; or grant hr_manager to scott with admin option; 4.建立缺省角色(establish default role) alter user scott default role hr_clerk,sales_clerk; or alter user scott default role all; or alter user scott default fole all except hr_clerk; or alter user scott default role none; 5.允許和禁止角色(enable and disable roles) set role hr_clerk; or set role sales_clerk identified by commission; or set role all except sales_clerk; or set role none; 6.撤銷用戶的角色(remove role from user) revoke sales_clerk from scott; or revoke hr_manager from public ; 7.刪除角色(remove role) drop role hr_manager; 8.顯示角色信息的視圖(display role information) view:=>dba_roles.dba_role_privs,role_role_privs,dba_sys_privs,role_sys_privs,role_tab_privs,session_roles
十二、備份和恢復(backup and recovery)
1.備份恢復用相關視圖 v$sga,v$instance,v$process,v$bgprocess,v$database,v$datafile,v$sgastat 2.為rman設置初始化參數 rman need set dbwr_io_slaves or backup_tape_io_slaves and large_pool_size 3.監控並行回滾的視圖(monitoring parallel rollback) v$fast_start_servers,v$fast_start_transactions 4.執行一個冷備份(perform a closed database baskup ( noarchivelog )) shutdown immediate cp files /backup/ startup 5.改變數據庫文件的位置(restore to a different location) connect system/manager as sysdba startupp mount alter database rename file '/disk1/../user.dbf' to '/disk/../user.dbf'; 6.恢復命令(recover)語法(recover syntax) --recover a mounted database recover database; recover datafile '/disk1/data/df2.dbf'; alter database recover database; --recover an opened database recover tablespace user_data; recover datafile 2; alter database recover datafile 2; 7.設置自動應用重做日志文件(how to apply redo log files automatically) set autorecovery on recover automatic datafile 4; 8.完全恢復(complete recovery) --method 1 ( mounted databe ) copy c:\backup\user.dbf c:\oradata\user.dbf startup mount recover datafile 'c:\oradata\user.dbf'; alter database oppen; --mothod 2 ( opened database,initially opened, not system or rollback datafile) copy c:\backup\user.dbf c:\oradata\user.dbf ( alter tablespace offline) recover datafile 'c:\oradata\user.dbf' or recover tablespace user_data; alter database datafile 'c:\oradata\user.dbf' online or alter tablespace user_data online; --method 3 ( opened database,initially closed not system or rollback datafile ) startup mount alter database datafile 'c:\oradata\user.dbf' offline; alter database open copy c:\backup\user.dbf d:\oradata\user.dbf alter database rename file 'c:\oradata\user.dbf' to 'd:\oradata\user.dbf' recover datafile 'e:\oradata\user.dbf' or recover tablespace user_data; alter tablespace user_data online; --method 4 ( loss of data file with no backup and have all archive log ) alter tablespace user_data offline immediate; alter database create datafile 'd:\oradata\user.dbf' as 'c:\oradata\user.dbf' recover tablespace user_data; alter tablespace user_data online 9.對一個打開的數據庫進行備份(perform an open database backup) alter tablespace user_data begin backup; copy files /backup/ alter database datafile '/c:/../data.dbf' end backup; alter system switch logfile; 10.備份一個控制文件(backup a control file) alter database backup controlfile to 'controll.bkp'; alter database backup xontrolfile to trace; 11.非歸檔模式下的恢復(recovery (noarchivelog mode)) shutdown abort cp files startup 12.備份模式下的文件恢復(recovery of file in backup mode) alter database datafile 2 end backup; 13.清空重做日志文件(rclearing redo log file) alter database clear unarchived logfile group 1; alter database clear unarchived logfile group 1 unrecoverable datafile; 14.重做日志的恢復(刪除與重建)(redo log recovery) alter database add logfile group 3 'c:\oradata\redo03.log' size 1000k; alter database drop logfile group 1; alter database open; or >cpp c:\oradata\redo02.log ,c:\oradata\redo01.log alter database clear logfile ‘c:\oradata\log01.log’;