In this article, we will see how to add comments to columns while CREATING a table and using the ALTER statement. Comments are used to provide some additional information that helps understand the column’s usage and build an overall understanding of the table usage.
Table of Contents:-
- Add comments to MySQL columns while table creation
- Add comments to MySQL columns using ALTER statement
- How to view column comments in MySQL
Add comments to MySQL columns while table creation
This section will create a table employee_designation and add COMMENT to one of the columns named emp_name.
CREATE TABLE employee_designation( id int NOT NULL AUTO_INCREMENT, emp_id int DEFAULT NULL, emp_name varchar(255) DEFAULT NULL COMMENT "name of employee", emp_designation varchar(255) DEFAULT NULL, primary key(id) );
The comments can be added to the MySQL columns while creating a table by adding the COMMENT keyword after the column definition, as shown above for column emp_name.
The table is created successfully with comments. Confirm the same in column information using MySQL workbench.
Add comments to MySQL columns using ALTER statement
This section will modify the table employee_designation and add COMMENT to one of the columns named emp_id.
ALTER TABLE employee_designation MODIFY COLUMN emp_id INT COMMENT "identification number of the employee";
The comments can be added to the MySQL columns using the ALTER statement and modifying the column.
We can also change the comments of a MySQL column using the same ALTER statement.
Modify the Comments of MySQL Column:-
ALTER TABLE employee_designation MODIFY COLUMN emp_name VARCHAR(255) COMMENT "full name of employee";
How to view column comments in MySQL
In this section, we will view the comments of columns added in the above sections.
We can view comments of any MySQL column using two ways. One uses SHOW FULL COLUMNS query, and another is using MySQL Workbench.
Use MySQL query:-
SHOW FULL COLUMNS FROM employee_designation ;
Use MySQL Workbench:-
Go to the SCHEMAS section and press the information icon.
Click on the COLUMNS tab.
- Find all tables with specific column names in MySQL
- MySQL add primary key multiple columns
- Mysql update set multiple columns
- Mysql update column with value from another table
- Select all columns except one of a MySQL table
- MySQL ADD COLUMN IF NOT EXISTS
We hope this article helped you with adding comments to columns in MySQL. Good Luck!!!
Thanks for reading.