Oracle的基本操作
- 寫在前面
- 一、登入
- 1.1、Linux指令行
- 1.2、Navicate工具
- 1.3、SQLPLUS工具
- 1.4、PL/SQL
- 二、基本語句
- 2.1、查詢表空間
- 2.2、查詢使用者
- 2.3、權限和角色查詢
- 2.4、檢視函數,存儲過程和視圖
- 2.5、查詢表資訊
- 2.6、常用腳本
- 2.7 get DDL
寫在前面
一、登入
1.1、Linux指令行
跟安裝方式有關,有時會比較麻煩
我這裡有個,可供參考
- 先切換到oracle使用者
su - oracle
sqlplus
這是會提示輸入使用者和密碼
使用者:sys
密碼:as sysdba
1.2、Navicate工具
圖一是Basic連接配接,根據
Host:
port:
Service Name:
User name:
pass:
圖二是根據 SSH server,先ssh 連接配接裝有Oracle的伺服器,在嘗試這種連接配接方式
1.3、SQLPLUS工具
1.4、PL/SQL
二、基本語句
2.1、查詢表空間
---- 查詢所有表空間
select * from dba_tablespaces;
select * from user_tablespaces;
---- 查詢使用過的表空間
select distinct tablespace_name from dba_all_tables;
select distinct tablespace_name from user_all_tables;
---- 查詢表空間中所有表的名稱
select * from user_all_tables where tablespace_name = 'USER';
select * from dba_all_tables where tablespace_name = 'USER';
表空間統計
select a.file_id "FileNo",
a.tablespace_name "表空間",
a.bytes/1024/1021/1024 Bytes_size,
a.bytes - sum(nvl(b.bytes, 0)) "已用",
sum(nvl(b.bytes, 0)) "空閑",
sum(nvl(b.bytes, 0)) / a.bytes * 100 "空閑百分率"
from dba_data_files a, dba_free_space b
where a.file_id = b.file_id(+)
group by a.tablespace_name, a.file_id, a.bytes
order by a.tablespace_name;
檢視表空間,檔案路徑
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name
2.2、查詢使用者
---- 查詢系統使用者
select * from all_users
select * from dba_users
----檢視目前連接配接作業系統資訊
select * from v$session
檢視目前使用者的身份
select * from V$PWFILE_USERS
2.3、權限和角色查詢
----檢視目前使用者權限
select * from session_privs;
SELECT * FROM USER_SYS_PRIVS;
SELECT * FROM DBA_SYS_PRIVS;
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;
select * from dba_roles;
SELECT * FROM DBA_ROLE_PRIVS;
SELECT * FROM USER_ROLE_PRIVS;
2.4、檢視函數,存儲過程和視圖
select * from user_source;
2.5、查詢表資訊
select * from user_tables;
select * from dba_tables;
2.6、常用腳本
禁用外鍵腳本
----ORACLE資料庫中的外鍵限制名都在表user_constraints中可以查到。其中constraint_type='R'表示是外鍵限制。
----啟用外鍵限制的指令為:
alter table table_name enable constraint constraint_name
----禁用外鍵限制的指令為:
alter table table_name disable constraint constraint_name
----然後再用SQL查出資料庫中是以外鍵的限制名:
select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='R'
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='R'
--啟用腳本
SET SERVEROUTPUT ON SIZE 1000000
BEGIN
for c in (select 'ALTER TABLE '||TABLE_NAME||' ENABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop
DBMS_OUTPUT.PUT_LINE(C.V_SQL);
begin
EXECUTE IMMEDIATE c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
for c in (select 'ALTER TABLE '||TNAME||' ENABLE ALL TRIGGERS ' AS v_sql from tab where tabtype='TABLE') loop
dbms_output.put_line(c.v_sql);
begin
execute immediate c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
/
commit;
--禁用腳本
SET SERVEROUTPUT ON SIZE 1000000
BEGIN
for c in (select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop
DBMS_OUTPUT.PUT_LINE(C.V_SQL);
begin
EXECUTE IMMEDIATE c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
for c in (select 'ALTER TABLE '||TNAME||' DISABLE ALL TRIGGERS ' AS v_sql from tab where tabtype='TABLE') loop
dbms_output.put_line(c.v_sql);
begin
execute immediate c.v_sql;
exception when others then
dbms_output.put_line(sqlerrm);
end;
end loop;
end;
/
commit;
2.7 get DDL
select dbms_metadata.get_ddl('USER', username) || '/' usercreate from dba_users ;
select 'alter user ' || username ||' identified by values ''' || password || ''';'from dba_users;