oracle常用sql收集

1、查找表的所有索引(包括索引名,类型,构成列):
SELECT T.*, I.INDEX_TYPE

  FROM USER_IND_COLUMNS T, USER_INDEXES I
 WHERE T.INDEX_NAME = I.INDEX_NAME
   AND T.TABLE_NAME = I.TABLE_NAME
   AND T.TABLE_NAME = 'TABLE_NAME'

将多列索引,列名合并

SELECT TABLE_NAME, INDEX_NAME, MAX(COLUMN_NAME), INDEX_TYPE

  FROM (SELECT T.INDEX_NAME,
               T.TABLE_NAME,
               WMSYS.WM_CONCAT(T.COLUMN_NAME) OVER(PARTITION BY T.INDEX_NAME, T.TABLE_NAME, I.INDEX_TYPE ORDER BY T.COLUMN_POSITION) COLUMN_NAME,
               I.INDEX_TYPE
          FROM USER_IND_COLUMNS T, USER_INDEXES I
         WHERE T.INDEX_NAME = I.INDEX_NAME
           AND T.TABLE_NAME = I.TABLE_NAME
           AND T.TABLE_NAME IN ('MYTABLE'))
 GROUP BY INDEX_NAME, TABLE_NAME, INDEX_TYPE
 ORDER BY TABLE_NAME, INDEX_NAME
目前对函数索引,无法 显示索引内容。

2、查找表的主键(包括名称,构成列):
SELECT CU.*
  FROM USER_CONS_COLUMNS CU, USER_CONSTRAINTS AU
 WHERE CU.CONSTRAINT_NAME = AU.CONSTRAINT_NAME
   AND AU.CONSTRAINT_TYPE = 'P'
   AND AU.TABLE_NAME = 'TABLE_NAME'

3、查找表的唯一性约束(包括名称,构成列):
SELECT COLUMN_NAME
  FROM USER_CONS_COLUMNS CU, USER_CONSTRAINTS AU
 WHERE CU.CONSTRAINT_NAME = AU.CONSTRAINT_NAME
   AND AU.CONSTRAINT_TYPE = 'U'
   AND AU.TABLE_NAME = 'TABLE_NAME'

4、查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询):
SELECT *
  FROM USER_CONSTRAINTS C
 WHERE C.CONSTRAINT_TYPE = 'R'
   AND C.TABLE_NAME = 'TABLE_NAME'

查询外键约束的列名:
SELECT * FROM USER_CONS_COLUMNS CL WHERE CL.CONSTRAINT_NAME = '外键名称'

查询引用表的键的列名:
SELECT *
  FROM USER_CONS_COLUMNS CL
 WHERE CL.CONSTRAINT_NAME = '外键引用表的键名'

5、查询表的所有列及其属性
SELECT T.*, C.COMMENTS
  FROM USER_TAB_COLUMNS T, USER_COL_COMMENTS C
 WHERE T.TABLE_NAME = C.TABLE_NAME
   AND T.COLUMN_NAME = C.COLUMN_NAME
   AND T.TABLE_NAME = 'TABLE_NAME'


6、oracle锁定表解锁

查询锁表记录

select sess.sid,  
    sess.serial#, 
    lo.oracle_username, 
    lo.os_user_name, 
    ao.object_name, 
    lo.locked_mode 
    from v$locked_object lo, 
    dba_objects ao, 
    v$session sess 
where ao.object_id = lo.object_id and lo.session_id = sess.sid; 
解锁语句
alter system kill session 'SID,SERIAL#';


 


你可能感兴趣的:(oracle,sql,session,user,table,constraints)