Comment

View as Markdown

Adds a comment or removes an existing comment for an existing table or column object.

COMMENT

1COMMENT ON (TABLE | COLUMN) <object_name> IS (<string_literal> | NULL);

Create or remove a comment for a TABLE or COLUMN object of name object_name. The comment must be a string literal or NULL. If NULL, the comment is removed.

Only super-users or owners of the object can modify comments on the object.

Column and table comments can be viewed either in the information_schema system tables, or in the result of the SHOW CREATE TABLE command run on the relevant table.

Currently comments are not supported with the CREATE TABLE command, and COMMENT ON is the canonical means to set or unset comments.

Examples

  1. Create a table and add a comments to it.
1CREATE TABLE employees (id INT, salary BIGINT);
2-- Add a comment to the 'employees' table
3COMMENT ON TABLE employees IS 'This table stores employee information';
4-- Add a comment to the 'salary' column
5COMMENT ON COLUMN employees.salary IS 'Stores the salary of the employee';

When specifying the name of the COLUMN object, it must be of the form <TABLE>.COLUMN> to uniquely identify it.

  1. Show the comments and the DDL of the table.
1SHOW CREATE TABLE employees;
2
3CREATE TABLE employees /* This table stores employee information */ (
4 id INTEGER,
5 salary BIGINT /* Stores the salary of the employee */);
61 rows returned.

Currently COMMENT ON is supported only on tables and and columns of that table. Other objects such as VIEW are not currently supported.

  1. View the table and column comment in respective system table.
1-- Connect to information_schema database
2\c information_schema admin XXXXXXXX
3
4-- Select subset of columns from the tables system table
5SELECT table_id,table_name,"comment" FROM tables where table_name = 'employees';
6
7-- Returns one result for the table comment
8table_id|table_name|comment
95|employees|This table stores employee information
101 rows returned.
11
12-- Select subset of columns from the columns system table
13SELECT table_id,table_name,column_id,column_name,"comment" FROM columns where table_name = 'employees';
14
15-- Returns two results, one of the columns has no comment.
16table_id|table_name|column_id|column_name|comment
175|employees|1|id|NULL
185|employees|2|salary|Stores the salary of the employee
192 rows returned.