Add comment to column in MySQL using Python
Last Updated :
05 Dec, 2022
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
Similar Reads
How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module.
3 min read
How to Add a Column to a MySQL Table in Python? Prerequisite: Python: MySQL Create Table 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. Â ALTER statemen
4 min read
Adding new enum column to an existing MySQL table using Python Prerequisite: Python: MySQL Create Table In this article, we are going to see how to add a new enum column to an existing MySQL 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.
1 min read
Connect to MySQL using PyMySQL in Python In this article, we will discuss how to connect to the MySQL database remotely or locally using Python. In below process, we will use PyMySQL module of Python to connect our database. What is PyMySQL? This package contains a pure-Python MySQL client library, based on PEP 249. Requirements : MySQL Se
2 min read
How to Concatenate Column Values of a MySQL Table Using Python? Prerequisite: Python: MySQL Create Table In this article, we show how to concatenate column values of a MySQL table using Python. We use various data types in SQL Server to define data in a particular column appropriately. We might have requirements to concatenate data from multiple columns into a s
2 min read
Rename column name in MySQL using Python 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
1 min read