Created
December 12, 2024 08:32
-
-
Save andreoss/d6f1b007cf14019d0087ab2dad28d5e6 to your computer and use it in GitHub Desktop.
MySQL. List all indices in the schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
CONCAT( | |
'ALTER TABLE ', | |
TABLE_NAME, | |
' ADD ', | |
CASE | |
WHEN MAX(NON_UNIQUE) = 1 THEN 'INDEX ' | |
ELSE 'UNIQUE INDEX ' | |
END, | |
INDEX_NAME, | |
' (', | |
GROUP_CONCAT( | |
COLUMN_NAME | |
ORDER BY | |
SEQ_IN_INDEX | |
), | |
');' | |
) AS alter_statement | |
FROM | |
information_schema.STATISTICS | |
WHERE | |
TABLE_SCHEMA = DATABASE() | |
GROUP BY | |
TABLE_NAME, | |
INDEX_NAME; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment