• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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,
                   MAX(DECODE(POSITION, 2, COLUMN_NAME, NULL)) CNAME2,
                   MAX(DECODE(POSITION, 3, COLUMN_NAME, NULL)) CNAME3,
                   MAX(DECODE(POSITION, 4, COLUMN_NAME, NULL)) CNAME4,
                   MAX(DECODE(POSITION, 5, COLUMN_NAME, NULL)) CNAME5,
                   MAX(DECODE(POSITION, 6, COLUMN_NAME, NULL)) CNAME6,
                   MAX(DECODE(POSITION, 7, COLUMN_NAME, NULL)) CNAME7,
                   MAX(DECODE(POSITION, 8, COLUMN_NAME, NULL)) CNAME8,
                   COUNT(*) COL_CNT
              FROM (SELECT SUBSTR(TABLE_NAME, 1, 30) TABLE_NAME,
                           SUBSTR(CONSTRAINT_NAME, 1, 30) CONSTRAINT_NAME,
                           SUBSTR(COLUMN_NAME, 1, 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,
           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.OWNER,B.TABLE_NAME,
                   B.CONSTRAINT_NAME,
                   MAX(DECODE(POSITION, 1, COLUMN_NAME, NULL)) CNAME1,
                   MAX(DECODE(POSITION, 2, COLUMN_NAME, NULL)) CNAME2,
                   MAX(DECODE(POSITION, 3, COLUMN_NAME, NULL)) CNAME3,
                   MAX(DECODE(POSITION, 4, COLUMN_NAME, NULL)) CNAME4,
                   MAX(DECODE(POSITION, 5, COLUMN_NAME, NULL)) CNAME5,
                   MAX(DECODE(POSITION, 6, COLUMN_NAME, NULL)) CNAME6,
                   MAX(DECODE(POSITION, 7, COLUMN_NAME, NULL)) CNAME7,
                   MAX(DECODE(POSITION, 8, COLUMN_NAME, NULL)) CNAME8,
                   COUNT(*) COL_CNT
              FROM (SELECT SUBSTR(TABLE_NAME, 1, 30) TABLE_NAME,
                           SUBSTR(CONSTRAINT_NAME, 1, 30) CONSTRAINT_NAME,
                           SUBSTR(COLUMN_NAME, 1, 30) COLUMN_NAME,
                           POSITION
                      FROM DBA_CONS_COLUMNS WHERE OWNER NOT IN ('SYS','SYSTEM','SYSMAN','HR','OE','EXFSYS','DBSNMP','MDSYS','OLAPSYS','SCOTT','EXFSYS','SH','PM','CTXSYS')) A,
                   DBA_CONSTRAINTS B
             WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
               AND B.CONSTRAINT_TYPE = 'R'
             GROUP BY B.OWNER,B.TABLE_NAME, 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, CNAME2, CNAME3, CNAME4, CNAME5,
                    CNAME6, CNAME7, CNAME8)
               AND I.COLUMN_POSITION = CONS.COL_CNT
             GROUP BY I.INDEX_NAME)
    /

    您可能感兴趣的文章:
    • 在Oracle数据库中添加外键约束的方法详解
    • Oracle外键不加索引引起死锁示例
    • Oracle系统表外键的更名
    • Oracle数据库中外键的相关操作整理
    上一篇:Oracle中查看表空间使用率的SQL脚本分享
    下一篇:Oracle中查看正在运行的SQL进程脚本分享
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

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