I recently transferred a database from a windows box to a linux box. The tables are mixed between lower and upper case names. I need a way to rename all tables and columns t
OK i have modified Dave Benson's answer to add the DISTINCT keyword (to prevent duplicate records being returned) and also to check for nullable timestamp columns.
SELECT DISTINCT
CONCAT('ALTER TABLE ', '`', t.TABLE_NAME, '`', ' CHANGE `', c.COLUMN_NAME, '`', ' `', LOWER(`COLUMN_NAME`), '` ', COLUMN_TYPE,
IF (IS_NULLABLE = "YES", ' ', ' NOT NULL'), IF (IS_NULLABLE = "YES", IF (COLUMN_DEFAULT IS NULL, IF (COLUMN_TYPE = 'TIMESTAMP', 'NULL DEFAULT NULL', 'DEFAULT NULL'), IF (COLUMN_DEFAULT = 'CURRENT_TIMESTAMP', ' DEFAULT CURRENT_TIMESTAMP', CONCAT(' DEFAULT ', '''', COLUMN_DEFAULT, ''''))), IF (COLUMN_DEFAULT IS NOT NULL,IF (COLUMN_DEFAULT = 'CURRENT_TIMESTAMP', ' DEFAULT CURRENT_TIMESTAMP', CONCAT(' DEFAULT ', '''', COLUMN_DEFAULT, '''')),'')), ' ', EXTRA, ';')
FROM information_schema.tables t
JOIN information_schema.columns c ON (c.table_name = t.table_name)
WHERE t.table_type = 'BASE TABLE'
AND c.table_schema not in ('information_schema', 'mysql')
INTO OUT FILE '/tmp/ColumnsToLowerCase.sql'