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

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.

Action Output:-

image_1

The table is created successfully with comments. Confirm the same in column information using MySQL workbench.

image_2

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.

Action Output:-

image_3

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";

Action Output:-

image_4

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 ;

Output:-

image_5

Use MySQL Workbench:-

Go to the SCHEMAS section and press the information icon.

image_6

Click on the COLUMNS tab.

image_7

READ MORE:

We hope this article helped you with adding comments to columns in MySQL. Good Luck!!!