Hi-Fi優質I.T網:技術文章...
您尚未登入...
帳號:
密碼:

忘記密碼 
 
  訂閱電子報

取消   訂閱
 
2004/9/4-23:44:21
 Oracle 常用指令│作者:站長│分數:-132

 

一、日誌管理

1.切換Redo Log File 群組

SQL> alter system switch logfile;

2.執行檢查點

SQL> alter system checkpoint;

3.新增Redo Log 群組

SQL> alter database add logfile [group 2]  ('/redo1/log2a.rdo','/redo2/log2b.rdo') size 1m;

4.新增Redo Log File

SQL> alter database add logfile member  '/redo3/log1b.rdo' to group 1,  '/redo4/log2b.rdo' to group 2;

5.變更Redo Log File 檔名

SQL> alter database rename file '/oracle/db_name/redo.log' to '/oracle/db_name/redo01.log';

6.刪除Redo Log 群組

SQL> alter database drop logfile group 1;

7.刪除Redo Log 成員 

SQL> alter database drop logfile member '/oracle/db_name/redo01.log';

8.清空Redo Log File 內容 

SQL> alter database clear [unarchived] logfile '/oracle/log2.rdo';

9.用Logminer 分析 Redo Log File

(1). 在 pfile.ora 指定 utl_file_dir = ' '

(2). SQL> execute dbms_logmnr_d.build('oradb.ora','/oracle/oradb/log');

(3). SQL> execute dbms_logmnr_add_logfile ('/oracle/oradata/oradb/redo01.log', dbms_logmnr.new);

(4). SQL> execute dbms_logmnr.add_logfile '/oracle/oradata/redo01.log', dbms_logmnr.addfile);
(5). SQL> execute dbms_logmnr.start_logmnr dictfilename=>'/oracle/log/logmin.ora');

(6). SQL> select * from v$logmnr_contents (v$logmnr_dictionary, v$logmnr_parameters, v$logmnr_logs);

(7). SQL> execute dbms_logmnr.end_logmnr;

二、表空間管理


1.新增Tablespace

SQL> create tablespace tablespace_name datafile '/oracle/db_name/tbx01.dbf' size 100m, '/oracle/db_name/tbx02.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.建立本地管理的Tablespace

SQL> create tablespace user_data datafile '/oracle/db_name/tbx01.dbf' size 500m extent management local uniform size 10m;

3.建立暫存Tablespace

SQL> create temporary tablespace temp tempfile '/oracle/db_name/temp01.dbf' size 500m extent management local uniform size 10m;

4.變更Tablespace Storage的設定

SQL> alter tablespace app_data minimum extent 2m;
SQL> alter tablespace app_data default storage(initial 2m next 2m maxextents 999);

5.變更Tablespace Online 或 Offline

SQL> alter tablespace app_data offline;
SQL> alter tablespace app_data online;

6.變更Tablespace 存取狀態

SQL> alter tablespace users read only|write;

7.刪除Tablespace

SQL> drop tablespace users including contents;

8.自動延伸Tablespace DataFile 容量 

SQL> alter tablespace app_data add datafile '/oracle/db_name/tbx01.dbf' size 200m autoextend on next 10m maxsize 500m;

9.手動變更DataFile 容量

SQL> alter database datafile '/oracle/users01.dbf' resize 200m;

10.變更Tablespace DataFile 存放位址

SQL> alter tablespace users rename datafile '/oracle/db_name/users01.dbf' to '/oracle/users01.dbf';

11.變更Tablespace DataFile 檔名

SQL> alter database rename file '/oracle/db_name/user.dbf' to '/oracle/db_name/user01.dbf';

三、表格

1.建立表格

SQL> create table table_name (column datatype,column datatype]....)
tablespace tbx_name [pctfree integer] [pctused integer]
 [initrans integer] [maxtrans integer] storage(initial 200k next 200k pctincrease 0 maxextents 50) [logging|nologging] [cache|nocache];

2.複製一個已存在的表格

SQL> create table table_new [logging|nologging] as table_old

3.建立暫時性表格

SQL> create global temporary table table_temp as select * from table_name;

4.pctfree = (average row size - initial row size) *100 /average row size

pctused = 100-pctfree- (average row size*100/available data space)

5.變更表格 Storage 與 Block 使用參數

SQL> alter table table_name pctfree=30 pctused=50 storage(next 500k minextents 2 maxextents 100);

6.手動延伸表格區塊

SQL> alter table table_name allocate extent(size 500k datafile '/oracle/db_name/tbx01.dbf');

7.搬移Table 至另一Tablespace 

SQL> alter table employee move tablespace users;

8.減少未使用的表格高水平位

SQL> alter table table_name deallocate unused [keep integer]

9.Truncate Table

SQL> truncate table table_name;

10.刪除表格

SQL> drop table table_name [cascade constraints];

11.刪除欄位

SQL> alter table table_name drop column comments cascade constraints checkpoint 1000;
SQL> alter table table_name drop columns continue;

12.標示欄位成不使用

