数据库查看全部索引

Posted on 2021-03-10 15:18  橙子j  阅读(699)  评论(0编辑  收藏  举报

postgresql

SELECT
    tablename,
    indexname,
    indexdef
FROM
    pg_indexes
WHERE
    tablename = 'user_tbl'
ORDER BY
    tablename,
    indexname;

 

Oracle

SELECT
    user_ind_columns.index_name 索引名称,
    user_ind_columns.table_name 表名,
    user_ind_columns.column_name 字段名,
    user_indexes.uniqueness 约束 
FROM
    user_ind_columns,
    user_indexes 
WHERE
    user_ind_columns.index_name = user_indexes.index_name;

 

MySQL

mysql的没测试,不一定对

SELECT
CONCAT('ALTER TABLE `',TABLE_NAME,'` ', 'ADD ',
 IF(NON_UNIQUE = 1,
 CASE UPPER(INDEX_TYPE)
 WHEN 'FULLTEXT' THEN 'FULLTEXT INDEX'
 WHEN 'SPATIAL' THEN 'SPATIAL INDEX'
 ELSE CONCAT('INDEX `',
  INDEX_NAME,
  '` USING ',
  INDEX_TYPE
 )
END,
IF(UPPER(INDEX_NAME) = 'PRIMARY',
 CONCAT('PRIMARY KEY USING ',
 INDEX_TYPE
 ),
CONCAT('UNIQUE INDEX `',
 INDEX_NAME,
 '` USING ',
 INDEX_TYPE
)
)
),'(', GROUP_CONCAT(DISTINCT CONCAT('`', COLUMN_NAME, '`') ORDER BY SEQ_IN_INDEX ASC SEPARATOR ', '), ');') AS 'Show_Add_Indexes'
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = '库名' and TABLE_NAME='表名'
GROUP BY TABLE_NAME, INDEX_NAME
ORDER BY TABLE_NAME ASC, INDEX_NAME ASC;

-- 说明:倒数第三行的where条件处 TABLE_SCHEMA 填写数据库名,TABLE_NAME 填写数据表名
-- 只需要改对应字段的值即可执行。

 

SELECTCONCAT('ALTER TABLE `',TABLE_NAME,'` ', 'ADD ', IF(NON_UNIQUE = 1, CASEUPPER(INDEX_TYPE) WHEN'FULLTEXT'THEN'FULLTEXT INDEX'WHEN'SPATIAL'THEN'SPATIAL INDEX'ELSECONCAT('INDEX `', INDEX_NAME, '` USING ', INDEX_TYPE ) END, IF(UPPER(INDEX_NAME) = 'PRIMARY', CONCAT('PRIMARY KEY USING ', INDEX_TYPE ), CONCAT('UNIQUE INDEX `', INDEX_NAME, '` USING ', INDEX_TYPE ) ) ),'(', GROUP_CONCAT(DISTINCTCONCAT('`', COLUMN_NAME, '`') ORDERBY SEQ_IN_INDEX ASC SEPARATOR ', '), ');') AS'Show_Add_Indexes'FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = '库名'and TABLE_NAME='表名'GROUPBY TABLE_NAME, INDEX_NAME ORDERBY TABLE_NAME ASC, INDEX_NAME ASC; -- 说明:倒数第三行的where条件处 TABLE_SCHEMA 填写数据库名,TABLE_NAME 填写数据表名-- 只需要改对应字段的值即可执行。