ALTER TABLE [ ONLY ] table [ * ] ADD [ COLUMN ] column type [ column_constraint [ ... ] ] |
ALTER TABLE [ ONLY ] table [ * ] ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT } |
ALTER TABLE [ ONLY ] table [ * ] ALTER [ COLUMN ] column SET STATISTICS integer |
ALTER TABLE [ ONLY ] table [ * ] RENAME [ COLUMN ] column TO newcolumn |
ALTER TABLE table RENAME TO new_table |
ALTER TABLE table ADD table_constraint_definition |
ALTER TABLE [ ONLY ] table DROP CONSTRAINT constraint { RESTRICT | CASCADE } |
ALTER TABLE table OWNER TO new_owner |
The name of an existing table that you want to alter:
ONLY table changes apply only to the current table
table * changes apply to the current table and its children
table changes apply depending on the inheritance mode from configuration option SQL_INHERITANCE. (Refer to the section on Run Time Configuration in the Red Hat Database Administrator and User's Guide.)
Name of a new or existing column.
Type of the new column.
New name for an existing column.
New name for the table.
New table constraint for the table
The user name of the new owner of the table.
ALTER TABLE changes the definition of an existing table.
The ADD COLUMN form adds a new column to the table and uses the same syntax as CREATE TABLE.
The ALTER COLUMN {SET | DROP DEFAULT} form allows you to set or remove the default for the column. Note that defaults apply only to subsequent INSERT commands; they do not cause rows already in the table to change.
The ALTER COLUMN SET STATISTICS form allows you to set the statistic-gathering target for subsequent ANALYZE operations. (See the section on ANALYZE for details.)
The RENAME clause causes the name of a table, column, index, or sequence to change without changing any of the data contained in the affected table. Thus, the data will remain of the same type and size after this command is executed.
The ADD table constraint definition clause adds a new constraint to the table using the same syntax as CREATE TABLE.
The DROP CONSTRAINT constraint clause drops all constraints on the table (and its children) that match constraint.
The OWNER clause changes the owner of the table to be the user new_owner.
You must own the table in order to change its schema.
The keyword COLUMN is superfluous and can be omitted.
In the current implementation of ADD COLUMN, default and NOT NULL clauses for the new column are not supported. You can use the SET DEFAULT form of ALTER TABLE to set the default later. (You will also have to update the already existing rows to the new default value, using UPDATE.)
In DROP CONSTRAINT, the RESTRICT keyword is required, although dependencies are not yet checked. The CASCADE option is unsupported. Currently DROP CONSTRAINT drops only CHECK constraints. To remove a PRIMARY or UNIQUE constraint, drop the relevant index using the DROP INDEX command. To remove FOREIGN KEY constraints you need to recreate and reload the table, using other parameters to the CREATE TABLE command.
For example, to drop all constraints on a table distributors:
CREATE TABLE temp AS SELECT * FROM distributors; DROP TABLE distributors; CREATE TABLE distributors AS SELECT * FROM temp; DROP TABLE temp; |
You must own the table in order to change it. Changing any part of the schema of a system catalog is not permitted. Refer to the Section called Inheritance in Chapter 1 for more information on inheritance.
Refer to CREATE TABLE for a further description of valid arguments.
Create a table to be used in the following examples:
CREATE TABLE tmp (initial int4); |
To add a column of type int4 to a table:
ALTER TABLE tmp ADD COLUMN a int4; |
To rename an existing column:
ALTER TABLE tmp RENAME COLUMN a TO aa; |
To rename an existing table:
ALTER TABLE tmp RENAME TO tmpa; |
To add a check constraint to a table:
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5); |
To remove a check constraint from a table and all its children:
ALTER TABLE distributors DROP CONSTRAINT zipchk RESTRICT; |
To add a foreign key constraint to a table:
CREATE TABLE tmpx (a int4 primary key); ALTER TABLE tmpa ADD CONSTRAINT tmpconstr foreign key(b) reference tmpx(a) MATCH FULL; |
To add a (multicolumn) unique constraint to a table:
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode); |
To add an automatically named primary key constraint to a table (note that a table can only ever have one primary key):
ALTER TABLE distributors ADD PRIMARY KEY (dist_id); |
The ADD COLUMN form is compliant with the exception that it does not support defaults and NOT NULL constraints, as explained above. The ALTER COLUMN form is in full compliance.
SQL92 specifies some additional capabilities for ALTER TABLE statement which are not yet directly supported by PostgreSQL:
ALTER TABLE table DROP [ COLUMN ] column { RESTRICT | CASCADE } |
Removes a column from a table. Currently, you must recreate and reload the table in order to remove an existing column:
CREATE TABLE temp AS SELECT did, city FROM distributors; DROP TABLE distributors; CREATE TABLE distributors ( did DECIMAL(3) DEFAULT 1, name VARCHAR(40) NOT NULL ); INSERT INTO distributors SELECT * FROM temp; DROP TABLE temp; |
The clauses to rename tables, columns, indexes, and sequences are PostgreSQL extensions to SQL92.