天天看點

Oracle 表空間和使用者的維護(建立、删除、修改)(二)

        最近為了完成資料在不同庫之間的導入導出,臨時建立了不少表空間和使用者,為了保持資料庫的幹淨整潔,根據需要删除無用的使用者、表空間及實體檔案。

        一個資料庫被分成一個個的邏輯單元,這些邏輯單元叫做“表空間”,表空間用于存放資料庫對象(如表、索引等)。表空間由一個或者多個資料檔案(實體檔案)組成,資料檔案是資料的實體載體。表空間的尺寸是組成這個表空間的所有資料檔案的尺寸之和。每個資料檔案是一個作業系統檔案,由多個作業系統檔案塊組成。使用者查詢一個表,如果表的資料不在記憶體中,Oralce會讀取該表所在的資料檔案,并把資料放到記憶體中。

        每個資料庫都有一個系統表空間(system tablespace)和一個輔助表空間(sysaux tablespace),建立資料庫的時候,系統會自動建立這兩個表空間。

下面是相關的文法操作。

1.檢視表空間

select * from dba_tablespaces
           

2.查找實體檔案

select * from dba_data_files
           

3.檢視表空間裡存放了哪些使用者的資料

select distinct owner from dba_segments where tablespace_name='MY_001'
           

4.檢視使用者

select * from dba_users
           

5.檢視使用者預設的表空間

select default_tablespace from dba_users where username='LG'
           

6.查找使用者下面所有的表

select * from dba_tables  where owner='LGL'
           

7.删除使用者,及級聯關系也删除

drop user user_name cascade
           

8.删除表空間,對應的表空間檔案也删除

drop tablespace tablespace_name including contents and datafiles cascade constraint
           

9.建立表空間

create tablespace ****
datafile 'd:\app\oracle12c\oradata\orcl\my_0003.dbf'
size 1024m
autoextend on next 200m
maxsize unlimited
           

10.修改表空間----增加實體檔案

alter tablespace *** add datafile
    'd:\app\oracle12c\oradata\orcl\my_0005.dbf' size 128m
    autoextend on next 5m
    maxsize unlimited
           

11.建立使用者及設定預設表空間

---建立使用者
CREATE USER GZZL_LS IDENTIFIED BY password
DEFAULT TABLESPACE "GZZL"
TEMPORARY TABLESPACE "TEMP"
QUOTA UNLIMITED ON "GZZL";   ---設定使用者無限制使用這個表空間配額
           

12.檢視某個表的占用實體空間大小

select segment_name AS TABLENAME,bytes b,bytes / 1024 kb,bytes /1024 /1024 mb,bytes /1024 /1024 /1024 GB from user_segments 
where segment_name = upper('D_DZ')
           

13.jdbc連接配接資料庫的時候,需要使用資料庫的sid_name,而不是資料庫的services_name

而使用plsql連接配接資料庫的時候,隻需要資料庫的services_name即可

        檢視資料庫的sid_name語句

select INSTANCE_NAME from v$instance;