问题
I am looking for the command line command that will display the same thing as the gui
Unindexed Foreign Keys
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/8102264/unindexed-foreign-keys