Add comment to column in MySQL using Python - GeeksforGeeks

11 Dec, 2020
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 geeksfoegeeks. Look at the below image.
Database=test and Table=geeksfoegeeks
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 connectionconn = pymysql.connect(host="localhost", user="root",                       password="1234",db="test")  # create cursor objectmycursor = conn.cursor()  # execute querymycursor.execute("ALTER TABLE geeksforgeeks MODIFY name \                 CHAR(50) COMMENT 'ENTER NAMES HERE'")  # display comments of all columnsmycursor.execute("SHOW FULL COLUMNS FROM GEEKSFOEGEEKS")result = mycursor.fetchall()for i in result:    print(i)mycursor.execute("COMMIT")  #terminate connectionconn.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 connectionconn = pymysql.connect(host="localhost", user="root",                       password="1234",db="test")  # create cursor objectmycursor = conn.cursor()  # execute querymycursor.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 attributesmycursor.execute("SHOW FULL COLUMNS FROM GEEKSFOEGEEKS")result = mycursor.fetchall()for i in result:    print(i)mycursor.execute("COMMIT")  # terminate connectionconn.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
Python-mySQL
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Pandas dataframe.groupby()
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list