Course
Rename Columns
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
Rename Columns
The ALTER TABLE statement in MySQL can be used to change the structure of a table. For instance, we can add, delete, or rename the columns, etc. using this statement.
Following are the two options that can be used with the ALTER TABLE statement to rename a column(s) of a table:
- RENAME COLUMN
- CHANGE COLUMN
Note: Renaming a column(s) of table requires ALTER and DROP privilages.
Using the RENAME COLUMN
In MySQL, we can change the name of one or multiple columns of a specified table using the ALTER TABLE RENAME COLUMN command.
Syntax
Following is the syntax to rename a column in MySQL table
ALTER TABLE table_nameRENAME COLUMN old_column1_name TO new_column1_name,RENAME COLUMN old_column2_name TO new_column2_name,...;
Example
First of all, let us create a table named CUSTOMERS using the query below
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL);
Here, we are using the DESCRIBE command to display the information about the above created table structure
DESCRIBE CUSTOMERS;
As we can see in the table below, we have three columns present in CUSTOMERS table
Now, we are renaming the column named ID to cust_id using following query
ALTER TABLE CUSTOMERS RENAME COLUMN ID TO cust_id;
Output
Executing the query above will produce the following output
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0
Verification
Let us retrive the CUSTOMERS table description to verify whether the column ID is renamed to stud_id or not
DESCRIBE CUSTOMERS;
As we observe in the output table, the ID column is renamed to stud_id successfully.
Example
Now, we are renaming the other two columns in CUSTOMERS table named NAME and AGE to cust_name and cust_age
ALTER TABLE CUSTOMERS RENAME COLUMN NAME TO cust_name, RENAME COLUMN AGE TO cust_age;
Output
Executing the query above will produce the following output
Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0
Verification
To verify whether column names have been renamed or not, execute the following query
DESCRIBE CUSTOMERS;
As we observe in the output table, the above mentioned columns are successfully renamed.
Using CHANGE COLUMN
In MySQL, we can change the name of one or more columns along with their datatypes using the ALTER TABLE ... CHANGE COLUMN command.
Syntax
Following is the syntax of the ALTER TABLE ... CHANGE commnad in MySQL
ALTER TABLE table_nameCHANGE COLUMN old_column_name new_column_name Data Type;
Example
Consider the previously updated CUSTOMERS table and, let us change the name and the datatype of cust_id column
ALTER TABLE CUSTOMERS CHANGE COLUMN cust_id ID varchar(10);
Output
Executing the query above will produce the following output
Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0
Verification
Using the following query, we can verify whether the column cust_id has changed its name and datatype or not
DESCRIBE CUSTOMERS;
The name of the column and datatype has been changed successfully.
Example
Here, we are changing the names and datatypes of multiple columns (cust_name and cust_age) in the CUSTOMERS table
ALTER TABLE CUSTOMERS CHANGE COLUMN cust_name NAME DECIMAL(18,2), CHANGE COLUMN cust_age AGE VARCHAR(20);
Output
Executing the query above will produce the following output
Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0
Verification
Let us retrive the CUSTOMERS table description to verify whether the columns name and datatype are changed or not
DESCRIBE STUDENTS;
As we observe in the output table, the names and datatypes of above mentioned columns are successfully changed.
Renaming a Column of a Table Using a Client Program
In addition to rename a column of a table in MySQL Database using MySQL query, we can also perform the ALTER TABLE operation on a table using a client program.
Syntax
Following are the syntaxes to rename a column of a MySQL table in various programming languages
PHPNodeJSJavaPython
To rename a column of a table in MySQL database through a PHP program, we need to execute ALTER TABLE statement using the mysqli function query() as
$sql = "ALTER TABLE table_nameRENAME COLUMN old_column1_name TO new_column1_name"; $mysqli->query($sql);
Example
Following are the programs
PHPNodeJSJavaPython
$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$dbname = 'TUTORIALS';$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) { printf("Connect failed: %s", $mysqli->connect_error); exit();}// printf('Connected successfully.');
// rename column;$sql = "ALTER TABLE tut_tbl RENAME COLUMN tutorial_id TO tutorial_IDs";if ($mysqli->query($sql)) { printf("Column renamed successfully!.");}if ($mysqli->errno) { printf("Columns could be renamed!.", $mysqli->error);} $mysqli->close();
Output
The output obtained is as follows
Column renamed successfully!.