SQL> alter table table_name set unused column comments cascade constraints;
SQL> alter table table_name drop unused columns checkpoint 1000;
SQL> alter table orders drop columns continue checkpoint 1000
data_dictionary : dba_unused_col_tabs

四、索引

1.建立 Function-Based Indexes

SQL> create index summit.item_quantity on summit.item(quantity-quantity_shipped);

2.建立 B-Tree Index

SQL> create [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);

3.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows

4.建立反向 Index

SQL> create unique index xyz_id on xyz(a) reverse pctfree 30 storage(initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx;

5.建立 Bitmap Index

SQL> create bitmap index xyz_id on xyz(a) pctfree 30 storage( initial 200k next 200k pctincrease 0 maxextents 50) tablespace indx;

6.變更Storage Index參數
 
SQL> alter index xyz_id storage (next 400k maxextents 100);

7.改變索引資料檔空間
 
SQL> alter index xyz_id allocate extent(size 200k datafile '/oracle/db_name/index01.dbf');
SQL> alter index xay_id deallocate unused;

五、约束條件

1.定義約束條件

SQL> alter session set constraint[s] = immediate/deferred/default;
(set constraint[s] constraint_name/all immediate/deferred;)
SQL> drop table table_name cascade constraints
SQL> drop tablespace tablespace_name including contents cascade constraints;

2. 建立表格時設定約束條件
 
SQL> create table xyz(id number(5) constraint xyz_id primary key deferrable using index storage(initial 100k next 100k) tablespace indx);
primary key/unique/references table(column)/check;

3.啟用約束條件

SQL> alter table xyz enable novalidate constraint zmi_id;
SQL> alter table xyz enable validate constraint zmi_id;
SQL> alter table xyz enable constraint zmi_id;

六、SQL DML

1.Select 、Insert 表格

SQL> insert /*+append */ into emp nologging
SQL> select * from emp_old;

2.平行處理 Select 、Insert 表格

SQL> alter session enable parallel dml;
SQL> insert /*+parallel(emp,2) */ into emp nologging
SQL> select * from emp_old;

3.使用 SQL*Loader 

$ sqlldr scott/tiger
$ control = temp.ctl
$ log = temp.log direct=true

七、邏輯備份與還原 Export & Import 

1.Export

$exp scott/tiger tables(dept,emp) file=/export/emp.dmp log=exp.log compress=n direct=y

2.Import

$imp scott/tiger tables(dept,emp) file=emp.dmp log=imp.log ignore=y

3.transporting a tablespace

sql>alter tablespace sales_ts read only;
$exp sys/.. file=xay.dmp transport_tablespace=y tablespace=sales_ts
triggers=n constraints=n
$copy datafile
$imp sys/.. file=xay.dmp transport_tablespace=y datafiles=(/disk1/sles01.dbf,/disk2
/sles02.dbf)
sql> alter tablespace sales_ts read write;

4.checking transport set

sql> DBMS_tts.transport_set_check(ts_list =>'sales_ts' ..,incl_constraints=>true);
在表transport_set_violations 中查看
sql> dbms_tts.isselfcontained 为true 是, 表示自包含

八、 密碼、資源、安全管理

1.controlling account lock and password

sql> alter user juncky identified by oracle account unlock;

2.user_provided password function

sql> function_name(userid in varchar2(30),password in varchar2(30),
old_password in varchar2(30)) return boolean

3.create a profile : password setting

sql> create profile grace_5 limit failed_login_attempts 3
sql> password_lock_time unlimited password_life_time 30
sql>password_reuse_time 30 password_verify_function verify_function
sql> password_grace_time 5;

4.altering a profile

sql> alter profile default failed_login_attempts 3
sql> password_life_time 60 password_grace_time 10;

5.drop a profile

sql> drop profile grace_5 [cascade];

6.create a profile : resource limit

sql> create profile developer_prof limit sessions_per_user 2
sql> 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

sql> alter system set resource_limit=true;

九、管理 Schema

1.create a user: database authentication

sql> create user juncky identified by oracle default tablespace users
sql> temporary tablespace temp quota 10m on data password expire
sql> [account lock|unlock] [profile profilename|default];

2.change user quota on tablespace

sql> alter user juncky quota 0 on users;

3.drop a user

sql> drop user juncky [cascade];

4. monitor user

view: dba_users , dba_ts_quotas

十、權限管理

1.system privileges: view => system_privilege_map ,dba_sys_privs,session_privs

2.grant system privilege
sql> grant create session,create table to managers;
sql> grant create session to scott with admin option;
with admin option can grant or revoke privilege from any user or role;

3.sysdba and sysoper privileges:
sysoper: startup,shutdown,alter database open|mount,alter database backup controlfile,
alter tablespace begin/end backup,recover database
alter database archivelog,restricted session
sysdba: sysoper privileges with admin option,create database,recover database until

4.password file members: view:=> v$pwfile_users

5.O7_dictionary_accessibility =true restriction access to view or tables in other schema

6.revoke system privilege

SQL> revoke create table from zmi;
SQL> revoke create session from scott;

7.grant object privilege

sql> grant execute on dbms_pipe to public;
sql> 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

sql> revoke execute on dbms_pipe from scott [cascade constraints];

10.audit record view :=> sys.aud$

11. protecting the audit trail

sql> audit delete on sys.aud$ by access;

12.statement auditing

sql> audit user;

13.privilege auditing

sql> audit select any table by summit by access;

14.schema object auditing

sql> audit lock on summit.employee by access whenever successful;

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.建立角色

SQL> create role zmi;
SQL> create role zmi identified by password;
SQL> create role zmi identified externally;

2.修改角色

SQL> alter role zmi identified by password;
SQL> alter role zmi identified externally;
SQL> alter role zmi not identified;

3.賦予角色

SQL> grant zmi to scott;
SQL> grant zmi to hr;
SQL> grant zmi to scott with admin option;

4.設定預設角色

SQL> alter user scott default role hr_manager,zmi;
SQL> alter user scott default role all;
SQL> alter user scott default role all except zmi;
SQL> alter user scott default role none;

5.開啟與關閉角色

SQL> set role zmi;
SQL> set role zmi identified by password;
SQL> set role all except zmi;
SQL> set role none;

6.從使用者移除角色

SQL> revoke zmi from scott;
SQL> revoke zmi from public;

7.刪除角色

SQL> drop role zmi;

8.顯示角色資訊

dba_roles,dba_role_privs,role_role_privs,dba_sys_privs,role_sys_privs,role_tab_privs,session_roles

十二、 備份與復原

1.可用到的View

v$sga,v$instance,v$process,v$bgprocess,v$database,v$datafile,v$sgastat

2.可監控平行處理的Rollback View 

v$fast_start_servers , v$fast_start_transactions

3.perform a closed database backup (noarchivelog)

SQL> shutdown immediate 
$ cp files /backup/
SQL> startup

4.Restore DataFile 到不同的位址 

$ connect system/manager as sysdba
SQL> startup mount
SQL> alter database rename file '/ora1/user.dbf' to '/dora2/user.dbf';
SQL> alter database open;

5.Recover 語法

--Recover 掛載的資料庫--
SQL> recover database;
SQL> recover datafile '/oracle/db_name/user02.dbf';
SQL> alter database recover database;

--Recover 開啟的資料庫 
SQL> recover tablespace users;
SQL> recover datafile 2;
SQL> alter database recover datafile 2;

6.自動Recover Redo Log Files

SQL> set autorecovery on
SQL> recover automatic datafile 4;

7.完整復原

--(資料庫掛載模式) --
$ cp /backup/user.dbf /oracle/db_name/user01.dbf
SQL> startup mount
SQL> recover datafile 'c:\oradata\user01.dbf;
SQL> alter database open;

--(資料庫開啟模式)--

$ cp /backup/user.dbf /oradata/db_name/user01.dbf (必須是先將Tablespace offline)
SQL> recover datafile '/oradata/db_name/user01.dbf' or
SQL> recover tablespace user_data;
SQL> alter database datafile '/oradata/db_name/user01.dbf' online or alter tablespace user_data online;
--(資料庫開啟模式)--

SQL> startup mount
SQL> alter database datafile '/db_name/user01.dbf' offline;
SQL> alter database open
$cp /backup/user01.dbf /db_name/user01.dbf
SQL> alter database rename file '/db_name/user01.dbf' to '/db_name/user01.dbf'
SQL> recover datafile '/db_name\user01.dbf' or recover tablespace users;
SQL> alter tablespace user_data online; 

--(資料庫開啟模式)-- 

SQL> alter tablespace user_data offline immediate;
SQL> alter database create datafile 'd:\oradata\user.dbf' as '/db_name/user01.dbf''
SQL> recover tablespace user_data;
SQL> alter tablespace user_data online

8.備份Online 資料庫Tablespace

SQL> alter tablespace user_data begin backup; 
$ cp files /backup/
SQL> alter database datafile '/backup/data.dbf' end backup;
SQL> alter system switch logfile;

9.備份控制檔

SQL> alter database backup controlfile to 'control1.bck';
SQL> alter database backup controlfile to trace;

10.Recovery (NoArchivelog Mode)

SQL> shutdown abort 
$ cp files
SQL> startup


11.Recovery Data File Backup Mode

SQL> alter database datafile 2 end backup;

12.清除 Redo Log File

SQL> alter database clear unarchived logfile group 1;
SQL> alter database clear unarchived logfile group 1 unrecoverable datafile;

13.Redo Log Recovery

SQL> alter database add logfile group 3 'c:\oradata\redo03.log' size 1000k;
SQL> alter database drop logfile group 1;
SQL> alter database open;
or
$cp c:\oradata\redo02.log' c:\oradata\redo01.log
SQL> alter database clear logfile 'c:\oradata\log01.log';


請評分: -3 -2 -1 +1 +2 +3
  
   

Hi-Fi優質I.T網”站內所有文章、圖片除特別加註外均屬本站所有,如有任何問題請來信告知
請支持言論自由,討論區所發表之言論不代表本站立場
Hi-Fi優質I.T網”版權所有2004©
---最佳顯示效果1024*768---