WEB DEVELOPER SITE
PHP7
 

PostgreSQL ALTER TABLE command


In PostgreSQL, the ALTER TABLE command is used to add, modify, and delete a column of an existing table.

Also you can add and remove constraints with the ALTER TABLE command.

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;

DROP COLUMN on an existing table with the following syntax:

ALTER TABLE table_name DROP COLUMN column_name;

Modify the DATA TYPE of a column in the table with the following syntax:

ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;

Add a NOT NULL constraint to a column in the table with the following syntax:

ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

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

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

To ADD CHECK CONSTRAINT in the table, the syntax is as follows:

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

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

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

DROP CONSTRAINT, with the following syntax:

ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;

If it's MYSQL, the code looks like this:

ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;

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

ALTER TABLE table_name
DROP CONSTRAINT MyPrimaryKey;

If it's MYSQL, the code looks like this:

ALTER TABLE table_name
DROP PRIMARY KEY;

Instance

Create a 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
(7rows)

The following example adds a new column to this table:

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

Now the watch is 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 |
(7rows)

The following example deletes the GENDER column:

runoobdb=# ALTER TABLE COMPANY DROP GENDER;

Get the results 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