SELECT 'CREATE INDEX '
|| owner
|| '.'
|| REPLACE (constraint_name, 'FK_', 'IX_')
|| ' ON '
|| owner
|| '.'
|| table_name
|| ' ('
|| col_list
|| ') TABLESPACE ' || :table_space ||';' indx
FROM (SELECT cc.owner, cc.table_name, cc.constraint_name,
MAX (DECODE (POSITION,
1, '"' || SUBSTR (column_name, 1, 30) || '"',
NULL
)
)
|| MAX (DECODE (POSITION,
2, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
3, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
4, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
5, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
6, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
7, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
8, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
9, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
10, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
) col_list
FROM dba_constraints dc, dba_cons_columns cc
WHERE dc.owner = cc.owner
AND dc.constraint_name = cc.constraint_name
AND dc.constraint_type = 'R'
AND UPPER (dc.owner) = UPPER (:owner)
GROUP BY cc.owner, cc.table_name, cc.CONSTRAINT_NAME
) con
WHERE NOT EXISTS (
SELECT 1
FROM (SELECT table_owner, table_name,
MAX (DECODE (column_position,
1, '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
2, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
3, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
4, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
5, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
6, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
7, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
8, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
9, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
10, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
) col_list
FROM dba_ind_columns
WHERE UPPER (table_owner) = UPPER (:owner)
GROUP BY table_owner, table_name, index_name) col
WHERE con.owner = col.table_owner
AND con.table_name = col.table_name
AND con.col_list =
SUBSTR (col.col_list, 1, LENGTH (con.col_list)));
|| owner
|| '.'
|| REPLACE (constraint_name, 'FK_', 'IX_')
|| ' ON '
|| owner
|| '.'
|| table_name
|| ' ('
|| col_list
|| ') TABLESPACE ' || :table_space ||';' indx
FROM (SELECT cc.owner, cc.table_name, cc.constraint_name,
MAX (DECODE (POSITION,
1, '"' || SUBSTR (column_name, 1, 30) || '"',
NULL
)
)
|| MAX (DECODE (POSITION,
2, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
3, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
4, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
5, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
6, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
7, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
8, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
9, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (POSITION,
10, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
) col_list
FROM dba_constraints dc, dba_cons_columns cc
WHERE dc.owner = cc.owner
AND dc.constraint_name = cc.constraint_name
AND dc.constraint_type = 'R'
AND UPPER (dc.owner) = UPPER (:owner)
GROUP BY cc.owner, cc.table_name, cc.CONSTRAINT_NAME
) con
WHERE NOT EXISTS (
SELECT 1
FROM (SELECT table_owner, table_name,
MAX (DECODE (column_position,
1, '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
2, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
3, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
4, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
5, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
6, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
7, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
8, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
9, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
)
|| MAX (DECODE (column_position,
10, ', '
|| '"'
|| SUBSTR (column_name, 1, 30)
|| '"',
NULL
)
) col_list
FROM dba_ind_columns
WHERE UPPER (table_owner) = UPPER (:owner)
GROUP BY table_owner, table_name, index_name) col
WHERE con.owner = col.table_owner
AND con.table_name = col.table_name
AND con.col_list =
SUBSTR (col.col_list, 1, LENGTH (con.col_list)));