未索引的外键

发布于 2024-12-15 15:54:30 字数 81 浏览 0 评论 0原文

我正在寻找将显示与 gui 相同内容的命令行命令

Unindexed Foreign Keys 

I am looking for the command line command that will display the same thing as the gui

Unindexed Foreign Keys 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2024-12-22 15:54:30

没有命令行命令(据我所知)可以做到这一点。但你可以自己推出。基本上,您需要一个查询来检查数据库中的这些内容,这实际上是您的 GUI 工具必须做的事情。查询类似于:

SELECT  FK.table_name, FK.constraint_name
FROM    user_constraints FK
WHERE   FK.constraint_type = 'R'
AND     EXISTS
        (   SELECT  FC.position, FC.column_name
            FROM    user_cons_columns FC
            WHERE   FC.constraint_name = FK.constraint_name
            MINUS
            SELECT  IC.column_position AS position, IC.column_name
            FROM    user_ind_columns IC
            WHERE   IC.table_name = FK.table_name
        )

注意:此 SQL 并不完美。在某些情况下,它可能会被愚弄,认为存在指数下注,但实际上并非如此。列位于正确位置的多个不同索引可能会欺骗它。要正确执行此操作,您需要在内联视图中开始分组或使用分析函数来确保所有索引列都来自同一索引。所以我把它保留在这个简单的版本中,它在大多数情况下都可以工作。

然后您可以在 sqlplus 中运行此 SQL,或者可以将其嵌入到可以从命令行轻松运行的 shell 脚本中。一个粗略的方法是:

#!/bin/bash -ue

LOGIN="$1"
sqlplus -s << END_SQL
    $LOGIN
    SET PAGESIZE 5000
    SELECT  FK.table_name, FK.constraint_name
    FROM    user_constraints FK
    WHERE   FK.constraint_type = 'R'
    AND     EXISTS
            (   SELECT  FC.position, FC.column_name
                FROM    user_cons_columns FC
                WHERE   FC.constraint_name = FK.constraint_name
                MINUS
                SELECT  IC.column_position AS position, IC.column_name
                FROM    user_ind_columns IC
                WHERE   IC.table_name = FK.table_name
            )
/
END_SQL

然后您可以像这样运行并获得基本结果:

[user@centos5 sql]$ ./fk.sh scott/tiger@orcl

TABLE_NAME                     CONSTRAINT_NAME
------------------------------ ------------------------------
EMP                            FK_DEPTNO

There is no command line command (that I know of) which does it. But you can roll your own. Basically you need a query which checks the database for these, which is effectively what your GUI tool must be doing. The query would be something like:

SELECT  FK.table_name, FK.constraint_name
FROM    user_constraints FK
WHERE   FK.constraint_type = 'R'
AND     EXISTS
        (   SELECT  FC.position, FC.column_name
            FROM    user_cons_columns FC
            WHERE   FC.constraint_name = FK.constraint_name
            MINUS
            SELECT  IC.column_position AS position, IC.column_name
            FROM    user_ind_columns IC
            WHERE   IC.table_name = FK.table_name
        )

NOTE: This SQL is NOT perfect. There could be situations where it is fooled into thinking there is an index bet there's not really. Multiple different indexes with columns in the right place could fool it. To do it properly you'll need to start grouping in inline views or use analytic functions to ensure all the index columns come from the same index. So I left it at this simple version which will work most of the time.

Then you can run this SQL in sqlplus, or you could embed it in a shell script which is easily run from the command line. A crude one would be:

#!/bin/bash -ue

LOGIN="$1"
sqlplus -s << END_SQL
    $LOGIN
    SET PAGESIZE 5000
    SELECT  FK.table_name, FK.constraint_name
    FROM    user_constraints FK
    WHERE   FK.constraint_type = 'R'
    AND     EXISTS
            (   SELECT  FC.position, FC.column_name
                FROM    user_cons_columns FC
                WHERE   FC.constraint_name = FK.constraint_name
                MINUS
                SELECT  IC.column_position AS position, IC.column_name
                FROM    user_ind_columns IC
                WHERE   IC.table_name = FK.table_name
            )
/
END_SQL

Which you can then run like this and get the basic results:

