oracle表空間、用戶、權限相關指令和語句

一、術語定義

 1、終端

  利用xshell或其他工具,以ssh協議或telnet協議連接到遠程主機的遠程連接工具

 2、終端命令行

  端連接到遠程主機後,出現的可輸入指令的行

 3、oracle命令行

  在終端命令行輸入指令進入oracle的操作命令行

 4、表空間

  爲存放表數據定義的一個空間,可以將數據庫裏不同功能的表分配到不同的表空間去**

二、常用指令

 1、在終端命令行輸入指令登入oracle命令行

  1.1、系統根用戶登入:
sqlplus / as sysdba
  1.2、普通權限用戶登入:
sqlplus dbname/dbpwd

 2、利用系統根用戶進入oracle命令行,對數據庫進行初始配置

  2.1、查詢臨時表空間存放目錄(以便創建臨時表空間時指定到同一個目錄下)
select name from v$tempfile;
--這裏查到的是:/home/oracle/app/oracle/oradata/oral/temp01.dbf
  2.2、創建臨時表空間(256M,後續20M遞增無上限)
create temporary tablespace SMARTDB_TEMP tempfile
'/home/oracle/app/oracle/oradata/oral/smartdbTemp.dbf' size 256m
reuse autoextend on next 20m maxsize unlimited; 
  2.3、查詢用戶表空間存放目錄(以便創建用戶表空間時指定到同一個目錄下)
select name from v$datafile;
--這裏查到5個,目錄與臨時表空間相同
  2.4、創建用戶表空開(10G,後續40M遞增無上限)
create tablespace SMART_DATA datafile 
	'/home/oracle/app/oracle/oradata/oral/smartData.dbf' size 10240M
	reuse autoextend on next 40M maxsize unlimited
	default storage(initial 128k next 128k minextents 2 maxextents unlimited);
  2.5、創建用戶smartdb/smartdb
create user smartdb identified by smartdb
  2.6、爲用戶解鎖
alter user smartdb account unlock;
  2.7、爲用戶指定默認表空間
alter user smartdb default tablespace SMART_DATA;
  2.8、爲用戶指定臨時表空間
alter user smartdb temporary tablespace SMARTDB_TEMP;
  2.9、爲用戶賦相應權限
grant create user, drop user,alter user,create any view,drop any view,
	exp_full_database,imp_full_database  to smartdb;
grant connect,resource,dba to smartdb;
grant create session to smartdb;
grant sysdba to smartdb;
  2.10、爲用戶修改密碼
alter user smartdb identified by newPassword;

 3、常用查詢命令(oracle命令行執行)

  3.1、查詢臨時表空間存放目錄
select name from v$tempfile;
  3.2、查詢用戶表空間存放目錄
select name from v$datafile;
  3.3、查詢表空間的使用情況
SELECT
	a.tablespace_name "表空間名", 
	total "表空間大小", 
	free "表空間剩餘大小", 
	(total - free) "表空間使用大小", 
	total / (1024 * 1024 * 1024) "表空間大小(G)", 
	free / (1024 * 1024 * 1024) "表空間剩餘大小(G)", 
	(total - free) / (1024 * 1024 * 1024) "表空間使用大小(G)", 
	round((total - free) / total, 4) * 100 "使用率 %" 
FROM 
	(SELECT tablespace_name, SUM(bytes) free
		FROM dba_free_space GROUP BY tablespace_name) a, 
	(SELECT tablespace_name, SUM(bytes) total
		FROM dba_data_files GROUP BY tablespace_name) b 
WHERE
	a.tablespace_name = b.tablespace_name
  3.4、查看錶空間物理文件的名稱及大小
SELECT tablespace_name, file_id, file_name, 
	round(bytes / (1024 * 1024), 0)||'M' total_space
FROM dba_data_files 
  3.5、查看控制文件
SELECT NAME FROM v$controlfile;
  3.6、查看日誌文件
SELECT MEMBER FROM v$logfile;
  3.7、查看數據庫的版本
SELECT version 
FROM product_component_version 
WHERE substr(product, 1, 6) = 'Oracle'; 
  3.8、查看數據庫的創建日期和歸檔方式
SELECT created, log_mode, log_mode FROM v$database;

 4、批量修改表所在表空間

  4.1、單個轉移表所在表空間的指令
slter table t_student move tablespace SMART_DATA;
  4.2、批量轉移表空間->1、查詢出用戶下的所有表;2、拼接轉移語句
--查詢出來的清單語句(每一條都是轉移表所在表空間指令)複製到命令行執行即可
select 'alter table  '|| table_name ||'  move tablespace SMART_DATA;'
from dba_tables where owner='SMARTDB'; 

 5、批量修改索引所在表空間

  5.1、單個轉移索引所在表空間的指令
slter index index_student rebuild tablespace SMART_DATA;
  5.2、批量轉移表空間->1、查詢出用戶下的所有索引;2、拼接轉移語句
--查詢出來的清單語句(每一條都是轉移索引所在表空間指令)複製到命令行執行即可
select'alter index '|| index_name ||' rebuild tablespace SMART_DATA;'
from  user_indexes where table_owner='SMARTDB'; 

 6、用戶權限查詢

  6.1、查看所有的用戶
select * from all_users;
  6.2、查看當前用戶信息
select * from user_users;
  6.3、查看當前用戶的角色
select * from user_role_privs;
  6.4、查看當前用戶的權限
select * from user_sys_privs;
  6.5、查看當前用戶的表可操作權限
select * from user_tab_privs;
  6.6、查看某一個表的約束,注意表名要 大寫
select * from user_constraints where table_name='TBL_XXX';
  6.7、查看某一個表的所有索引,注意表名要 大寫
select index_name,index_type,status,blevel
from user_indexes where table_name = 'TBL_XXX';
  6.8、查看索引的構成,注意表名要 大寫
select table_name,index_name,column_name, column_position
FROM user_ind_columns WHERE table_name='TBL_XXX';
  6.9、系統數據字典 DBA_TABLESPACES 中記錄了關於表空間的詳細信息
select * from sys.dba_tablespaces;
  6.10、查看用戶序列
select * from user_sequences;
  6.11、查看數據庫序列
select * from dba_sequences;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章