且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-01 09:00:04

使用 评论语句

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';

另外: int索引无效。

如果要在列上创建索引,请执行使用 create index 语句

If you want to create an index on a column, you do that using the create index statement:

create index on session_log(phonenumber);

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

If you want an index over both columns use:

create index on session_log(userid, phonenumber);

您可能想将userid定义为主键。这是使用以下语法(而不是使用 int索引)完成的:

You probably want to define the userid as the primary key. This is done using the following syntax (and not using int index):

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

将列定义为主键会隐式地使其变为不为空

Defining a column as the primary key implicitly makes it not null