5.35. PostgreSQL ALTER TABLE command

发布时间 :2025-10-25 12:30:47 UTC      

In PostgreSQL ALTER TABLE The command is used to add, modify, and delete a column that already exists in a table.

Besides, you can also use it. ALTER TABLE Command to add and remove constraints.

5.35.1. Grammar

Use ALTER TABLE The syntax to add a column to an existing table is as follows:

ALTER TABLE table_name ADD column_name datatype;

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

ALTER TABLE table_name DROP COLUMN column_name;

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

ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;

Add to a column in the table NOT NULL Constraints, the syntax is as follows:

ALTER TABLE table_name ALTER column_name datatype NOT NULL;

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

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

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

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);

Give the watch ADD PRIMARY KEY (add primary key), the syntax is as follows:

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

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

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 (delete 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;

5.35.2. Example

Create the COMPANY table ( 下载 COMPANY SQL 文件 ), the data are as follows:

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);

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

Delete the following example GENDER Column:

runoobdb=# ALTER TABLE COMPANY DROP GENDER;

The results are 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
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.