在PostgreSQL中创建表时添加注释到列?

use*_*910 56 postgresql ddl comments

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

create table session_log (
                UserId int index not null,
                PhoneNumber int index); 
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 86

使用以下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';
Run Code Online (Sandbox Code Playgroud)

您还可以向表中添加注释:

comment on table session_log is 'Our session logs';
Run Code Online (Sandbox Code Playgroud)

另外:int index无效.

如果要在列上创建索引,可以使用以下create index语句执行此操作:

create index on session_log(phonenumber);
Run Code Online (Sandbox Code Playgroud)

如果要在两列上都使用索引,请使用:

create index on session_log(userid, phonenumber);
Run Code Online (Sandbox Code Playgroud)

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

create table session_log 
( 
   UserId int primary key, 
   PhoneNumber int
); 
Run Code Online (Sandbox Code Playgroud)

将列定义为主键隐式地创建它 not null

  • @PeterKrauss:CREATE TABLE 语句的注释没有标准(Postgres 使用与 Oracle 相同的语法) (6认同)
  • @a_horse_with_no_name,抱歉,建议**不是**放弃标准,只是在 CREATE 子句上添加**可选注释**...您同意选项会有所帮助吗? (3认同)
  • 似乎 PG 没有提供 CREATE TABLE 子句注释的标准语法...为什么不呢? (2认同)