In this PostgreSQL post explains how to use the PostgreSQL ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with syntax and examples).
The PostgreSQL ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The PostgreSQL ALTER TABLE statement is also used to rename a table.
The syntax to add a column in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the new column to add to the table.
The datatype of the column.
Let's look at an example that shows how to add a column in a PostgreSQL table using the ALTER TABLE statement.
For example:
This PostgreSQL ALTER TABLE example will add a column called order_date to the order_details table. It will be created as a NULL column.
The syntax to add multiple columns in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the new column to add to the table.
The datatype of the column.
Let's look at an example that shows how to add multiple columns in a PostgreSQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will add two columns to the order_details table - order_date and quantity.
The order_date field will be created as a date column and the quantity column will be created as an integer column.
The syntax to modify a column in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to modify in the table.
The modified datatype of the column.
Let's look at an example that shows how to modify a column in a PostgreSQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will modify the column called notes to be a data type of varchar(500) in the order_details table.
The syntax to modify multiple columns in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to modify in the table.
The modified datatype of the column.
Let's look at an example that shows how to modify multiple columns in a PostgreSQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will modify two columns to the order_details table - notes and quantity.
The notes field will be changed to a varchar(500) column and the quantity column will be modified to a numeric column.
The syntax to drop a column in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the column to delete from the table.
Let's look at an example that shows how to drop a column in a PostgreSQL table using the ALTER TABLE statement.
For example:
This ALTER TABLE example will drop the column called notes from the table called order_details.
The syntax to rename a column in a table in PostgreSQL (using the ALTER TABLE statement) is:
The name of the table to modify.
The column to rename.
The new name for the column.
Let's look at an example that shows how to rename a column in a PostgreSQL table using the ALTER TABLE statement.
For example:
This PostgreSQL ALTER TABLE example will rename the column called notes to order_notes in the order_details table.
To rename a table, the PostgreSQL ALTER TABLE syntax is:
The table to rename.
The new table name.
Let's look at an example that shows how to rename a table in PostgreSQL using the ALTER TABLE statement.
For example:
This ALTER TABLE example will rename the order_details table to order_information.