Oracle數據庫常用的腳本命令(一)


--連接數據庫的命令connect,用於切換連接用戶,簡寫形式conn
--語法格式:conn 用戶名/密碼
conn yanln/yanln

--顯示當前登錄的用戶
show user

--執行操作系統的命令
host mkdir d:\testOracle

--導出記錄到文本
spool d:\testOracle\test.txt

select * from book;

spool off

--清屏
clear screen

--執行文件系統中的sql語句
start d:\test.sql

--顯示表結構,命令describe,簡寫形式desc
desc student

--顯示錯誤信息
show error

--退出
exit

cmd-->sqlplus-->提示輸入用戶名、密碼的登錄方式

sqlplus /nolog : 進入sqlplus界面但不登錄

--sys用戶以sysdba的身份連接數據庫,連接時指定數據庫連接標識符@orcl
conn sys/oracle @orcl as sysdba

--sys用戶以sysdba的身份連接數據庫,連接時沒有指定數據庫連接標識符,此時將連接默認的數據庫
conn sys/oracle as sysdba
show user

--system用戶直接登錄數據庫
conn system/oracle
show user

--system用戶以sysdba的身份登錄數據庫
conn system/oracle as sysdba
show user

--創建默認表空間
create tablespace test1_tablespace datafile 'test1file.dbf' size 10m;

--創建臨時表空間
create temporary tablespace temptest1_tablespace tempfile 'tempfile1.dbf' size 10m;

--查看表空間數據文件的位置
select file_name from dba_data_files where tablespace_name = 'TEST1_TABLESPACE';

--查看臨時表空間數據文件的位置
select file_name from dba_temp_files where tablespace_name = 'TEMPTEST1_TABLESPACE';

--創建用戶
create user yan identified by test default tablespace test1_tablespace temporary tablespace temptest1_tablespace;

--查看創建的用戶
select username from dba_users;

--給剛創建的用戶授權
grant connect to yan;

--如果想更改用戶的密碼,我們可以通過
alter user yan identified by t123;

--如果管理員不希望某用戶登錄, 但又不打算刪除某用戶,可以將此用戶鎖定
alter user yan account lock;

--如果這個用戶不用了,想刪除這個用戶,可以用drop
drop user yan cascade;

--創建用戶user02
create user user02 identified by pass02;

--創建角色
create role manager;

--給角色賦予創建表、創建視圖的權限
grant create table, create view to manager;

--給角色manager授權給用戶
grant manager to user02;

--回收權限
revoke manager from user02;

--刪除權限
drop role manager;

--查看所有系統權限
select * from system_privilege_map;

--創建用戶
create user user02 identified by pass02;

--給用戶賦予一個創建會話的權限
grant create session to user01;

--通過角色給用戶賦予一個系統權限
create role manager;

grant create table, create sequence to manager;

grant manager to user01;

--查看所有對象權限
select * from table_privilege_map;

--通過角色給用戶賦予一個對象權限
create role manager01;

grant select,update,insert on scott.emp to manager01;

grant manager01 to user01;

--測試對象權限
conn user01/pass01

select * from scott.emp;(成功)

select * from scott.dept;(失敗)

--回收對象權限
revoke select,update,insert on scott.emp from manager01;

--查看管理員級別的表空間描述信息
select tablespace_name from dba_tablespaces;

--查看普通用戶級別的表空間描述信息
select tablespace_name from user_tablespaces;

--dba_users針對管理員級別的數據字典,用於查看數據庫所有用戶的用戶信息
select username,default_tablespace,temporary_tablespace from dba_users;

--user_users針對普通用戶級別的數據字典,用於查看當前登錄用戶的用戶信息
select username,default_tablespace,temporary_tablespace from user_users;

--設置用戶默認和臨時表空間
alter user user01
default tablespace TEST1_TABLESPACE
temporary tablespace TEMPTEST1_TABLESPACE;

-更改表空間的狀態為脫機狀態
alter tablespace test1_tablespace offline;

--更改表空間的狀態為聯機狀態
alter tablespace test1_tablespace online;

--更改表空間的狀態為只讀狀態
alter tablespace test1_tablespace read only;

--更改表空間的狀態為可讀寫狀態
alter tablespace test1_tablespace read write;

--查看表空間的狀態
select status from dba_tablespaces where tablespace_name = 'TEST1_TABLESPACE';

--增加數據文件
alter tablespace test1_tablespace add datafile 'test2_file.dbf' size 10m;

--查看表空間的數據文件
select file_name from dba_data_files where tablespace_name='TEST1_TABLESPACE';

--刪除數據文件
alter tablespace test1_tablespace drop datafile 'test2_file.dbf';

--刪除表空間
drop tablespace test1_tablespace including contents;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM