1. 程式人生 > >Oracle表空間概念詳解

Oracle表空間概念詳解

表空間概念詳解

1.使用系統使用者登入SQL Plus:

sys,system;sysman;scott使用者登入格式:[username/password] [@server] [as sysdba|sysoper]

                        system/orcl @orcl as sysdba 

                        備註:orcl 就是自己設定的服務名system/orcl

如果已經使用某個使用者登入了SQL Plus,切換登入使用者:

                        connect sys/orcl as sysdba 

                        備註:書寫不區分大小寫

2. 檢視登入使用者

show user 

備註:sqlplus 中輸入的命令不需要分號,當輸入的是sql語句時需要加分號

通過”資料字典”—dba_users(資料庫提供的表,也是由很多的欄位組成)檢視使用者的其他欄位資訊

檢視資料字典中的欄位:

desc dba_users

通過資料字典檢視有多少個使用者:

select username from dba_users;

3. 啟用(scott)使用者的語句:

使用scott使用者登入sqlplus:(scott使用者的預設密碼是tiger)
connect scott/tiger

4. 表空間概述:

表空間:

        資料庫的邏輯儲存空間,可以理解為在資料庫中開闢的空間用來儲存資料庫物件

;

表空間和資料檔案的關係

        表空間由一個或多個數據檔案組成;資料檔案的大小和位置可以自己定義;

表空間的分類:    永久表空間:資料庫中要永久化儲存的一些物件,如:表、檢視、儲存過程    臨時表空間:資料庫操作當中中間執行的過程,執行結束後,存放的內容會被自動釋放    UNDO表空間:用於儲存事務所修改資料的舊值,可以進行資料的回滾

5. 檢視使用者的表空間:

①資料字典

dba_tablespaces(系統管理員級別檢視的資料字典)

user_tablespaces(普通使用者檢視的資料字典)

②.查看錶空間的欄位
desc dba_tablespaces
③.檢視有幾個表空間
select tablespace_name from dba_tablespaces; 
⑤.檢視使用者的欄位資訊
desc dba_users
⑥.檢視使用者的預設表空間、臨時表空間等等
select default_tablespace from dba_users where username=’SYS’;

6. 設定使用者的預設或臨時表空間

alter user username default|tempporart tablespace tablespace_name;
 備註:普通使用者沒有設定表空間的許可權

7. 建立、修改、刪除表空間

①.建立表空間
create [temporary] tablespace tablespace_name tempfile|datafile ‘xx.dbf’ size xx;
備註:如果建立的是臨時表空間,需要加上temporary關鍵字;②.查看錶空間的具體路徑:(通過dba_data_files 和 dba_temp_files這個資料字典)
desc dba_data_files
select file_name from dba_data_files where tablespace_name=”;(條件是表空間的名字,需要大寫)
.修改表空間的狀態設定聯機或離線的狀態(表空間是離線時不可用,預設是聯機的)
alter tablespace tablespace_name online|offline;
如何知道表空間所處的狀態?(通過這個dba_tablespaces資料字典)
desc dba_tablespaces
select status from dba_tablespaces where tablespace_name=”;(條件是表空間的名字,需要大寫)
設定只讀或可讀寫的狀態(只有在聯機狀態才可以更改,預設的聯機狀態就是讀寫狀態)
alter tablespace tablespace_name read only | read write;
.修改資料檔案增加資料檔案
alter tablespace tablespace_name add datafile ‘xx.dbf’ size xx;
select file_name from dba_data_files where tablespace_name=”;(條件是表空間的名字,需要大寫)
備註:通過這條select語句就查詢到當前表空間中的資料檔案刪除資料檔案(不能刪除表空間當中第一個資料檔案,如果要刪除就需要刪除整個表空間)
alter tablespace tablespace_name drop datafile ‘xx.dbf’;
.刪除表空間
drop tablespace tablespace_name[including contents];
備註:如果只是刪除表空間不刪除該表空間下的資料檔案,則不加including contents;