COMMENT ON

On this page Carat arrow pointing down
Warning:
CockroachDB v19.2 is no longer supported as of May 12, 2021. For more details, refer to the Release Support Policy.

The COMMENT ON statement associates comments to databases, tables, or columns.

Required privileges

The user must have the CREATE privilege on the object they are commenting on.

Synopsis

COMMENT ON DATABASE database_name TABLE table_name COLUMN column_name IS comment_text

Parameters

Parameter Description
database_name The name of the database you are commenting on.
table_name The name of the table you are commenting on.
column_name The name of the column you are commenting on.
comment_text The comment (STRING) you are associating to the object.

Examples

Setup

The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see MovR: A Global Vehicle-sharing App.

To follow along, run cockroach demo to start a temporary, in-memory cluster with the movr dataset preloaded:

icon/buttons/copy
$ cockroach demo

Add a comment to a database

To add a comment to a database:

icon/buttons/copy
> COMMENT ON DATABASE movr IS 'This database contains information about users, vehicles, and rides.';

To view database comments, use SHOW DATABASES:

icon/buttons/copy
> SHOW DATABASES WITH COMMENT;
  database_name |                              comment
+---------------+-------------------------------------------------------------------+
  defaultdb     | NULL
  movr          | This database contains information about users, vehicles, and rides.
  postgres      | NULL
  system        | NULL
(4 rows)

Add a comment to a table

To add a comment to a table:

icon/buttons/copy
> COMMENT ON TABLE vehicles IS 'This table contains information about vehicles registered with MovR.';

To view table comments, use SHOW TABLES:

icon/buttons/copy
> SHOW TABLES FROM movr WITH COMMENT;
          table_name         |                               comment
+----------------------------+----------------------------------------------------------------------+
  users                      |
  vehicles                   | This table contains information about vehicles registered with MovR.
  rides                      |
  vehicle_location_histories |
  promo_codes                |
  user_promo_codes           |
(6 rows)

Add a comment to a column

To add a comment to a column:

icon/buttons/copy
> COMMENT ON COLUMN users.credit_card IS 'This column contains user payment information.';

To view column comments, use SHOW COLUMNS:

icon/buttons/copy
> SHOW COLUMNS FROM users WITH COMMENT;
  column_name | data_type | is_nullable | column_default | generation_expression |  indices  | is_hidden |                    comment
+-------------+-----------+-------------+----------------+-----------------------+-----------+-----------+------------------------------------------------+
  id          | UUID      |    false    | NULL           |                       | {primary} |   false   | NULL
  city        | VARCHAR   |    false    | NULL           |                       | {primary} |   false   | NULL
  name        | VARCHAR   |    true     | NULL           |                       | {}        |   false   | NULL
  address     | VARCHAR   |    true     | NULL           |                       | {}        |   false   | NULL
  credit_card | VARCHAR   |    true     | NULL           |                       | {}        |   false   | This column contains user payment information.
(5 rows)

See also


Yes No
On this page

Yes No