Skip to content
Related Articles

Related Articles

Rename column name in MySQL using Python

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jan, 2021
View Discussion
Improve Article
Save Article

In this article, we are going to see how we can rename or alter the name of a column table using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. 


  • Import module.
  • Make a connection request with the database.
  • Create an object for the database cursor.
  • Execute the following MySQL query:
ALTER TABLE your_table_name RENAME COLUMN original_column_name TO new_column_name;


In this example we are using this database table with the following query;

Below is the implementation:


# Establish connection to MySQL database
import mysql.connector
db = mysql.connector.connect(
  database = "geeks"
#getting the cursor by cursor() method
mycursor = db.cursor()
query = "ALTER TABLE persons RENAME COLUMN PersonID TO Emp_Id;"
# close the Connection

After running this script, let’s check our tables:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!