Simulate the showindexfromtable command of MySQL in Oracle
Simulate the show index from table command of MySQL in Oracle
Experiment data initialization:
-
Create table t as select * from hr. employees
;
- Create index inx_t1 on t (employee_id, first_name desc, last_name );
- Create index inx_t2 on t (job_id, hire_date );
Displays information about all indexes of the table.
Log on as a dba
-
Set linesize 300
;
- Set pagesize 100;
- Col c1 format a20;
- Col c2 format a20;
- Col c3 format a20;
- Col c4 format a20;
- Col c5 format a20;
- Col INDEX_NAME format a20;
- Select INDEX_NAME,
- Max (decode (COLUMN_POSITION, 1, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c1,
- Max (decode (COLUMN_POSITION, 2, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c2,
- Max (decode (COLUMN_POSITION, 3, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c3,
- Max (decode (COLUMN_POSITION, 4, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c4,
- Max (decode (COLUMN_POSITION, 5, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c5
- From (
- Select INDEX_NAME, COLUMN_NAME, COLUMN_LENGTH, COLUMN_POSITION, DESCEND
- From dba_ind_columns
- Where table_owner = 'lihuilin'
- AND table_name ='t'
- Order by INDEX_NAME, column_position
- ) Group by INDEX_NAME;
Log on as a common user
-
Set linesize 300
;
- Set pagesize 100;
- Col c1 format a20;
- Col c2 format a20;
- Col c3 format a20;
- Col c4 format a20;
- Col c5 format a20;
- Col INDEX_NAME format a20;
- Select INDEX_NAME,
- Max (decode (COLUMN_POSITION, 1, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c1,
- Max (decode (COLUMN_POSITION, 2, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c2,
- Max (decode (COLUMN_POSITION, 3, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c3,
- Max (decode (COLUMN_POSITION, 4, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c4,
- Max (decode (COLUMN_POSITION, 5, COLUMN_NAME | ',' | COLUMN_LENGTH | ',' | DESCEND, null) c5
- From (
- Select INDEX_NAME, COLUMN_NAME, COLUMN_LENGTH, COLUMN_POSITION, DESCEND
- From user_ind_columns
- Where table_name ='t'
- Order by INDEX_NAME, column_position
- ) Group by INDEX_NAME;
-
Create or replace function long_2_varchar
(
- P_index_name IN user_ind_expressions. index_name % TYPE,
- P_table_name IN user_ind_expressions. table_name % TYPE,
- P_COLUMN_POSITION IN user_ind_expressions. table_name % TYPE)
- RETURN VARCHAR2
- AS
- Rochelle column_expression LONG;
- BEGIN
- SELECT COLUMN_EXPRESSION
- INTO l_COLUMN_EXPRESSION
- FROM user_ind_expressions
- WHERE index_name = p_index_name
- AND table_name = p_table_name
- AND COLUMN_POSITION = p_COLUMN_POSITION;
- Return substr (maid expression, 1, 4000 );
- END;
- /
-
Set linesize 300
;
- Set pagesize 100;
- Col c1 format a20;
- Col c2 format a20;
- Col c3 format a20;
- Col c4 format a20;
- Col c5 format a20;
- Col INDEX_NAME format a20;
- SELECT INDEX_NAME,
- MAX (DECODE (COLUMN_POSITION, 1, COLUMN_NAME | ''| DESCEND, NULL ))
- C1,
- MAX (DECODE (COLUMN_POSITION, 2, COLUMN_NAME | ''| DESCEND, NULL ))
- C2,
- MAX (DECODE (COLUMN_POSITION, 3, COLUMN_NAME | ''| DESCEND, NULL ))
- C3,
- MAX (DECODE (COLUMN_POSITION, 4, COLUMN_NAME | ''| DESCEND, NULL ))
- C4,
- MAX (DECODE (COLUMN_POSITION, 5, COLUMN_NAME | ''| DESCEND, NULL ))
- C5
- FROM (SELECT a. INDEX_NAME,
- REPLACE (
- DECODE (
- Descend,
- 'Desc', long_2_varchar (B. index_name,
- B. table_NAME,
- B. COLUMN_POSITION ),
- A. column_name ),
- '"',
- '')
- COLUMN_NAME,
- A. COLUMN_LENGTH,
- A. COLUMN_POSITION,
- DESCEND
- FROM user_ind_columns
- LEFT JOIN
- User_ind_expressions B
- ON a. index_name = B. index_name
- AND a. table_name = B. table_name
- WHERE a. table_name ='t'
- Order by INDEX_NAME, column_position)
- Group by INDEX_NAME;