COMMENT¶
为现有对象添加注释或覆盖现有注释。
可以将注释添加到所有对象(用户、角色、仓库、数据库、表等)。您还可使用此命令为各个表列添加注释,但不能为列上的约束添加注释。
语法¶
COMMENT [ IF EXISTS ] ON <object_type> <object_name> IS '<string_literal>';
COMMENT [ IF EXISTS ] ON COLUMN <table_name>.<column_name> IS '<string_literal>';
参数¶
使用说明¶
除此命令之外,您还可以在创建或更改对象时添加或修改注释:
要添加注释,请在 CREATE <object> 或 ALTER <object> 命令中指定
COMMENT
参数。要修改现有注释,请在 ALTER <object> 命令中指定
COMMENT
参数。
在表列上添加或修改注释时使用的语法略有不同:
要在创建时添加注释,请在列声明后加上
COMMENT
关键字(而不是属性)。要修改注释,请使用此命令。
若要向约束添加注释,请使用 CREATE TABLE 或 CREATE | ALTER TABLE ... CONSTRAINT 命令。
DESCRIBE TABLE 输出不显示表约束的注释,例如多列主键。要查看这些注释,请查询 TABLE_CONSTRAINTS 视图。
关于元数据:
注意
客户应确保在使用 Snowflake 服务时,不会将个人数据(用户对象除外)、敏感数据、出口管制数据或其他受监管数据作为元数据输入。有关更多信息,请参阅 Snowflake 中的元数据字段。
示例¶
创建一个带有注释的架构,然后覆盖该注释:
CREATE SCHEMA my_schema COMMENT='this is comment1';
SHOW SCHEMAS LIKE 'my_schema';
+-------------------------------+-----------+------------+------------+---------------+---------+------------------+---------+----------------+------+
| created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | ... |
|-------------------------------+-----------+------------+------------+---------------+---------+------------------+---------+----------------+------|
| 2025-02-26 12:08:52.363 -0800 | MY_SCHEMA | N | Y | MY_DB | MY_ROLE | this is comment1 | | 1 | ... |
+-------------------------------+-----------+------------+------------+---------------+---------+------------------+---------+----------------+------+
COMMENT ON SCHEMA my_schema IS 'now comment2';
SHOW SCHEMAS LIKE 'my_schema';
+-------------------------------+-----------+------------+------------+---------------+---------+--------------+---------+----------------+-----+
| created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | ... |
|-------------------------------+-----------+------------+------------+---------------+---------+--------------+---------+----------------+-----+
| 2025-02-26 12:08:52.363 -0800 | MY_SCHEMA | N | Y | MY_DB | MY_ROLE | now comment2 | | 1 | ... |
+-------------------------------+-----------+------------+------------+---------------+---------+--------------+---------+----------------+-----+
创建一个带有表列注释的表,然后覆盖该注释:
CREATE OR REPLACE TABLE test_comment_table_column(my_column STRING COMMENT 'this is comment3');
DESC TABLE test_comment_table_column;
+-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+------------------+-------------+----------------+
| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain |
|-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+------------------+-------------+----------------|
| MY_COLUMN | VARCHAR(16777216) | COLUMN | Y | NULL | N | N | NULL | NULL | this is comment3 | NULL | NULL |
+-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+------------------+-------------+----------------+
COMMENT ON COLUMN test_comment_table_column.my_column IS 'now comment4';
DESC TABLE test_comment_table_column;
+-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+--------------+-------------+----------------+
| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain |
|-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+--------------+-------------+----------------|
| MY_COLUMN | VARCHAR(16777216) | COLUMN | Y | NULL | N | N | NULL | NULL | now comment4 | NULL | NULL |
+-----------+-------------------+--------+-------+---------+-------------+------------+-------+------------+--------------+-------------+----------------+
创建带有注释的视图,然后覆盖该注释:
CREATE OR REPLACE VIEW test_comment_view COMMENT='this is comment5' AS (SELECT * FROM test_comment_table_column);
SHOW VIEWS LIKE 'test_comment_view';
+-------------------------------+-------------------+----------+---------------+-------------+---------+------------------+-----+
| created_on | name | reserved | database_name | schema_name | owner | comment | ... |
|-------------------------------+-------------------+----------+---------------+-------------+---------+------------------+-----+
| 2025-02-26 12:38:35.440 -0800 | TEST_COMMENT_VIEW | | MY_DB | MY_SCHEMA | MY_ROLE | this is comment5 | ... |
+-------------------------------+-------------------+----------+---------------+-------------+---------+------------------+-----+
COMMENT ON VIEW test_comment_view IS 'now comment6';
SHOW VIEWS LIKE 'test_comment_view';
+-------------------------------+-------------------+----------+---------------+-------------+---------+--------------+-----+
| created_on | name | reserved | database_name | schema_name | owner | comment | ... |
|-------------------------------+-------------------+----------+---------------+-------------+---------+--------------+-----+
| 2025-02-26 12:38:35.440 -0800 | TEST_COMMENT_VIEW | | MY_DB | MY_SCHEMA | MY_ROLE | now comment6 | ... |
+-------------------------------+-------------------+----------+---------------+-------------+---------+--------------+-----+