English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL ALTER TABLE Command

In PostgreSQL,ALTER TABLE Command for adding, modifying, and deleting columns of an existing table.

You can also use ALTER TABLE Command for adding and deleting constraints.

Syntax

Syntax for adding a column to an existing table using ALTER TABLE as follows:

ALTER TABLE table_name ADD column_name datatype;

DROP COLUMN (delete column) from an existing table, syntax as follows:

ALTER TABLE table_name DROP COLUMN column_name;

Modify the DATA TYPE (data type) of a specific column in the table, syntax as follows:

ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;

Add NOT NULL constraint to a specific column in the table, syntax as follows:

ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

Add UNIQUE constraint to a specific column in the table, syntax as follows:

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);

Add CHECK CONSTRAINT (add CHECK constraint) to the table, syntax as follows:

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

Add PRIMARY KEY to table (add primary key), syntax is as follows:

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

DROP CONSTRAINT (delete constraint), syntax is as follows:

ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;

If it is MYSQL, the code is like this:

ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;

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

ALTER TABLE table_name
DROP CONSTRAINT MyPrimaryKey;

If it is MYSQL, the code is like this:

ALTER TABLE table_name
DROP PRIMARY KEY;

Online Example

Create COMPANY table (Download COMPANY SQL file ),data content is as follows:

w3codeboxdb# 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 shows how to add a new column to this table:

w3codeboxdb=# 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 shows how to delete the GENDER column:

w3codeboxdb=# 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