- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 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.
- How to Add Video to your Facebook Comment?
- How to add column using alter in MySQL?
- How to add a column to a MySQL table in Python?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to add column values in MySQL without using aggregate function?
- Add to existing value in MySQL column using CONCAT function?
- How do we add a comment in HTML code?
- How to add a NOT NULL column in MySQL?
- How to add a column using MySQL SELECT in an already created table?
- How to add a column in a table in MySQL?
- How to add not null constraint to existing column in MySQL?
- Add leading zeros to a MySQL column?
- How to add subtotal to a table column displaying NULL in MySQL?
- How to add column and index in a single MySQL query?
- MySQL add “prefix” to every column?