1. 程式人生 > >oralcle數據庫常用SQL語句

oralcle數據庫常用SQL語句

media user 刪除索引 序列號 bst rep synonyms column 自增

表空間

SQL>select username,default_tablespace from user_users;

1.查看當前用戶的角色

SQL>select * from user_role_privs;

2.查看當前用戶的系統權限和表級權限

SQL>select * from user_sys_privs;

SQL>select * from user_tab_privs;

3.查看用戶下所有的表

SQL>select * from user_tables;

用戶



1.查看當前用戶的缺省表空間

SQL>select username,default_tablespace from user_users;

2.查看當前用戶的角色

SQL>select * from user_role_privs;

3.查看當前用戶的系統權限和表級權限

SQL>select * from user_sys_privs;

SQL>select * from user_tab_privs;

4.顯示當前會話所具有的權限

SQL>select * from session_privs;

5.顯示指定用戶所具有的系統權限


SQL>select * from dba_sys_privs where grantee=‘GAME‘;



1.查看用戶下所有的表

SQL>select * from user_tables;

SELECT * FROM ALL_TABLES;

2.查看名稱包含log字符的表

SQL>select object_name,object_id from user_objects

where instr(object_name,‘LOG‘)>0;

3.查看某表的創建時間


SQL>select object_name,created from user_objects where object_name=upper(‘&table_name‘);

4.查看某表的大小

SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

where segment_name=upper(‘&table_name‘);

查看放在ORACLE的內存區裏的表

SQL>select table_name,cache from user_tables where instr(cache,‘Y‘)>0;

索引

1.查看索引個數和類別

SQL>select index_name,index_type,table_name from user_indexes order by table_name;

2.查看索引被索引的字段

SQL>select * from user_ind_columns where index_name=upper(‘&index_name‘);

3.查看索引的大小

SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

where segment_name=upper(‘&index_name‘);

序列號

1.查看序列號,last_number是當前值

SQL>select * from user_sequences;

視圖

1.查看視圖的名稱

SQL>select view_name from user_views;

2.查看創建視圖的select語句

SQL>set view_name,text_length from user_views;

SQL>set long 2000; 說明:可以根據視圖的text_length值設定set long 的大小

SQL>select text from user_views where view_name=upper(‘&view_name‘);

同義詞

1.查看同義詞的名稱

SQL>select * from user_synonyms;

SELECT * FROM ALL_SYSNONYMS;

約束條件

1.查看某表的約束條件

SQL>select constraint_name, constraint_type,search_condition, r_constraint_name

from user_constraints where table_name = upper(‘&table_name‘);

SQL>select c.constraint_name,c.constraint_type,cc.column_name

from user_constraints c,user_cons_columns cc

where c.owner = upper(‘&table_owner‘) and c.table_name = upper(‘&table_name‘)

and c.owner = cc.owner and c.constraint_name = cc.constraint_name

order by cc.position;

存儲函數和過程

1.查看函數和過程的狀態

SQL>select object_name,status from user_objects where object_type=‘FUNCTION‘;

SQL>select object_name,status from user_objects where object_type=‘PROCEDURE‘;

2.查看函數和過程的源代碼

SQL>select text from all_source where owner=user and name=upper(‘&plsql_name‘);

-- 查詢本用戶的表的列名和註釋
select * from user_col_comments
-- 查詢本用戶的視圖
select * from user_tab_comments where table_type=‘VIEW‘
-----查詢用戶所有的存儲過程
select * from dba_source where OWNER=‘ZLZ‘ AND TYPE=‘PROCEDURE‘ ;
-----查詢用戶所有的包
select * from dba_source where OWNER=‘ZLZ‘ AND TYPE=‘PACKAGE‘ ;
-----查詢用戶所有的函數
select * from dba_source where OWNER=‘ZLZ‘ AND TYPE=‘FUNCTION‘ ;
---物化視圖日誌
SELECT * FROM user_MVIEW_LOGS
---物化視圖
select *from USER_dependencies where type = ‘MATERIALIZED VIEW‘

SELECT GRANTEE, OWNER,TABLE_NAME,GRANTOR FROM USER_TAB_PRIVS ---查詢表存儲過程的賦權

