数据库
首页 > 数据库> > 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;

 

标签:INDEX,NAME,导出,ASC,索引,mysql,TABLE,TYPE,CONCAT
来源: https://www.cnblogs.com/zkx4213/p/16184511.html