Oracle12c用戶和表空間創建

簡介

本文主要介紹如何創建用戶和表空間,使用的操作用戶爲system,可以使用具備操作權限的任何用戶進行操作。

創建表空間

臨時表空間

  1. 確定數據庫表空間文件應該放什麼地方
# 查詢數據庫中的表空間文件位置
select * from dba_data_files 

在這裏插入圖片描述
根據查詢結果確定可以將文件放在/home/oracle/app/oracle/oradata/orcl/目錄下,由於此處是測試環境,用這種方式確定沒啥問題。生產環境需要確認目錄是否有足夠空間,權限等問題。

  1. 創建臨時表空間tbs_expert_temp
-- 創建臨時表空間 
create temporary tablespace tbs_expert_temp --表空間名稱
tempfile '/home/oracle/app/oracle/oradata/orcl/tbs_expert_temp.dbf'--表空間文件文件路徑
size 1024m --初始大小
autoextend on --自動擴展
-- 每次擴展128m,無上限(實際上oralce單個表空間文件有上限,當數據塊爲8K時,上限爲32G)
next 128m maxsize UNLIMITED
extent management local; 

數據和索引表空間

--創建數據表空間,用於存放表數據 
create tablespace tbs_expert_data
datafile '/home/oracle/app/oracle/oradata/orcl/tbs_expert_data.dbf'
size 1024m
autoextend on 
next 128m maxsize UNLIMITED
extent management local;

--創建索引表空間,爲索引建立單獨表空間 
create tablespace tbs_expert_index
datafile '/home/oracle/app/oracle/oradata/orcl/tbs_expert_index.dbf'
size 1024m
autoextend on 
next 128m maxsize UNLIMITED
extent management local;

查看錶空間

-- 查看錶空間
select * from dba_tablespaces where tablespace_name like 'TBS%'; 
-- 查看數據文件
select * from dba_data_files where tablespace_name like 'TBS%';

在這裏插入圖片描述
在這裏插入圖片描述

創建用戶

創建用戶expert,此處我使用的是12c的非Container db。如果你使用的是Container db,那麼用戶名必須以c##開頭,例如c##expert

# 查詢是不是Container db,如果返回YES,則是Container db,返回NO,則不是
select CDB from v$database;

-- 返回NO時,創建用戶並指定表空間 
create user expert identified by expert -- 賬號、密碼都爲expert
default tablespace tbs_expert_data--默認用戶表空間
temporary tablespace tbs_expert_temp; --默認臨時表空間

-- 返回YES時,創建用戶並指定表空間 
create user c##expert identified by expert -- 賬號c##expert,密碼expert
default tablespace tbs_expert_data--默認用戶表空間
temporary tablespace tbs_expert_temp; --默認臨時表空間

授權

--給用戶授予權限 
-- 授予連接,資源權限
grant connect,resource to expert ; 
-- 授予DBA權限
grant dba to expert ;

刪除相關

--刪除空的表空間,但是不包含物理文件
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;

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

表空間統計

-- 統計信息
SELECT a.tbs_type,
       a.tablespace_name "表空間名",
       trunc(a.bytes / 1024 / 1024) "表空間大小(M)",
       trunc((a.bytes - b.bytes) / 1024 / 1024) "已使用空間(M)",
       trunc(b.bytes / 1024 / 1024) "空閒空間(M)",
       round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "使用比"
  FROM (SELECT '永久' tbs_type, tablespace_name, sum(bytes) bytes
          FROM dba_data_files
         GROUP BY tablespace_name
        union
        SELECT '臨時' tbs_type, tablespace_name, sum(bytes) bytes
          FROM dba_temp_files
         GROUP BY tablespace_name
        ) a,
       (SELECT '永久' tbs_type, tablespace_name, sum(bytes) bytes
          FROM dba_free_space
         GROUP BY tablespace_name
        union
        SELECT '臨時' tbs_type, tablespace_name, sum(free_space) bytes
          FROM dba_temp_free_space
         GROUP BY tablespace_name
        ) b
 WHERE a.tablespace_name = b.tablespace_name
   and a.tbs_type = b.tbs_type
 ORDER BY a.tbs_type, ((a.bytes - b.bytes) / a.bytes) DESC;

-- 表空間文件達到上限時添加文件
alter tablespace 表空間名稱 add datafile '全路徑的數據文件名稱' size ***M
autoextend on maxsize 20G;

SELECT file_id, file_name, tablespace_name, autoextensible, increment_by
FROM dba_data_files
WHERE tablespace_name = 'TBS_XX'
ORDER BY file_id desc;

# 添加新的數據文件
alter tablespace tbs_duty add datafile '/home/oracle/app/oracle/oradata/orcl/tbs_XX010.dbf' size 1024M
autoextend on
next 128m maxsize UNLIMITED 
extent management local;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章