SELECT DISTINCT GRANTEE, OWNER, TABLE_NAME, GRANTOR FROM USER_COL_PRIVS ----查詢表字段授權

----oracle查看表分區----
select table_owner, table_name, partition_name, tablespace_name
from dba_tab_partitions
where table_name in (‘ZLZ_TEST_FAMILY_PART_RANGE‘,
‘ZLZ_TEST_FAMILY_PART_HASH‘,
‘ZLZ_TEST_FAMILY_PART_LIST‘);

如何殺掉imp/emp進程
a.執行命令:ps -ef | grep exp | grep -v grep | awk ‘{print "kill -9 "$2}‘ >test.sh
b.執行命令ll ,test.sh 大小不為0 時,執行 /bin/sh test.sh
c.等於0就沒有要殺的

查詢數據源的數據泵路徑:select * from dba_directories;

修改表中字段:select t.*,t.rowid from zlz_test t;
刪除表:drop table 表名稱
清除表數據:truncate table 表名稱
刪除某一列的值:delete from 表名稱 where 列名稱 = 值

--加表字段
alter table zlz_test add homenum varchar2(12);

---增加zlz_test_2_1表的三個時間字段
update zlz_test_2_1 set date_id=sysdate-dbms_random.value
update zlz_test_2_1 set timestamp_id=sysdate-dbms_random.value
update zlz_test_2_1 set time_id=sysdate-dbms_random.value

oracle增加主鍵:alter table ZLZ_TDM_35 add constraint pk_card1 primary key(st_id)
oracle增加外鍵:alter table zlz_test_family_35 add constraint fk_family1 foreign key(st_id) references ZLZ_TDM_35 (st_id);
顯示數據庫所有分區表的信息:select * from DBA_PART_TABLES

drop user zlz_test cascade; ---刪除schema
create user zlztest identified by zlztest; ----創建schema
grant dba to zlztest; ----給schema賦dba權限
revoke dba from zlztest; -----去掉schema的dba權限

查看當前用戶的索引和約束
select index_name, table_name,status,logging from user_indexes;
select owner, constraint_name, constraint_type,table_name,status from user_constraints;
status = valid 是啟用
status為ENABLED 是啟用

oracle查看用戶角色: select * from user_role_privs;
oracle查看用戶權限: select * from dba_sys_privs where grantee=‘ZLZ‘;

--創建索引
create index 索引名 on 表名 (字段1, 字段2, 字段3);
--禁用索引
ALTER INDEX <indexName> UNUSABLE ;
--恢復索引
ALTER INDEX <indexName> REBUILD ;
--刪除索引
drop index 索引名

----查看表大小-----
Select Segment_Name,Sum(bytes)/1024/1024/1024 GB From dba_Extents where owner=‘CXC_SOURCE‘ and Segment_Name=‘NORMALTABLE_5G_1‘ Group By Segment_Name;

---查詢當前用戶下全部的表及表大小m-----
select SEGMENT_NAME ,sum( bytes/1024/1024/1024) from DBA_segments where owner=‘CXC_SOURCE‘ AND segment_type = ‘TABLE‘ GROUP BY SEGMENT_NAME ORDER BY 2 DESC ;

-----查每個對象
select owner , name "對象名", type "對象類型", referenced_name "關聯表" from USER_DEPENDENCIES where referenced_name = ‘INT_TBL_EVENT‘ and referenced_type = ‘TABLE‘

----統計個對象
select count(object_name) num, object_type from USER_objects group by object_type order by num desc

----查ddl語句
select dbms_metadata.get_ddl(‘TABLE‘,‘SZX_C_O_01‘,‘SUNZHAOXIONG‘) from dual

-----統計索引
select count(index_name) num, table_name from user_indexes group by table_name order by num asc,table_name
select count(index_name) from user_indexes
select COUNT from user_objects where object_type = ‘INDEX‘
--分區所以
select * from user_objects where object_type = ‘INDEX PARTITION‘
--每個索引有多少條分區索引
select count(subobject_name) num, object_name from USER_objects where object_type = ‘INDEX PARTITION‘ group by object_name order by num ,object_name

