1. 程式人生 > 實用技巧 >查詢oracle中所有使用者資訊(轉)

查詢oracle中所有使用者資訊(轉)

----查詢oracle中所有使用者資訊
----1、查詢資料庫中的表空間名稱

----1)查詢所有表空間

select tablespace_name from dba_tablespaces;
select tablespace_name from user_tablespaces;

----2)查詢使用過的表空間  

select distinct tablespace_name from dba_all_tables;

select distinct tablespace_name from user_all_tables;

----2、查詢表空間中所有表的名稱

select *  from dba_all_tables where tablespace_name = 'SYNC_PLUS_1' and owner='GDSDCZJ'

----3、查詢系統使用者

select * from all_users
select * from dba_users

----4、檢視當前連線使用者

select * from v$session

----5、檢視當前使用者許可權

select * from session_privs

----6、檢視所有的函式和儲存過程

select * from user_source

----其中TYPE包括:PROCEDURE、FUNCTION

----7、查看錶空間使用情況
select  sum(Bytes_size) from (
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
 );




-----------------------------------------------------

----1.檢視所有使用者:
select * from dba_users;
select * from all_users;
select * from user_users;

----2.檢視使用者或角色系統許可權(直接賦值給使用者或角色的系統許可權):
select * from dba_sys_privs;
select * from user_sys_privs; (檢視當前使用者所擁有的許可權)

----3.檢視角色(只能檢視登陸使用者擁有的角色)所包含的許可權
sql>select * from role_sys_privs;

----4.檢視使用者物件許可權:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;

----5.檢視所有角色:
select * from dba_roles;

----6.檢視使用者或角色所擁有的角色:
select * from dba_role_privs;
select * from user_role_privs;

----7.檢視哪些使用者有sysdba或sysoper系統許可權(查詢時需要相應許可權)
select * from V$PWFILE_USERS

----8.SqlPlus中檢視一個使用者所擁有許可權
SQL>select * from dba_sys_privs where grantee='username';
其中的username即使用者名稱要大寫才行。
比如:
SQL>select * from dba_sys_privs where grantee='TOM';


----9、Oracle刪除指定使用者所有表的方法
select 'Drop table '||table_name||';' from all_tables
where owner='要刪除的使用者名稱(注意要大寫)';

----10、刪除使用者
drop user user_name cascade;
如:drop user SMCHANNEL CASCADE

----11、獲取當前使用者下所有的表:
select table_name from user_tables;

----12、刪除某使用者下所有的表資料:
 select 'truncate table  ' || table_name from user_tables;

----13、禁止外來鍵
----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'

--14、ORACLE禁用/啟用外來鍵和觸發器
--啟用指令碼
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;

轉自 https://www.cnblogs.com/ios9/p/9100185.html