【问题标题】:Adding comment to column when I create table in PostgreSQL?在 PostgreSQL 中创建表时向列添加注释?
【发布时间】:2015-11-11 07:11:15
【问题描述】:

如何在 PostgreSQL 中为列添加注释?

create table session_log (
                UserId int index not null,
                PhoneNumber int index); 

【问题讨论】:

    标签: postgresql comments ddl


    【解决方案1】:

    使用the comment statement 将评论附加到列:

    create table session_log 
    ( 
       userid int not null, 
       phonenumber int
    ); 
    
    comment on column session_log.userid is 'The user ID';
    comment on column session_log.phonenumber is 'The phone number including the area code';
    

    您还可以在表格中添加注释:

    comment on table session_log is 'Our session logs';
    

    另外:int index 无效。

    如果你想在列上创建索引,你可以这样做using the create index statement

    create index on session_log(phonenumber);
    

    如果您想对两列都使用索引:

    create index on session_log(userid, phonenumber);
    

    您可能希望将用户标识定义为主键。这是使用以下语法完成的(而不是使用int index):

    create table session_log 
    ( 
       UserId int primary key, 
       PhoneNumber int
    ); 
    

    将列定义为主键隐式使其成为not null

    【讨论】:

    • 似乎 PG 没有提供标准语法来评论 CREATE TABLE 子句...为什么不呢?
    • @PeterKrauss:CREATE TABLE 语句中的 cmets 没有标准(Postgres 使用与 Oracle 相同的语法)
    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2021-10-11
    • 1970-01-01
    • 2022-01-12
    • 2017-09-10
    • 2021-04-02
    相关资源
    最近更新 更多