環境
Oracle 11.2.0.4
db_block_size = 8192
字符集:AMERICAN_AMERICA.ZHS16GBK
表空間的管理
下圖為Oracle資料庫存盤結構

我們的資料是存盤在資料檔案中,資料檔案屬于表空間的,所以添加資料首先要有表空間存在,
1、查看資料庫的表空間資訊
select * from dba_tablespaces;

創建為資料庫后,默認會有5個表空間,
SYSTEM:系統表空間
SYSAUX:系統輔助表空間
UNDOTBS1:UNDO表空間
TEMP:臨時表空間
USERS:默認表空間
2、查看資料檔案及臨時資料檔案資訊
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_data_files
union all
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_temp_files;

可以看出每個表空間有一個資料檔案,
我們在專案中一般會創建專門的表空間用來存放相應的資料,避免資料全存放在系統默認的表空間中,導致性能問題,
3、創建表空間
create tablespace ys datafile '/oradata/ys01.dbf' size 1G autoextend on next 100M;
--檢查
select * from dba_tablespaces;
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_data_files
union all
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_temp_files;


這里我創建的為小檔案表空間,且大小為1G,自動擴充,每次增長100M,直到32為止,還有一種大檔案表空間,
小檔案表空間:一個小檔案表空間可以有1024個資料檔案,
大檔案表空間:一個大檔案表空間只能有一個資料檔案,
因為此環境中db_block_size為8K,所以小檔案表空間的資料檔案一個最大為32G,大檔案表空間的資料檔案最大為32T,
后面隨著資料的增長,此表空間的資料檔案也會隨之被資料填滿,如果再想添加資料則需要為該表空間添加資料檔案了,
4、添加資料檔案
alter tablespace ys add datafile '/oradata/ys02.dbf' size 1G autoextend on next 100M;
--檢查
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_data_files
union all
select tablespace_name, file_name, file_id, bytes, maxbytes, autoextensible from dba_temp_files;

此時YS表空間就有了兩個資料檔案ys01.dbf和ys02.dbf了,
在生產環境中,如果資料量較大,建議把表資料與索引資料分別存放不不同的表空間中,
創建一個索引表空間,
create tablespace ys_index datafile '/oradata/ys_index01.dbf' size 1G autoextend on next 100M;
--檢查
select * from dba_tablespaces;

表空間準備好后,就可以創建表了, 但表是屬于某個用戶的,所以還需要提前創建專門的用戶,
用戶管理
1、用戶的創建
create user ys identified by 123456 default tablespace ys;
創建一個名為ys的用戶,密碼為123456,默認存放的表空間為ys,
如果此時使用該用戶去創建表,會報錯,因為沒有權限,
2、用戶的授權
首先需要賦予連接會話的權限,再賦予表創建權限等,
grant connect, resource to ys;
--檢查
select * from dba_role_privs where grantee = 'YS';

賦予了connect與resource角色的權限給ys用戶,生產環境也推薦賦予此權限,如果不夠再額外賦予,千萬不要為了方便就賦予dba權限,
然后我們就可以使用該用戶來創建表了,
表的管理
1、表的創建
create table ys.t1(
id number,
name varchar2(10),
sex char(1));
--檢查
select * from user_tables;
select * from ys.t1;

在ys用戶下創建了一張t1表,欄位為id、name和sex,表資料存盤在ys表空間中,
2、資料添加
insert into ys.t1 values(1,'a',1);
insert into ys.t1 values(2,'b',1);
insert into ys.t1 values(3,'c',2);
commit;
--檢查
select * from ys.t1;

往ys.t1表中添加了三行資料,
日后隨著資料的增加,查詢會變滿,我們可以在檢索的欄位上加上索引來提高查詢速度,
索引管理
1、索引的創建
create index ind_ys_t1_id on ys.t1(id);
create bitmap index ind_ys_t1_sex on ys.t1(sex);
--檢查
select * from user_indexes;
select * from user_ind_columns;


此處創建了兩個索引,一個為B樹索引:IND_YS_T1_ID,另一個為位圖索引:IND_YS_T1_SEX,都存放在ys_index表空間中,與表資料分開,
索引并非創建的越多越好,在對表資料操作時,Oracle會自動維護索引資料,也是需要成本的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/326848.html
標籤:其他
上一篇:最新系統漏洞--Fortinet Fortiweb作業系統命令注入漏洞
下一篇:如何計算矩陣和矢量的距離?