select * from user_indexes where index_name = ‘INT_TBL_PTRANACCT‘
select * from user_indexes where table_name = ‘INT_TBL_EVENT‘

------查詢關聯關系
select *from USER_dependencies where referenced_type = ‘TABLE‘

------使用這條語句查哪臺主機使用FY連數據庫-------
select sid,serial#,machine,osuser from v$session where username=‘FY‘

----oracle查看類型種類和總個數-----
select object_type ,COUNT(*) from dba_objects where owner=‘TEST1‘ GROUP BY object_type
ORDER BY 2;

----oracle查看類型種類詳情----
select object_name,object_type from dba_objects where owner=‘TEST1‘ ORDER BY 2,1

---oracle刪除用戶---
user test;
若用戶擁有對象,則不能直接刪除,否則將返回一個錯誤值。指定關鍵字cascade,可刪除用戶所有的對象,然後再刪除用戶。
drop user 用戶名 cascade;

擁有connect role 的用戶還能夠創建表、視圖、序列(sequence)、簇(cluster)、同義詞(synonym)、回話(session)和其他 數據的鏈(link)


2》. resource role(資源角色)

更可靠和正式的數據庫用戶可以授予resource role。

resource提供給用戶另外的權限以創建他們自己的表、序列、過程(procedure)、觸發器(trigger)、索引(index)和簇(cluster)。


3》. dba role(數據庫管理員角色)

dba role擁有所有的系統權限
包括無限制的空間限額和給其他用戶授予各種權限的能力。system由dba用戶擁有

(2)授權命令

語法: grant connect, resource to 用戶名;

例子: grant connect, resource to test;

(3)撤銷權限

語法: revoke connect, resource from 用戶名;

列子: revoke connect, resource from test;


----oracle創建/授權/刪除角色----

除了前面講到的三種系統角色

connect、resource和dba,用戶還可以在oracle創建自己的role。用戶創建的role可以由表或系統權限或兩者的組合構成。為了創建role,用戶必須具有create role系統權限。

1》創建角色

語法: create role 角色名;

例子: create role testRole;

2》授權角色

語法: grant select on class to 角色名;

列子: grant select on class to testRole;

註:現在,擁有testRole角色的所有用戶都具有對class表的select查詢權限

3》刪除角色

語法: drop role 角色名;

例子: drop role testRole;

註:與testRole角色相關的權限將從數據庫全部刪除

---- --數據庫允許的最大連接數---
select value from v$parameter where name =‘processes‘;

---- --當前的數據庫連接數-----
select count(*) from v$process ;

------修改最大連接數:------
alter system set processes = 300 scope = spfile;
註意:要關閉數據庫重啟才能生效
shutdown immediate; --關閉數據庫
startup; --重啟數據庫

-------查看當前有哪些用戶正在使用數據----
select osuser, a.username, cpu_time/executions/1000000||‘s‘, b.sql_text, machine
from v$session a, v$sqlarea b
where a.sql_address =b.address
order by cpu_time/executions desc;

----當前的session連接數---
select count(*) from v$session

----並發連接數---
select count(*) from v$session where status=‘ACTIVE‘; 

---【【【 在表空間新增數據文件和修改表空間數據文件的大小 / 擴表空間 / 表空間擴展 】】】----

現在服務器上查剩余磁盤(看數據文件掛載[mount]的那個磁盤)
df -h

查看當前用戶所屬的表空間
select * from dba_users where username=‘CXC_SOURCE‘;

指定表空間找到數據文件的file_name
SELECT file_name, tablespace_name, bytes/(1024*1024) M FROM dba_data_files where tablespace_name=‘SHSNC‘;

修改表空間數據文件的大小
ALTER database datafile ‘/u01/app/oracle/product/10.1/dbs/users01.dbf‘ RESIZE 32M;

在表空間新增加數據文件(單個數據文件最大限制32G)--註意:每個文件大小最大30G-----
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc35.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc36.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc37.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc38.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc39.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc40.dbf‘ SIZE 30G;
ALTER TABLESPACE SHSNC ADD DATAFILE ‘/home/app/oradata/orcl/shsnc41.dbf‘ SIZE 30G;

