Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL ALTER TABLE Command

PostgreSQL ALTER TABLE Command

In PostgreSQL,ALTER TABLE the command is used to add, modify, or drop columns of an existing table.

You can also use ALTER TABLE to add and drop constraints.

Syntax

The syntax for adding a column to an existing table with ALTER TABLE is as follows:

ALTER TABLE table_name ADD column_name datatype;

To DROP COLUMN (delete a column) on an existing table, the syntax is as follows:

ALTER TABLE table_name DROP COLUMN column_name;

To modify the DATA TYPE (data type) of a column in a table, the syntax is as follows:

ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;

To add a NOT NULL constraint to a column in a table, the syntax is as follows:

ALTER TABLE table_name ALTER column_name datatype NOT NULL;

To ADD UNIQUE CONSTRAINT (add a UNIQUE constraint) to a column in a table, the syntax is as follows:

ALTER TABLE table_name

ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);

To ADD CHECK CONSTRAINT (add a CHECK constraint) to a table, the syntax is as follows:

ALTER TABLE table_name

ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

To ADD PRIMARY KEY (add a primary key) to a table, the syntax is as follows:

ALTER TABLE table_name

ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);

DROP CONSTRAINT (drop a constraint), the syntax is as follows:

ALTER TABLE table_name

DROP CONSTRAINT MyUniqueConstraint;

If it is MySQL, the code looks like this:

ALTER TABLE table_name

DROP INDEX MyUniqueConstraint;

DROP PRIMARY KEY (drop the primary key), the syntax is as follows:

ALTER TABLE table_name

DROP CONSTRAINT MyPrimaryKey;

If it is MySQL, the code looks like this:

ALTER TABLE table_name

DROP PRIMARY KEY;

Example

Create the COMPANY table (Download the COMPANY SQL file ), with the following data:

runoobdb# select * from COMPANY;

 id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000

(7 rows)

The following example adds a new column to this table:

runoobdb=# ALTER TABLE COMPANY ADD GENDER char(1);

The table now looks like this:

 id | name  | age | address     | salary | gender

----+-------+-----+-------------+--------+--------

  1 | Paul  |  32 | California  |  20000 |

  2 | Allen |  25 | Texas       |  15000 |

  3 | Teddy |  23 | Norway      |  20000 |

  4 | Mark  |  25 | Rich-Mond   |  65000 |

  5 | David |  27 | Texas       |  85000 |

  6 | Kim   |  22 | South-Hall  |  45000 |

  7 | James |  24 | Houston     |  10000 |

(7 rows)

The following example drops the GENDER column:

runoobdb=# ALTER TABLE COMPANY DROP GENDER;

The result is as follows:

id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000