Make all column names lower case in MySQL with a single query

MySQLMySQLi Database

Let us first create a −

mysql> create table DemoTable1
   -> (
   -> StudentFirstName varchar(20),
   -> StudentLastName varchar(20),
   -> StudentAge int,
   -> StudentCountryName varchar(20)
   -> );
Query OK, 0 rows affected (4.20 sec)

Let us now make all column names lower case in MySQL −

mysql> select concat('alter table ', table_name, ' change `', column_name, '` `',
   -> lower(column_name), '` ', column_type, ';')
   -> from information_schema.columns  where table_schema = 'demo';

This will produce the following output −

+-----------------------------------------------------------------------------------------------------------------+
| concat('alter table ', table_name, ' change `', column_name, '` `', lower(column_name), '` ', column_type, ';') |
+-----------------------------------------------------------------------------------------------------------------+
| alter table demotable1 change `StudentFirstName` `studentfirstname` varchar(20);                                |
| alter table demotable1 change `StudentLastName` `studentlastname` varchar(20);                                  |
| alter table demotable1 change `StudentAge` `studentage` int(11);                                                |
| alter table demotable1 change `StudentCountryName` `studentcountryname` varchar(20);                            |
| alter table mytable change `MyEuroColumn` `myeurocolumn` varchar(5);                                            |
+-----------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 10:20:19
Advertisements