在 PostgreSQL 中创建表时向列添加注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32070876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding comment to column when I create table in PostgreSQL?
提问by user3600910
How can I add comment to column in PostgreSQL?
如何为 PostgreSQL 中的列添加注释?
create table session_log (
UserId int index not null,
PhoneNumber int index);
回答by a_horse_with_no_name
Comments are attached to a column using the comment
statement:
使用以下comment
语句将注释附加到列:
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';
You can also add a comment to the table:
您还可以向表中添加注释:
comment on table session_log is 'Our session logs';
Additionally: int index
is invalid.
另外:int index
无效。
If you want to create an index on a column, you do that using the create index
statement:
如果要在列上创建索引,请使用以下create index
语句:
create index on session_log(phonenumber);
If you want an index over both columns use:
如果您想在两列上建立索引,请使用:
create index on session_log(userid, phonenumber);
You probably want to define the userid as the primary key. This is done using the following syntax (and not using int index
):
您可能希望将 userid 定义为主键。这是使用以下语法(而不是使用int index
)完成的:
create table session_log
(
UserId int primary key,
PhoneNumber int
);
Defining a column as the primary key implicitly makes it not null
将列定义为主键隐式使其成为 not null