Oracle常用命令

來源:互聯網
上載者:User

標籤:


1.串連Óracle:

sqlplus username/[email protected] [as sysdba]

SQL>startup force [nomount/mount/open/force/restrict]

SQL>shutdown [normal/transactional/immediate/abort]

2.啟動控制台:

mysqld-nt –install(壓縮的安裝成服務)

mysqld-nt –remove(卸載服務)

net start(stop) mysql

DE>mysql -v或者mysql -u root -pDE>

set oracle_sid=**
emctl start dbconsole
http://hnjk-oh:1158/em
isqlpuls start
http://hnjk-oh:5560/isqlplus
lsnrctl start(stop) (啟動tns)
dbca(啟動組態管理)
dbua(升級管理)
netca(網路設定管理)

3.建立資料表空間和使用者:

SQL>create tablespace wy863college datafile ‘ F:\oracle\product\10.2.0\oradata\hnjkorg\wy863college.dbf‘ size 500M AUTOEXTEND ON NEXT 5M MAXSIZE UNLIMITED;

SQL>drop tablespace tablespacename including contents and datafiles;

SQL>create user wy863college identified by college default tablespace wy863college;

SQL>grant dba to wy863college;

4.更改字元集:

登入到oracle:

sqlplus sys/**** as sysdba ****是sys的密碼

查看字元集:

SQL>select userenv(‘language‘) from dual;

停掉服務:

SQL>shutdown immediate

以限制模式開啟服務:

SQL>startup restrict

更改字元集:

SQL>alter database character set internal_use ZHS16GBK

重啟oracle:

SQL>shutdown immediate

SQL>startup

5、ORACLE歸檔

查看oracle資料庫是否為歸檔模式
SQL> select name,log_mode from V$database;

NAME LOG_MODE
------------------ ------------------------
TEST NOARCHIVELOG

SQL> archive log list
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 80157
Current log sequence 80163

編輯本段設定資料庫的歸檔模式
1.改變非歸檔模式到歸檔模式:   
1)SQL> conn / as sysdba (以DBA身份串連資料庫)   
2)SQL> shutdown immediate;(立即關閉資料庫)   
3)SQL> startup mount (啟動執行個體並載入資料庫,但不開啟)   
4)SQL> alter database archivelog; (更改資料庫為歸檔模式)   
5)SQL> alter database open; (開啟資料庫)   
6)SQL> alter system archive log start; (啟用自動歸檔)   
7)SQL> exit (退出)   

做一次完全備份,因為非歸檔記錄模式下產生的備份日誌對于歸檔模式已經不可用了.這一步非非常重要!
2.改變歸檔模式到非歸檔模式:   
1)SQL>SHUTDOWN NORMAL/IMMEDIATE;   
2)SQL>STARTUP MOUNT;   
3)SQL>ALTER DATABASE NOARCHIVELOG;   
4)SQL>ALTER DATABASE OPEN;
3.歸檔相關命令
archive log stop;
archive log start;
archive log list;

show parameters;
show parameters log_archive_start;
show parameters log_archive_max_process; #歸檔進程數
alter system set log_archive_max_process=5; #將歸檔進程數改為5
select * from v$bgprocess; #檢察後台進程

6. 修改進程數:

sqlplus /nolog

SQL>conn / as sysdba;

SQL>show parameter pfile;

SQL>show parameter process;

SQL>alter system set processes=500 scope=spfile;

SQL>startup force;

7. 創目錄:

sqlplus username/[email protected] [as sysdba]

I.建立:

SQL>create or replace directory DUMP_DIR as ‘E:\test\‘;(基於Windows系統)

SQL>create or replace directory DUMP_DIR as ‘/home/oracle/bfiletest‘;(基於Linux系統)

SQL>commit;

II.查看:

SQL>select * from dba_directories;

III.刪除:

SQL>drop directory DUMP_DIR;

8.導資料:

DOC模式下:

I.expdp \"sys/hnjk as sysdba\" schemas=(developer,jforum,courseforum,
virtualcourtroom) directory=DUMP_DIR dumpfile="db.dmp"

exp user/password file=exp.dmp compress=Y grants=Y indexes=Y tables=(table1,table2);

(exp才可以用compress=Y)

II.impdp \"sys/hnjk as sysdba\" directory=DUMP_DIR dumpfile="$dump_file"

III.impdp system/<password> SCHEMAS=sh REMAP_SCHEMA=sh:sh2 \

DUMPFILE=datadir1:schema1%U.dmp,datadir2:schema2%U.dmp \

EXCLUDE=constraint, ref_constraint, index,materialized_view \

TABLE_EXISTS_ACTION=replace logfile=datadir1:impschema.log

9. 查看FLASH_RECOVERY_AREA空間使用方式:

1)查看資料庫REDOLOG情況:

SQL> select * from v$log;

2)手工切換日誌

SQL> alter system switch logfile;

3)查看FLASH_RECOVERY_AREA空間使用方式:

[[email protected] oracle]# du -a -k flash_recovery_area
……………

2353148 flash_recovery_area說明:FLASH_RECOVERY_AREA空間使用了2.35GB

4)查看FLASH_RECOVERY_AREA空間中各部分使用方式:

SQL> select * from v$recovery_file_dest;

SQL> select * from v$flash_recovery_area_usage;

10、Oracle刪除使用者及資料表空間

Oracle 使用時間長了, 新增了許多user 和tablespace. 需要清理一下

對於單個user和tablespace 來說, 可以使用如下命令來完成。

步驟一: 刪除user

drop user ×× cascade

說明: 刪除了user,只是刪除了該user下的schema objects,是不會刪除相應的tablespace的。

步驟二: 刪除tablespace

DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;

但是,因為是供開發環境來使用的db, 需要清理的user 和 table space 很多。

思路:

Export出DB中所有的user和tablespace, 篩選出系統的和有用的tablespace,把有用的資訊load到一張表中去。

然後寫常式迴圈,把不在有用表的tablespace刪掉

1). select username,default_tablespace from dba_users;

2).

create table MTUSEFULSPACE
(
ID Number(4) NOT NULL PRIMARY KEY,
USERNAME varchar2(30),
TABLESPACENAME varchar2(60),
OWNERNAME varchar2(30)
);

3).

declare icount number(2);
tempspace varchar2(60);
begin
for curTable in (select username as allusr,default_tablespace as alltblspace from dba_users)
loop
tempspace :=curTable.alltblspace;
dbms_output.put_line(tempspace);
select count(TABLESPACENAME) into icount from MTUSEFULSPACE where TABLESPACENAME = tempspace;
if icount=0 then
DROP TABLESPACE tempspace INCLUDING CONTENTS AND DATAFILES;
end if;
commit;
end loop;
end;

執行後會報如下錯誤

ORA-06550: 第 10 行, 第 5 列:
PLS-00103: 出現符號 "DROP"在需要下列之一時:
begin case declare exit
for goto if loop mod null pragma raise return select update
while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

好像是被鎖了。。

沒辦法,常式不能寫,就只能組出語句執行了。

把需要刪除的user, tablespace 匯出到Excel. 使用CONCATENATE 組出SQL.

貼到SQLdevelop 批量執行。

整個刪除會比較耗時間, 100多個user. 用了12個小時左右。

如要找datafile的具體位置,可以使用

select t1.name,t2.name from v$tablespace t1, v$datafile t2 where t1.ts# = t2.ts#;

SQL code
--刪除空的資料表空間,但是不包含物理檔案
drop tablespace tablespace_name;
--刪除非空資料表空間,但是不包含物理檔案
drop tablespace tablespace_name including contents;
--刪除空資料表空間,包含物理檔案
drop tablespace tablespace_name including datafiles;
--刪除非空資料表空間,包含物理檔案
drop tablespace tablespace_name including contents and datafiles;
--如果其他資料表空間中的表有外鍵等約束關聯到了本資料表空間中的表的欄位,就要加上CASCADE CONSTRAINTS
drop tablespace tablespace_name including contents and datafiles CASCADE CONSTRAINTS;

Oracle常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.