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. Use On an existing table To modify a column in a table Add to a column in the table Give a column in a table Give to the table Give the watch If it’s MYSQL, the code looks like this: If it’s MYSQL, the code looks like this: Create the COMPANY table ( 下载 COMPANY SQL 文件 ), the data are as follows: The following example adds a new column to this table: Now the watch looks like this: Delete the following example The results are as follows: 5.35.1. Grammar ¶
ALTER
TABLE
The syntax to add a column to an existing table is as follows:ALTER TABLE table_name ADD column_name datatype;
DROP
COLUMN
(delete column), the syntax is as follows:ALTER TABLE table_name DROP COLUMN column_name;
DATA
TYPE
(data type), the syntax is as follows:ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;
NOT
NULL
Constraints, the syntax is as follows:ALTER TABLE table_name ALTER column_name datatype NOT NULL;
ADD
UNIQUE
CONSTRAINT
(add UNIQUE constraint), the syntax is as follows:ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);
ADD
CHECK
CONSTRAINT
(add CHECK constraint), the syntax is as follows:ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
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;
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;
ALTER TABLE table_name
DROP PRIMARY KEY;
5.35.2. Example ¶
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)
runoobdb=# ALTER TABLE COMPANY ADD GENDER char(1);
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)
GENDER
Column:runoobdb=# ALTER TABLE COMPANY DROP GENDER;
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