Open In App

Add comment to column in MySQL using Python

Last Updated : 05 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server we use various modules in Python such as PyMySQL, mysql.connector, etc.

A comment is a readable explanation or a statement placed in the SQL queries. It is used for the purpose of making the SQL statements easier for humans to understand. MySQL generally ignores them during the parsing of the SQL code. Comments can be written in a single line or multiple lines. 

In this article, we’re using a database called test and which has a table called geeksforgeeks. Look at the below image.

Database=test and Table=geeksforgeeks

To check if there is any comment for any column, see below statement.

SHOW FULL COLUMNS FROM table_name;

Below is the image which shows that no column is associated with any kind of comment.

No Comment for any columns

The statement to add any comment to a column is given below:

ALTER TABLE table_name MODIFY column_name type_of_that_column COMMENT ‘enter comment here’;

Below are some examples which depict how to add a comment to a column in a MySQL table using Python:

Example 1

Adding Single line comment.

Python3




# import required module
import pymysql
 
# make connection
conn = pymysql.connect(host="localhost", user="root",
                       password="1234",db="test")
 
# create cursor object
mycursor = conn.cursor()
 
# execute query
mycursor.execute("ALTER TABLE geeksforgeeks MODIFY name \
                 CHAR(50) COMMENT 'ENTER NAMES HERE'")
 
# display comments of all columns
mycursor.execute("SHOW FULL COLUMNS FROM GEEKSFORGEEKS")
result = mycursor.fetchall()
for i in result:
    print(i)
mycursor.execute("COMMIT")
 
#terminate connection
conn.close()


Output:

After adding comments

We could see that the comment “ENTER NAMES HERE” for column “Name” has been added.

Example 2 

Adding multi-line comments.

Python3




# import required module
import pymysql
 
# establish connection connection
conn = pymysql.connect(host="localhost", user="root",
                       password="1234",db="test")
 
# create cursor object
mycursor = conn.cursor()
 
# execute query
mycursor.execute("ALTER TABLE geeksforgeeks MODIFY NAME CHAR(50) \
                 COMMENT 'ENTER NAMES HERE',MODIFY ADDRESS CHAR(50) \
                 COMMENT 'Do not Enter Address\nmore than 50 \
                 characters',MODIFY AGE INT COMMENT \
                 'Enter Age here',MODIFY MOB_NUMBER INT COMMENT \
                 'Mobile number should be of 10 digits', \
                 MODIFY ID_NO VARCHAR(50) COMMENT 'Id Number \
                 is Unique\nKindly Enter different Ids'")
 
# display comments of all attributes
mycursor.execute("SHOW FULL COLUMNS FROM GEEKSFORGEEKS")
result = mycursor.fetchall()
for i in result:
    print(i)
mycursor.execute("COMMIT")
 
# terminate connection
conn.close()


Output:

After adding Comments

We could see that comments have been added successfully. Below is a screenshot of the MySQL database is also added.

MySQL Database



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads