Next: GRANT and REVOKE
Up: Table Management
Previous: Temporary Tables
ALTER TABLE
ALTER TABLE allows the following operations:
- Rename tables
- Rename columns
- Add columns
- Add column defaults
- Remove column defaults
Figure
shows examples of these options.
test=> CREATE TABLE altertest (col1 INTEGER);
CREATE
test=> ALTER TABLE altertest RENAME TO alterdemo;
ALTER
test=> ALTER TABLE alterdemo RENAME COLUMN col1 TO democol;
ALTER
test=> ALTER TABLE alterdemo ADD COLUMN col2 INTEGER;
ALTER
test=> -- show renamed table, renamed column, and new column
test=> \d alterdemo
Table "alterdemo"
Attribute | Type | Modifier
-----------+---------+----------
democol | integer |
col2 | integer |
test=> ALTER TABLE alterdemo ALTER COLUMN col2 SET DEFAULT 0;
ALTER
test=> -- show new default value
test=> \d alterdemo
Table "alterdemo"
Attribute | Type | Modifier
-----------+---------+-----------
democol | integer |
col2 | integer | default 0
test=> ALTER TABLE alterdemo ALTER COLUMN col2 DROP DEFAULT;
ALTER
Bruce Momjian
2005-04-21