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

PostgreSQL - ALTER TABLE

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:

1. Rename a Table:

ALTER TABLE old_table_name RENAME TO new_table_name;

2. Add a New Column:

ALTER TABLE table_name ADD COLUMN column_name data_type [DEFAULT default_value] [constraints];

3. Rename a Column:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

4. Drop a Column:

ALTER TABLE table_name DROP COLUMN column_name [CASCADE | RESTRICT];

5. Change a Column's Data Type:

ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type [USING expression];

6. Set a Default Value for a Column:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;

7. Remove a Default Value from a Column:

ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT;

8. Add a Constraint:

For example, to add a UNIQUE constraint:

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);

9. Drop a Constraint:

ALTER TABLE table_name DROP CONSTRAINT constraint_name [CASCADE | RESTRICT];

10. Add a Foreign Key Constraint:

ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES referenced_table (referenced_column);

11. Set or Drop NOT NULL Constraint:

Set:

ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;

Drop:

ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;

12. Add or Drop a Primary Key:

Add:

ALTER TABLE table_name ADD PRIMARY KEY (column_name);

Drop:

ALTER TABLE table_name DROP CONSTRAINT primary_key_name;

13. Change a Table's Tablespace:

ALTER TABLE table_name SET TABLESPACE tablespace_name;

Points to Remember:

  • 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.

  1. Adding a new column to a table in PostgreSQL:

    • Description: Expanding an existing table by introducing a new column.
    • Code Example:
      ALTER TABLE your_table
      ADD COLUMN new_column_name data_type;
      
  2. Changing the data type of a column with ALTER TABLE in PostgreSQL:

    • Description: Modifying the data type of an existing column in a table.
    • Code Example:
      ALTER TABLE your_table
      ALTER COLUMN existing_column_name TYPE new_data_type;
      
  3. Renaming a column in PostgreSQL:

    • Description: Changing the name of an existing column in a PostgreSQL table.
    • Code Example:
      ALTER TABLE your_table
      RENAME COLUMN old_column_name TO new_column_name;
      
  4. Dropping a column with ALTER TABLE in PostgreSQL:

    • Description: Removing a column from an existing table in PostgreSQL.
    • Code Example:
      ALTER TABLE your_table
      DROP COLUMN column_to_remove;
      
  5. Adding or removing constraints with ALTER TABLE:

    • Description: Applying or removing constraints (e.g., UNIQUE, CHECK) to/from a table.
    • Code Examples:
      -- 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;
      
  6. Altering the default value of a column in PostgreSQL:

    • Description: Changing the default value assigned to a column.
    • Code Example:
      ALTER TABLE your_table
      ALTER COLUMN column_name SET DEFAULT new_default_value;
      
  7. Modifying table attributes in PostgreSQL:

    • Description: Adjusting various attributes of a PostgreSQL table.
    • Code Example:
      ALTER TABLE your_table
      SET (attribute_name = attribute_value);
      
  8. PostgreSQL ALTER TABLE vs ALTER COLUMN:

    • Description: Distinguishing between modifying a table and modifying a column.
    • Code Example:
      -- 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;