SQL script sharing in Oracle to check whether the foreign key is indexed _oracle

Source: Internet
Author: User

Copy Code code as follows:

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) TABLE_NAME,
SUBSTR (constraint_name, 1) constraint_name,
SUBSTR (column_name, 1) 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)
/

On the basis of the above modified a bit, you can check all the users.
Copy Code code as follows:

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) TABLE_NAME,
SUBSTR (constraint_name, 1) constraint_name,
SUBSTR (column_name, 1) column_name,
POSITION
From Dba_cons_columns WHERE OWNER is 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)
/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.