|
ALTER TABLENameALTER TABLE — Modifies table propertiesALTER TABLE table [ * ] ADD [ COLUMN ] column type ALTER TABLE table [ * ] ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT } ALTER TABLE table [ * ] RENAME [ COLUMN ] column TO newcolumn ALTER TABLE table RENAME TO newtable ALTER TABLE table ADD table constraint definition Inputs
Outputs
DescriptionALTER TABLE changes the definition of an existing table. The ADD COLUMN form adds a new column to the table using the same syntax as CREATE TABLE. The ALTER COLUMN form allows you to set or remove the default for the column. Note that defaults only apply to newly inserted rows. The RENAME clause causes the name of a table or column to change without changing any of the data contained in the affected table. Thus, the table or column 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. You must own the table in order to change its schema. NotesThe keyword COLUMN is noise and can be omitted. “*” following a name of a table indicates that the statement should be run over that table and all tables below it in the inheritance hierarchy; by default, the attribute will not be added to or renamed in any of the subclasses. This should always be done when adding or modifying an attribute in a superclass. If it is not, queries on the inheritance hierarchy such as SELECT NewColumn FROM SuperClass*will not work because the subclasses will be missing an attribute found in the superclass. In the current implementation, default and constraint clauses for the new column will be ignored. 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 the current implementation, only FOREIGN KEY constraints can be added to a table. To create or remove a unique constraint, create a unique index (see CREATE INDEX). To add check constraints you need to recreate and reload the table, using other parameters to the CREATE TABLE command. You must own the class in order to change its schema. Renaming any part of the schema of a system catalog is not permitted. The PostgreSQL User's Guide has further information on inheritance. Refer to CREATE TABLE for a further description of valid arguments. UsageTo add a column of type VARCHAR to a table: ALTER TABLE distributors ADD COLUMN address VARCHAR(30); To rename an existing column: ALTER TABLE distributors RENAME COLUMN address TO city; To rename an existing table: ALTER TABLE distributors RENAME TO suppliers; To add a foreign key constraint to a table: ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses(address) MATCH FULL CompatibilitySQL92The ADD COLUMN form is compliant with the exception that it does not support defaults and 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 Postgres:
The clauses to rename columns and tables are Postgres extensions from SQL92. |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |