Summary of common operations of Postgresql ALTER statement

This article mainly introduces a summary of the common operations of Postgresql ALTER statement. This article explains the code examples of common operations such as adding a column, deleting a column, changing the data type of a column, renaming a table, changing the name of a column, setting not null of a field, etc. Friends who need it can refer to the following

 

postgresql version: psql (9.3.4)

1. Add a column

Copy the code The code is as follows:

ALTER TABLE table_name ADD column_name datatype;             


2, delete a column

Copy the code The code is as follows:

ALTER TABLE table_name DROP  column_name;                             


3. Change the data type of the column

Copy the code The code is as follows:

ALTER TABLE table_name ALTER  column_name TYPE datatype;              


4. Rename the table

Copy the code The code is as follows:

ALTER TABLE table_name RENAME TO new_name;                           


5. Change the name of the column

Copy the code The code is as follows:

ALTER TABLE table_name RENAME column_name to new_column_name;          


6. The not null setting of the field

Copy the code The code is as follows:

ALTER TABLE table_name ALTER column_name {SET|DROP} NOT NULL;          


7. Add default to the column

Copy the code The code is as follows:

ALTER TABLE table_name ALTER column_name SET DEFAULT expression;  

 

Quoted from: http://www.jb51.net/article/68421.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326311908&siteId=291194637