
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Syntax
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
Example
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 password",database="database_name") cursor=db.cursor() query="ALTER TABLE Students MODIFY Address VARCHAR(100) COMMENT "This is the column to store address."" cursor.execute(query) db.commit() print("COMMENT ADDED..") db.close()
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.
Output
COMMENT ADDED..
- Related Articles
- How to add column using alter in MySQL?\n
- How to add a column to a MySQL table in Python?
- How to add comment to chart in Excel?
- How to add hyperlink to comment in Excel?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to add column values in MySQL without using aggregate function?
- How to Add Video to your Facebook Comment?
- Add to existing value in MySQL column using CONCAT function?
- How to add a column using MySQL SELECT in an already created table?
- How to Auto Add Date and Time to Comment in Excel?
- How to add a NOT NULL column in MySQL?
- How to add a column in a table in MySQL?
- How to add not null constraint to existing column in MySQL?
- How to add comment to a data point in an Excel chart?
- MySQL add “prefix” to every column?
