How do I check if an index exists on a table field in MySQL?
I've needed to Google this a couple times, so I'm sharing my Q/A.
Solution 1:
Use SHOW INDEX
like so:
SHOW INDEX FROM [tablename]
Docs: https://dev.mysql.com/doc/refman/5.0/en/show-index.html
Solution 2:
Try:
SELECT * FROM information_schema.statistics
WHERE table_schema = [DATABASE NAME]
AND table_name = [TABLE NAME] AND column_name = [COLUMN NAME]
It will tell you if there is an index of any kind on a certain column without the need to know the name given to the index. It will also work in a stored procedure (as opposed to show index)
Solution 3:
SHOW KEYS FROM tablename WHERE Key_name='unique key name'
you can find if there exists an unique key in the table
Solution 4:
show index from table_name where Column_name='column_name';