
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Rename all tables and columns to lower case in MySQL?
You can achieve this with the help of INFORMATION_SCHEMA.COLUMNS. The syntax is as follows −
SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CHANGE `', COLUMN_NAME, '` `', LOWER(COLUMN_NAME), '` ', COLUMN_TYPE, ';') AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ‘yourDatabaseName’;
Now use the database which has two tables. The database name is as follows “bothinnodbandmyisam”. This database is having the following tables −
- employee
- student
The description of the employee table is as follows −
mysql> desc employee;
The following is the output. Let’s say we have the following columns in the employee table which are not in lowercase −
+--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | EmployeeId | int(11) | YES | | NULL | | | EmployeeName | varchar(30) | YES | | NULL | | +--------------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
The description of the student table is as follows. The query is as follows −
mysql> desc student;
The following is the output. Let’s say we have the following columns in the student table which are not in lowercase −
+-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | YES | | NULL | | | StudentName | varchar(20) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
Here is the query to change column names of all tables to lowercase. The query is as follows −
mysql> SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CHANGE `', COLUMN_NAME, '` `', -> LOWER(COLUMN_NAME), '` ', COLUMN_TYPE, ';') AS changeColumnNameToLower -> FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'bothinnodbandmyisam';
The following is the output displaying the ALTER TABLE command that shows the updated column names −
+------------------------------------------------------------------------+ | changeColumnNameToLower | +------------------------------------------------------------------------+ | ALTER TABLE employee CHANGE `EmployeeId` `employeeid` int(11); | | ALTER TABLE employee CHANGE `EmployeeName` `employeename` varchar(30); | | ALTER TABLE student CHANGE `StudentId` `studentid` int(11); | | ALTER TABLE student CHANGE `StudentName` `studentname` varchar(20); | +------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
Look at the above sample output, all column names have been changed in lowercase.
- Related Articles
- MySQL Query to change lower case to upper case?
- How to find all tables that contains two specific columns in MySQL?
- Make all column names lower case in MySQL with a single query
- Lower case column names with MySQL SELECT?
- How to auto-increment value of tables to lower value in MySQL?
- Concatenate columns from different tables in MySQL
- Convert mixed case string to lower case in JavaScript
- Are quotes around tables and columns in a MySQL query really necessary?
- How to concatenate all columns in MySQL?
- How to find all tables that contains columnA and columnB in MySQL?
- How to convert Lower case to Upper Case using C#?
- How to convert Upper case to Lower Case using C#?
- Java String to Lower Case example.
- How can I sum columns across multiple tables in MySQL?
- How to display all the MySQL tables in one line?