重新查詢
SELECT file_name, tablespace_name, bytes/(1024*1024) M FROM dba_data_files where tablespace_name=‘USERS‘;

--添加表字段數據的兩種方法
select * from zlz_test for update
select rowid,t.* from zlz_test t

--驗證表中字段正則表達式是否正確
select ‘A‘,a.* from zlz_test1 a union all
select ‘B‘,b.* from zlz_test1 b where regexp_like(address,‘^(.*省)+.+市.+(區|縣)‘);


-------查表所屬分區-----
select * from all_all_tables where table_name=‘zlz_fenqu_1‘

----新增一個表,將表中ID字段修改為自增長
create table ZLZ_TEST_CHINESE
(
id NUMBER,
name VARCHAR2(28)
)

create sequence st_id
increment by 1
start with 1
maxvalue 999999 ---建立一個最小為1,最大為999999的一個序列號會自動循環的序列
cycle;

--查看當前數據源的表空間容量信息

select dbf.tablespace_name,
dbf.totalspace "總量(M)",
dbf.totalblocks as "總塊數",
dfs.freespace "剩余總量(M)",
dfs.freeblocks "剩余塊數",
(dfs.freespace / dbf.totalspace) * 100 "空閑比例"
from (select t.tablespace_name,
sum(t.bytes) / 1024 / 1024 totalspace,
sum(t.blocks) totalblocks
from dba_data_files t
group by t.tablespace_name) dbf,
(select tt.tablespace_name,
sum(tt.bytes) / 1024 / 1024 freespace,
sum(tt.blocks) freeblocks
from dba_free_space tt
group by tt.tablespace_name) dfs
where trim(dbf.tablespace_name) = trim(dfs.tablespace_name);

--查看表空間使用百分比
select total.tablespace_name,
  round(total.MB, 2) as Total_MB,
  round(total.MB - free.MB, 2) as Used_MB,
  round((1 - free.MB / total.MB) * 100, 2) || ‘%‘ as Used_Pct
  from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
  from dba_free_space
  group by tablespace_name) free,
  (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
  from dba_data_files
  group by tablespace_name) total
  where free.tablespace_name = total.tablespace_name and free.tablespace_name=‘ZLZTBS‘;

--建立刪表語句(含表結構)
select ‘drop table ‘||segment_name||‘ purge;‘ MB
from dba_segments
where tablespace_name = ‘ZLZTBS‘ and segment_name not like ‘%$$‘
group by segment_name;

--查看某個表是否正在執行與殺進程
select t.P1,
t.P2,
t.P3,
t.event,
t.sid,
BLOCKING_SESSION,
t.SQL_EXEC_START,
(sysdate - t.SQL_EXEC_START) * 24,
t.PROGRAM,
T.INST_ID,
t.SQL_ID,
T.pdml_status,
T.pddl_status,
T.SID,
t.PROGRAM,
t.SERVICE_NAME,
t.USERNAME,
t.MACHINE,
t.PROCESS,
T.SERIAL#,
‘‘‘;‘,
to_char(substr(a.SQL_FULLTEXT, 1, 3000)),
a.SQL_FULLTEXT,
‘alter system kill session ‘‘‘ || t.sid || ‘,‘ || t.SERIAL# || ‘,@‘ ||
t.inst_id || ‘‘‘;‘,
t.USERNAME,
t.STATUS
from Gv$sql a, gv$session t
where t.sql_id = a.SQL_ID
and a.sql_id in
(select a.SQL_ID
from gv$session a, gv$sqlarea b
where a.SQL_ID = b.SQL_ID
and a.INST_ID = b.INST_ID
and b.SQL_TEXT like ‘%SIM_TB_ACCESS_LOG_BAK%‘)
and t.INST_ID = a.INST_ID
and a.SQL_FULLTEXT not like ‘%gv$session%‘
and upper(a.SQL_FULLTEXT) like ‘%SIM_TB_ACCESS_LOG_BAK%‘;


oralcle數據庫常用SQL語句