Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding comment to column when I create table in PostgreSQL?

How can I add comment to column in PostgreSQL?

create table session_log (                 UserId int index not null,                 PhoneNumber int index);  
like image 608
user3600910 Avatar asked Aug 18 '15 11:08

user3600910


People also ask

How do you add a comment in PostgreSQL?

Syntax Using /* and */ symbols In PostgreSQL, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.

How do you add a comment to a column in SQL?

Use the COMMENT statement to add a comment about a table, view, materialized view, or column into the data dictionary. To drop a comment from the database, set it to the empty string ' '. See Also: "Comments" for more information on associating comments with SQL statements and schema objects.


1 Answers

Comments are attached to a column using 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'; 

You can also add a comment to the table:

comment on table session_log is 'Our session logs'; 

Additionally: int index is invalid.

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

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

like image 67
a_horse_with_no_name Avatar answered Nov 12 '22 19:11

a_horse_with_no_name