[user@centos5 sql]$ ./fk.sh scott/tiger@orcl

TABLE_NAME                     CONSTRAINT_NAME
------------------------------ ------------------------------
EMP                            FK_DEPTNO
空气里的味道 2024-12-22 15:54:30

以下是一个每次都能正确运行的脚本,由 Steve Adams 提供:

-------------------------------------------------------------------------------
--
-- Script:  missing_fk_indexes.sql
-- Purpose: to check for locking problems with missing foriegn key indexes
-- For:     8.1 and higher
--
-- Copyright:   (c) Ixora Pty Ltd
-- Author:  Steve Adams
--
-------------------------------------------------------------------------------
@save_sqlplus_settings

column constraint_name noprint
column table_name format a48
break on constraint_name skip 1 on table_name

select /*+ ordered */
  n.name  constraint_name,
  u.name ||'.'|| o.name  table_name,
  c.name  column_name
from
  (
    select /*+ ordered */ distinct
      cd.con#,
      cd.obj#
    from
      sys.cdef$  cd,
      sys.tab$  t
    where
      cd.type# = 4 and          -- foriegn key
      t.obj# = cd.robj# and
      bitand(t.flags, 6) = 0 and    -- table locks enabled
      not exists (          -- not indexed
    select
      null
    from
      sys.ccol$  cc,
          sys.ind$  i,
      sys.icol$  ic
    where
          cc.con# = cd.con# and
          i.bo# = cc.obj# and
          bitand(i.flags, 1049) = 0 and     -- index must be valid
          ic.obj# = i.obj# and
      ic.intcol# = cc.intcol#
        group by
          i.obj#
        having
          sum(ic.pos#) = (cd.cols * cd.cols + cd.cols)/2
      )
  )  fk,
  sys.obj$  o,
  sys.user$  u,
  sys.ccol$  cc,
  sys.col$  c,
  sys.con$  n
where
  o.obj# = fk.obj# and
  o.owner# != 0 and         -- ignore SYS
  u.user# = o.owner# and
  cc.con# = fk.con# and
  c.obj# = cc.obj# and
  c.intcol# = cc.intcol# and
  n.con# = fk.con#
order by
  2, 1, 3
/

@restore_sqlplus_settings

希望有所帮助。

The following is a script which should work correctly every time, courtesy of Steve Adams:

-------------------------------------------------------------------------------
--
-- Script:  missing_fk_indexes.sql
-- Purpose: to check for locking problems with missing foriegn key indexes
-- For:     8.1 and higher
--
-- Copyright:   (c) Ixora Pty Ltd
-- Author:  Steve Adams
--
-------------------------------------------------------------------------------
@save_sqlplus_settings

column constraint_name noprint
column table_name format a48
break on constraint_name skip 1 on table_name

select /*+ ordered */
  n.name  constraint_name,
  u.name ||'.'|| o.name  table_name,
  c.name  column_name
from
  (
    select /*+ ordered */ distinct
      cd.con#,
      cd.obj#
    from
      sys.cdef$  cd,
      sys.tab$  t
    where
      cd.type# = 4 and          -- foriegn key
      t.obj# = cd.robj# and
      bitand(t.flags, 6) = 0 and    -- table locks enabled
      not exists (          -- not indexed
    select
      null
    from
      sys.ccol$  cc,
          sys.ind$  i,
      sys.icol$  ic
    where
          cc.con# = cd.con# and
          i.bo# = cc.obj# and
          bitand(i.flags, 1049) = 0 and     -- index must be valid
          ic.obj# = i.obj# and
      ic.intcol# = cc.intcol#
        group by
          i.obj#
        having
          sum(ic.pos#) = (cd.cols * cd.cols + cd.cols)/2
      )
  )  fk,
  sys.obj$  o,
  sys.user$  u,
  sys.ccol$  cc,
  sys.col$  c,
  sys.con$  n
where
  o.obj# = fk.obj# and
  o.owner# != 0 and         -- ignore SYS
  u.user# = o.owner# and
  cc.con# = fk.con# and
  c.obj# = cc.obj# and
  c.intcol# = cc.intcol# and
  n.con# = fk.con#
order by
  2, 1, 3
/

@restore_sqlplus_settings

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文