PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the ALTER TABLE
command is used to modify the structure of an existing table. This command allows for a wide variety of changes, such as adding or dropping columns, changing data types, renaming tables or columns, adding or removing constraints, and more.
Here's a comprehensive overview of the various uses of the ALTER TABLE
command:
ALTER TABLE old_table_name RENAME TO new_table_name;
ALTER TABLE table_name ADD COLUMN column_name data_type [DEFAULT default_value] [constraints];
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
ALTER TABLE table_name DROP COLUMN column_name [CASCADE | RESTRICT];
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type [USING expression];
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;
ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT;
For example, to add a UNIQUE
constraint:
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
ALTER TABLE table_name DROP CONSTRAINT constraint_name [CASCADE | RESTRICT];
ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES referenced_table (referenced_column);
Set:
ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;
Drop:
ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;
Add:
ALTER TABLE table_name ADD PRIMARY KEY (column_name);
Drop:
ALTER TABLE table_name DROP CONSTRAINT primary_key_name;
ALTER TABLE table_name SET TABLESPACE tablespace_name;
Altering tables, especially those with large amounts of data or those critical for application functionality, should be done with caution.
Some alterations, like changing a column's data type, can be time-consuming and can lock the table, affecting application performance and accessibility.
Always test changes in a development or staging environment before applying them to a production database.
Backup your database before making structural changes. This allows for recovery in case something goes wrong.
When dropping columns or constraints, be aware of the implications it might have on your applications, and ensure that no dependent code will break.
Adding a new column to a table in PostgreSQL:
ALTER TABLE your_table ADD COLUMN new_column_name data_type;
Changing the data type of a column with ALTER TABLE in PostgreSQL:
ALTER TABLE your_table ALTER COLUMN existing_column_name TYPE new_data_type;
Renaming a column in PostgreSQL:
ALTER TABLE your_table RENAME COLUMN old_column_name TO new_column_name;
Dropping a column with ALTER TABLE in PostgreSQL:
ALTER TABLE your_table DROP COLUMN column_to_remove;
Adding or removing constraints with ALTER TABLE:
-- Adding a UNIQUE constraint ALTER TABLE your_table ADD CONSTRAINT unique_constraint_name UNIQUE (column1, column2); -- Removing a CHECK constraint ALTER TABLE your_table DROP CONSTRAINT check_constraint_name;
Altering the default value of a column in PostgreSQL:
ALTER TABLE your_table ALTER COLUMN column_name SET DEFAULT new_default_value;
Modifying table attributes in PostgreSQL:
ALTER TABLE your_table SET (attribute_name = attribute_value);
PostgreSQL ALTER TABLE vs ALTER COLUMN:
-- ALTER TABLE ALTER TABLE your_table SET (attribute_name = attribute_value); -- ALTER COLUMN ALTER TABLE your_table ALTER COLUMN column_name TYPE new_data_type;