How to add comment to column in MySQL using Python?

A comment is a readable explanation provided about something. The comments in the code are provided to explain the purpose of the code staement used. This enables the outside person to understand the need and use of that particular code.

The comments are ignored by the compilers and are not executed. These are just for the explanatory purpose and are of great importance.

We can add comments to columns in a sql table. These comments will explain the function or tell something about the column. If some other person is viewing and working on that table, he can get idea about the columns are all about.

The comments can be added in the columns using the ALTER command. The ALTER statement along with the MODIFY command and COMMENT clause is used to add comments to a column.


ALTER TABLE table_name
MODIFY column_name column_definition COMMENT “your_comment”

Here, the table_name specifies the name of the table. The column_name and column_definition specify the name and datatype of the column. The your_comment is the comment you wish to add to that particular column.

Steps to add comment to acolumn in a table using MySQL in python

  • import MySQL connector

  • establish connection with the connector using connect()

  • create the cursor object using cursor() method

  • create a query using the appropriate mysql statements

  • execute the SQL query using execute() method

  • close the connection


Suppose we have a table named “Students”. We have a column named “addr” of type VARCHAR(50). We need to add a comment to this column to specify that this is the address column.

import mysql.connector
db=mysql.connector.connect(host="your host", user="your username", password="your


query="ALTER TABLE Students MODIFY Address VARCHAR(100) COMMENT "This is the column to store address.""

print("COMMENT ADDED..")


The above code adds a comment to the column “Address” the table.

This can be verified using “SHOW FULL COLUMNS FROM table_name” command. This displays the column definitions along with the comments, if any.