Oracle中检查外键是否有索引的SQL脚本分享

代码如下:
COLUMN COLUMNS format a30 word_wrapped
COLUMN tablename format a15 word_wrapped
COLUMN constraint_name format a15 word_wrapped
SELECT TABLE_NAME,
CONSTRAINT_NAME,
CNAME1 || NVL2(CNAME2,',' || CNAME2,NULL) ||
NVL2(CNAME3,' || CNAME3,NULL) ||
NVL2(CNAME4,' || CNAME4,NULL) ||
NVL2(CNAME5,' || CNAME5,NULL) ||
NVL2(CNAME6,' || CNAME6,NULL) ||
NVL2(CNAME7,' || CNAME7,NULL) ||
NVL2(CNAME8,' || CNAME8,NULL) COLUMNS
FROM (SELECT B.TABLE_NAME,
B.CONSTRAINT_NAME,
MAX(DECODE(POSITION,1,COLUMN_NAME,NULL)) CNAME1,2,NULL)) CNAME2,3,NULL)) CNAME3,4,NULL)) CNAME4,5,NULL)) CNAME5,6,NULL)) CNAME6,7,NULL)) CNAME7,8,NULL)) CNAME8,
COUNT(*) COL_CNT
FROM (SELECT SUBSTR(TABLE_NAME,30) TABLE_NAME,
SUBSTR(CONSTRAINT_NAME,30) CONSTRAINT_NAME,
SUBSTR(COLUMN_NAME,30) COLUMN_NAME,
POSITION
FROM USER_CONS_COLUMNS) A,
USER_CONSTRAINTS B
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
AND B.CONSTRAINT_TYPE = 'R'
GROUP BY B.TABLE_NAME,B.CONSTRAINT_NAME) CONS
WHERE COL_CNT > ALL
(SELECT COUNT(*)
FROM USER_IND_COLUMNS I
WHERE I.TABLE_NAME = CONS.TABLE_NAME
AND I.COLUMN_NAME IN (CNAME1,CNAME2,CNAME3,CNAME4,CNAME5,
CNAME6,CNAME7,CNAME8)
AND I.COLUMN_POSITION <= CONS.COL_CNT
GROUP BY I.INDEX_NAME)
/

在上面的基础上修改了一下,可以检查所有的用户
代码如下:
SET linesize 400;
COLUMN OWNER format a10 word_wrapped
COLUMN COLUMNS format a30 word_wrapped
COLUMN TABLE_NAME format a15 word_wrapped
COLUMN CONSTRAINT_NAME format a40 word_wrapped
SELECT OWNER,
TABLE_NAME,NULL) COLUMNS
FROM (SELECT B.OWNER,B.TABLE_NAME,
POSITION
FROM DBA_CONS_COLUMNS WHERE OWNER NOT IN ('SYS','SYSTEM','SYSMAN','HR','OE','EXFSYS','DBSNMP','MDSYS','OLAPSYS','SCOTT','SH','PM','CTXSYS')) A,
DBA_CONSTRAINTS B
WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
AND B.CONSTRAINT_TYPE = 'R'
GROUP BY B.OWNER,B.CONSTRAINT_NAME) CONS
WHERE COL_CNT > ALL
(SELECT COUNT(*)
FROM DBA_IND_COLUMNS I
WHERE I.TABLE_NAME = CONS.TABLE_NAME AND I.TABLE_OWNER=CONS.OWNER
AND I.COLUMN_NAME IN (CNAME1,CNAME8)
AND I.COLUMN_POSITION <= CONS.COL_CNT
GROUP BY I.INDEX_NAME)
/

相关文章

数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增...
(一)问题背景最近在对一个大约200万行数据的表查看执行计划时,发现存在异常,理论上应该返回100多万...
(一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件。当使用RMAN执行备份操作时,会在RM...
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文...
RMAN(Recovery Manager)是Oracle恢复管理器的简称,是集数据库备份(backup)、修复(restore)和恢复...
(1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件; --数...