{"Entry":{"collection":"sql","key":"alter-table","name":"ALTER TABLE","aliases":["altertable"],"metadata":{"aliases":["altertable"],"changed_in":["6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18"],"changes":[{"from":"6.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ * ] ADD [ COLUMN ] ER\"\u003ecoBLE\u003e type","[ * ] RENAME [ COLUMN ] ER\"\u003ecoBLE\u003e TO newcolumn"],"removed":["[ * ] ADD [ COLUMN ] column type","ALTER TABLE table","[ * ] RENAME [ COLUMN ] column TO newcolumn"]},"to":"6.5"},{"from":"6.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["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","ADD table constraint definition"],"removed":["[ * ] ADD [ COLUMN ] ER\"\u003ecoBLE\u003e type","[ * ] RENAME [ COLUMN ] ER\"\u003ecoBLE\u003e TO newcolumn"]},"to":"7.0"},{"from":"7.0","purpose_changed":false,"renamed":{"from_file":"sql-altertable.htm","to_file":"sql-altertable.html"},"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE table","OWNER TO new owner"],"removed":[]},"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ADD [ COLUMN ] column type [ column_constraint [ ... ] ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER [ COLUMN ] column SET STATISTICS integer","ALTER TABLE [ ONLY ] table [ * ]","RENAME TO new_table","ADD table_constraint_definition","ALTER TABLE [ ONLY ] table","DROP CONSTRAINT constraint { RESTRICT | CASCADE }","OWNER TO new_owner"],"removed":["RENAME TO newtable","ADD table constraint definition","OWNER TO new owner"]},"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ADD [ COLUMN ] column type [ column_constraint [ ... ] ]","ALTER TABLE [ ONLY ] table [ * ]","DROP [ COLUMN ] column [ RESTRICT | CASCADE ]","ALTER [ COLUMN ] column { SET | DROP } NOT NULL","ALTER TABLE [ ONLY ] table [ * ]","ALTER [ COLUMN ] column SET STATISTICS integer","ALTER TABLE [ ONLY ] table [ * ]","ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }","RENAME [ COLUMN ] column TO new_column","ALTER TABLE [ ONLY ] table [ * ]","ADD table_constraint","ALTER TABLE [ ONLY ] table [ * ]","DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]"],"removed":["RENAME [ COLUMN ] column TO newcolumn","ADD table_constraint_definition","DROP CONSTRAINT constraint { RESTRICT | CASCADE }"]},"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","notes","examples"],"changed":["description","compatibility"],"removed":["usage"]},"status":"changed","synopsis":{"added":["ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER [ COLUMN ] column { SET DEFAULT expression | DROP DEFAULT }","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","SET WITHOUT OIDS","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE name","RENAME TO new_name","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE name","ALTER TABLE name","CLUSTER ON index_name"],"removed":["ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT }","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE table","RENAME TO new_table","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE [ ONLY ] table [ * ]","ALTER TABLE table"]},"to":"7.4"},{"from":"7.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["action [, ... ]","where action is one of:","ADD [ COLUMN ] column type [ column_constraint [ ... ] ]","DROP [ COLUMN ] column [ RESTRICT | CASCADE ]","ALTER [ COLUMN ] column TYPE type [ USING expression ]","ALTER [ COLUMN ] column SET DEFAULT expression","ALTER [ COLUMN ] column DROP DEFAULT","ALTER [ COLUMN ] column { SET | DROP } NOT NULL","ALTER [ COLUMN ] column SET STATISTICS integer","ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }","CLUSTER ON index_name","SET WITHOUT CLUSTER","SET WITHOUT OIDS","SET TABLESPACE tablespace_name"],"removed":["ADD [ COLUMN ] column type [ column_constraint [ ... ] ]","ALTER TABLE [ ONLY ] name [ * ]","DROP [ COLUMN ] column [ RESTRICT | CASCADE ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER [ COLUMN ] column { SET DEFAULT expression | DROP DEFAULT }","ALTER TABLE [ ONLY ] name [ * ]","ALTER [ COLUMN ] column { SET | DROP } NOT NULL","ALTER TABLE [ ONLY ] name [ * ]","ALTER [ COLUMN ] column SET STATISTICS integer","ALTER TABLE [ ONLY ] name [ * ]","ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }","ALTER TABLE [ ONLY ] name [ * ]","SET WITHOUT OIDS","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE [ ONLY ] name [ * ]","ALTER TABLE name","ALTER TABLE name","CLUSTER ON index_name"]},"to":"8.0"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER TABLE name","SET SCHEMA new_schema","DISABLE TRIGGER [ trigger_name | ALL | USER ]","ENABLE TRIGGER [ trigger_name | ALL | USER ]","SET TABLESPACE new_tablespace"],"removed":["SET TABLESPACE tablespace_name"]},"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples"],"removed":[]},"status":"changed","synopsis":{"added":["SET ( storage_parameter = value [, ... ] )","RESET ( storage_parameter [, ... ] )","INHERIT parent_table","NO INHERIT parent_table"],"removed":[]},"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["ENABLE REPLICA TRIGGER trigger_name","ENABLE ALWAYS TRIGGER trigger_name","DISABLE RULE rewrite_rule_name","ENABLE RULE rewrite_rule_name","ENABLE REPLICA RULE rewrite_rule_name","ENABLE ALWAYS RULE rewrite_rule_name"],"removed":[]},"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER [ COLUMN ] column [ SET DATA ] TYPE type [ USING expression ]","SET WITH OIDS","SET WITHOUT OIDS"],"removed":[]},"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["DROP [ COLUMN ] [ IF EXISTS ] column [ RESTRICT | CASCADE ]","ALTER [ COLUMN ] column SET ( attribute_option = value [, ... ] )","ALTER [ COLUMN ] column RESET ( attribute_option [, ... ] )","ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }","DROP CONSTRAINT [ IF EXISTS ]  constraint_name [ RESTRICT | CASCADE ]"],"removed":["where action is one of:"]},"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":["see_also"],"changed":["description","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ADD [ COLUMN ] column data_type [ COLLATE collation ] [ column_constraint [ ... ] ]","ALTER [ COLUMN ] column [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]","ADD table_constraint [ NOT VALID ]","ADD table_constraint_using_index","VALIDATE CONSTRAINT constraint_name","OF type_name","NOT OF","[ CONSTRAINT constraint_name ]","{ UNIQUE | PRIMARY KEY } USING INDEX index_name","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]"],"removed":["ADD [ COLUMN ] column type [ column_constraint [ ... ] ]","ALTER [ COLUMN ] column [ SET DATA ] TYPE type [ USING expression ]"]},"to":"9.1"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]","ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]","RENAME [ COLUMN ] column_name TO new_column_name","ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]","RENAME CONSTRAINT constraint_name TO new_constraint_name","ALTER TABLE [ IF EXISTS ] name","ALTER TABLE [ IF EXISTS ] name","ADD [ COLUMN ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]","DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]","ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]","ALTER [ COLUMN ] column_name SET DEFAULT expression","ALTER [ COLUMN ] column_name DROP DEFAULT","ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL","ALTER [ COLUMN ] column_name SET STATISTICS integer","ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )","ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )","ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }"],"removed":["RENAME [ COLUMN ] column TO new_column","ADD [ COLUMN ] column data_type [ COLLATE collation ] [ column_constraint [ ... ] ]","DROP [ COLUMN ] [ IF EXISTS ] column [ RESTRICT | CASCADE ]","ALTER [ COLUMN ] column [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]","ALTER [ COLUMN ] column SET DEFAULT expression","ALTER [ COLUMN ] column DROP DEFAULT","ALTER [ COLUMN ] column { SET | DROP } NOT NULL","ALTER [ COLUMN ] column SET STATISTICS integer","ALTER [ COLUMN ] column SET ( attribute_option = value [, ... ] )","ALTER [ COLUMN ] column RESET ( attribute_option [, ... ] )","ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }"]},"to":"9.2"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"9.3"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]","SET TABLESPACE new_tablespace [ NOWAIT ]","ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]","REPLICA IDENTITY {DEFAULT | USING INDEX index_name | FULL | NOTHING}"],"removed":[]},"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples"],"removed":[]},"status":"changed","synopsis":{"added":["DISABLE ROW LEVEL SECURITY","ENABLE ROW LEVEL SECURITY","FORCE ROW LEVEL SECURITY","NO FORCE ROW LEVEL SECURITY","SET TABLESPACE new_tablespace","SET { LOGGED | UNLOGGED }","SET ( storage_parameter [= value] [, ... ] )","OWNER TO { new_owner | CURRENT_USER | SESSION_USER }"],"removed":["SET TABLESPACE new_tablespace"]},"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters"],"removed":[]},"status":"changed","synopsis":{"added":["ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]"],"removed":[]},"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER TABLE [ IF EXISTS ] name","ATTACH PARTITION partition_name FOR VALUES partition_bound_spec","ALTER TABLE [ IF EXISTS ] name","DETACH PARTITION partition_name","ALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]","ALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...]","ALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ]"],"removed":[]},"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["ATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }","IN ( { numeric_literal | string_literal | TRUE | FALSE | NULL } [, ...] ) |","FROM ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] )","TO ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] ) |","WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )","[ CONSTRAINT constraint_name ]","{ NOT NULL |","NULL |","CHECK ( expression ) [ NO INHERIT ] |","DEFAULT default_expr |","GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |","UNIQUE index_parameters |","PRIMARY KEY index_parameters |","REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]","[ ON DELETE action ] [ ON UPDATE action ] }","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]","[ CONSTRAINT constraint_name ]","{ CHECK ( expression ) [ NO INHERIT ] |","UNIQUE ( column_name [, ... ] ) index_parameters |","PRIMARY KEY ( column_name [, ... ] ) index_parameters |","EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |","FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]","[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] }","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]","[ INCLUDE ( column_name [, ... ] ) ]","[ WITH ( storage_parameter [= value] [, ... ] ) ]","[ USING INDEX TABLESPACE tablespace_name ]","{ column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]"],"removed":[]},"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["IN ( partition_bound_expr [, ...] ) |","FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )","TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |","GENERATED ALWAYS AS ( generation_expr ) STORED |","GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |","[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }","[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }"],"removed":["SET WITH OIDS","SET WITHOUT OIDS","IN ( { numeric_literal | string_literal | TRUE | FALSE | NULL } [, ...] ) |","FROM ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] )","TO ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] ) |","[ ON DELETE action ] [ ON UPDATE action ] }","[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] }"]},"to":"12"},{"from":"12","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","examples"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]","{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]"],"removed":[]},"to":"13"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":{"added":["DETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]","ALTER [ COLUMN ] column_name SET COMPRESSION compression_method","OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }"],"removed":[]},"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["SET ACCESS METHOD new_access_method","SET TABLESPACE new_tablespace","UNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |","UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] ) index_parameters |","{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }"],"removed":[]},"to":"15"},{"from":"15","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }"],"removed":[]},"to":"16"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER [ COLUMN ] column_name SET EXPRESSION AS ( expression )","ALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]","ALTER [ COLUMN ] column_name SET STATISTICS { integer | DEFAULT }","SET ACCESS METHOD { new_access_method | DEFAULT }"],"removed":[]},"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","notes","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]","ALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ]","{ NOT NULL [ NO INHERIT ] |","GENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]","[ CONSTRAINT constraint_name ]","{ CHECK ( expression ) [ NO INHERIT ] |","NOT NULL column_name [ NO INHERIT ] |","UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |","PRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |","FOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ]  [, PERIOD refcolumn ] ) ]","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]"],"removed":[]},"to":"18"},{"from":"18","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples"],"removed":[]},"status":"changed","synopsis":null,"to":"19"}],"content_hash":"c74ec5e17bf7511dc2d28cb0b47ae43fad9f2e6b1cf9f5581a16dd43b9ebaaa4","editorial":{},"first_version":"6.4","group":"table","imported_at":"2026-09-30T17:43:36.589138+08:00","last_version":"20","name":"ALTER TABLE","object":"TABLE","position":9,"present_in":["6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"change the definition of a table","purpose_zh":"","related":["create-table"],"slug":"alter-table","source_rev":"a709ab85","synopsis":"ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\naction [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME [ COLUMN ] column_name TO new_column_name\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME CONSTRAINT constraint_name TO new_constraint_name\nALTER TABLE [ IF EXISTS ] name\nRENAME TO new_name\nALTER TABLE [ IF EXISTS ] name\nSET SCHEMA new_schema\nALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]\nSET TABLESPACE new_tablespace [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] name\nATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }\nALTER TABLE [ IF EXISTS ] name\nDETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]\n\nwhere action is one of:\n\nADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]\nDROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]\nALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]\nALTER [ COLUMN ] column_name SET DEFAULT expression\nALTER [ COLUMN ] column_name DROP DEFAULT\nALTER [ COLUMN ] column_name { SET | DROP } NOT NULL\nALTER [ COLUMN ] column_name SET EXPRESSION AS ( expression )\nALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]\nALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]\nALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...]\nALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ]\nALTER [ COLUMN ] column_name SET STATISTICS { integer | DEFAULT }\nALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )\nALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )\nALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\nALTER [ COLUMN ] column_name SET COMPRESSION compression_method\nADD table_constraint [ NOT VALID ]\nADD table_constraint_using_index\nALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\nALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ]\nVALIDATE CONSTRAINT constraint_name\nDROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]\nDISABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE REPLICA TRIGGER trigger_name\nENABLE ALWAYS TRIGGER trigger_name\nDISABLE RULE rewrite_rule_name\nENABLE RULE rewrite_rule_name\nENABLE REPLICA RULE rewrite_rule_name\nENABLE ALWAYS RULE rewrite_rule_name\nDISABLE ROW LEVEL SECURITY\nENABLE ROW LEVEL SECURITY\nFORCE ROW LEVEL SECURITY\nNO FORCE ROW LEVEL SECURITY\nCLUSTER ON index_name\nSET WITHOUT CLUSTER\nSET WITHOUT OIDS\nSET ACCESS METHOD { new_access_method | DEFAULT }\nSET TABLESPACE new_tablespace\nSET { LOGGED | UNLOGGED }\nSET ( storage_parameter [= value] [, ... ] )\nRESET ( storage_parameter [, ... ] )\nINHERIT parent_table\nNO INHERIT parent_table\nOF type_name\nNOT OF\nOWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\nREPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }\n\nand partition_bound_spec is:\n\nIN ( partition_bound_expr [, ...] ) |\nFROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numeric_literal, REMAINDER numeric_literal )\n\nand column_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL [ NO INHERIT ] |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT default_expr |\nGENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |\nGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |\nPRIMARY KEY index_parameters |\nREFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\nand table_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ CHECK ( expression ) [ NO INHERIT ] |\nNOT NULL column_name [ NO INHERIT ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nPRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nEXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |\nFOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ]\n[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\nand table_constraint_using_index is:\n\n[ CONSTRAINT constraint_name ]\n{ UNIQUE | PRIMARY KEY } USING INDEX index_name\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\nindex_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:\n\n[ INCLUDE ( column_name [, ... ] ) ]\n[ WITH ( storage_parameter [= value] [, ... ] ) ]\n[ USING INDEX TABLESPACE tablespace_name ]\n\nexclude_element in an EXCLUDE constraint is:\n\n{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\nreferential_action in a FOREIGN KEY/REFERENCES constraint is:\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }","verb":"ALTER"}},"Definition":{"Collection":"sql","Key":"alter-table","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"alter-table","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-ALTERTABLE","file":"sql-altertable.html","lang":"en","name":"ALTER TABLE","purpose":"change the definition of a table","purpose_zh":"","related":["create-table"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e changes the definition of an existing table. There are several subforms described below. Note that the lock level required may differ for each subform. An \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock is acquired unless explicitly noted. When multiple subcommands are given, the lock acquired will be the strictest one required by any subcommand.\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD [ COLUMN ] [ IF NOT EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form adds a new column to the table, using the same syntax as \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e. If \u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e is specified and a column already exists with this name, no error is thrown.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP [ COLUMN ] [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form drops a column from a table. Indexes and table constraints involving the column will be automatically dropped as well. Multivariate statistics referencing the dropped column will also be removed if the removal of the column would cause the statistics to contain data for only a single column. You will need to say \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e if anything outside the table depends on the column, for example, foreign key references or views. If \u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e is specified and the column does not exist, no error is thrown. In this case a notice is issued instead.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the type of a column of a table. Indexes and simple table constraints involving the column will be automatically converted to use the new column type by reparsing the originally supplied expression. The optional \u003ccode class=\"literal\"\u003eCOLLATE\u003c/code\u003e clause specifies a collation for the new column; if omitted, the collation is the default for the new column type. The optional \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause must be provided if there is no implicit or assignment cast from old to new type.\u003c/p\u003e\u003cp\u003eWhen this form is used, the column's statistics are removed, so running \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e on the table afterwards is recommended. For a virtual generated column, \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e is not necessary because such columns never have statistics.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e/\u003ccode class=\"literal\"\u003eDROP DEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms set or remove the default value for a column (where removal is equivalent to setting the default value to NULL). The new default value will only apply in subsequent \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e or \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e commands; it does not cause rows already in the table to change.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e/\u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms change whether a column is marked to allow null values or to reject null values.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSET NOT NULL\u003c/code\u003e may only be applied to a column provided none of the records in the table contain a \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e value for the column. Ordinarily this is checked during the \u003ccode class=\"literal\"\u003eALTER TABLE\u003c/code\u003e by scanning the entire table, unless \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e is specified; however, if a valid \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint exists (and is not dropped in the same command) which proves no \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e can exist, then the table scan is skipped. If a column has an invalid not-null constraint, \u003ccode class=\"literal\"\u003eSET NOT NULL\u003c/code\u003e validates it.\u003c/p\u003e\u003cp\u003eIf this table is a partition, one cannot perform \u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e on a column if it is marked \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e in the parent table. To drop the \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint from all the partitions, perform \u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e on the parent table. Even if there is no \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint on the parent, such a constraint can still be added to individual partitions, if desired; that is, the children can disallow nulls even if the parent allows them, but not the other way around. It is also possible to drop the \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint from \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e the parent table, which does not remove it from the children.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET EXPRESSION AS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form replaces the expression of a generated column. Existing data in a stored generated column is rewritten and all the future changes will apply the new generation expression.\u003c/p\u003e\u003cp\u003eWhen this form is used on a stored generated column, its statistics are removed, so running \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e on the table afterwards is recommended. For a virtual generated column, \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e is not necessary because such columns never have statistics.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP EXPRESSION [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form turns a stored generated column into a normal base column. Existing data in the columns is retained, but future changes will no longer apply the generation expression.\u003c/p\u003e\u003cp\u003eThis form is currently only supported for stored generated columns (not virtual ones).\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003eDROP EXPRESSION IF EXISTS\u003c/code\u003e is specified and the column is not a generated column, no error is thrown. In this case a notice is issued instead.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET GENERATED { ALWAYS | BY DEFAULT }\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP IDENTITY [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms change whether a column is an identity column or change the generation attribute of an existing identity column. See \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e for details. Like \u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e, these forms only affect the behavior of subsequent \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e and \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e commands; they do not cause rows already in the table to change.\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003eDROP IDENTITY IF EXISTS\u003c/code\u003e is specified and the column is not an identity column, no error is thrown. In this case a notice is issued instead.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTART\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms alter the sequence that underlies an existing identity column. \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e is an option supported by \u003ca href=\"/docs/18/sql-altersequence.html\" title=\"ALTER SEQUENCE\"\u003e\u003ccode class=\"command\"\u003eALTER SEQUENCE\u003c/code\u003e\u003c/a\u003e such as \u003ccode class=\"literal\"\u003eINCREMENT BY\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET STATISTICS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form sets the per-column statistics-gathering target for subsequent \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e operations. The target can be set in the range 0 to 10000. Set it to \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e to revert to using the system default statistics target (\u003ca href=\"/docs/18/runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET\"\u003edefault_statistics_target\u003c/a\u003e). (Setting to a value of -1 is an obsolete way spelling to get the same outcome.) For more information on the use of statistics by the \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e query planner, refer to \u003ca href=\"/docs/18/planner-stats.html\" title=\"14.2. Statistics Used by the Planner\"\u003eSection 14.2\u003c/a\u003e.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSET STATISTICS\u003c/code\u003e acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form sets or resets per-attribute options. Currently, the only defined per-attribute options are \u003ccode class=\"literal\"\u003en_distinct\u003c/code\u003e and \u003ccode class=\"literal\"\u003en_distinct_inherited\u003c/code\u003e, which override the number-of-distinct-values estimates made by subsequent \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e operations. \u003ccode class=\"literal\"\u003en_distinct\u003c/code\u003e affects the statistics for the table itself, while \u003ccode class=\"literal\"\u003en_distinct_inherited\u003c/code\u003e affects the statistics gathered for the table plus its inheritance children, and for the statistics gathered for partitioned tables. When the value specified is a positive value, the query planner will assume that the column contains exactly the specified number of distinct nonnull values. Fractional values may also be specified by using values below 0 and above or equal to -1. This instructs the query planner to estimate the number of distinct values by multiplying the absolute value of the specified number by the estimated number of rows in the table. For example, a value of -1 implies that all values in the column are distinct, while a value of -0.5 implies that each value appears twice on average. This can be useful when the size of the table changes over time. For more information on the use of statistics by the \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e query planner, refer to \u003ca href=\"/docs/18/planner-stats.html\" title=\"14.2. Statistics Used by the Planner\"\u003eSection 14.2\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eChanging per-attribute options acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form sets the storage mode for a column. This controls whether this column is held inline or in a secondary \u003cacronym\u003eTOAST\u003c/acronym\u003e table, and whether the data should be compressed or not. \u003ccode class=\"literal\"\u003ePLAIN\u003c/code\u003e must be used for fixed-length values such as \u003ccode class=\"type\"\u003einteger\u003c/code\u003e and is inline, uncompressed. \u003ccode class=\"literal\"\u003eMAIN\u003c/code\u003e is for inline, compressible data. \u003ccode class=\"literal\"\u003eEXTERNAL\u003c/code\u003e is for external, uncompressed data, and \u003ccode class=\"literal\"\u003eEXTENDED\u003c/code\u003e is for external, compressed data. Writing \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e sets the storage mode to the default mode for the column's data type. \u003ccode class=\"literal\"\u003eEXTENDED\u003c/code\u003e is the default for most data types that support non-\u003ccode class=\"literal\"\u003ePLAIN\u003c/code\u003e storage. Use of \u003ccode class=\"literal\"\u003eEXTERNAL\u003c/code\u003e will make substring operations on very large \u003ccode class=\"type\"\u003etext\u003c/code\u003e and \u003ccode class=\"type\"\u003ebytea\u003c/code\u003e values run faster, at the penalty of increased storage space. Note that \u003ccode class=\"literal\"\u003eALTER TABLE ... SET STORAGE\u003c/code\u003e doesn't itself change anything in the table; it just sets the strategy to be pursued during future table updates. See \u003ca href=\"/docs/18/storage-toast.html\" title=\"66.2. TOAST\"\u003eSection 66.2\u003c/a\u003e for more information.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form sets the compression method for a column, determining how values inserted in future will be compressed (if the storage mode permits compression at all). This does not cause the table to be rewritten, so existing data may still be compressed with other compression methods. If the table is restored with \u003cspan class=\"application\"\u003epg_restore\u003c/span\u003e, then all values are rewritten with the configured compression method. However, when data is inserted from another relation (for example, by \u003ccode class=\"command\"\u003eINSERT ... SELECT\u003c/code\u003e), values from the source table are not necessarily detoasted, so any previously compressed data may retain its existing compression method, rather than being recompressed with the compression method of the target column. The supported compression methods are \u003ccode class=\"literal\"\u003epglz\u003c/code\u003e and \u003ccode class=\"literal\"\u003elz4\u003c/code\u003e. (\u003ccode class=\"literal\"\u003elz4\u003c/code\u003e is available only if \u003ccode class=\"option\"\u003e--with-lz4\u003c/code\u003e was used when building \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.) In addition, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e can be \u003ccode class=\"literal\"\u003edefault\u003c/code\u003e, which selects the default behavior of consulting the \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TOAST-COMPRESSION\"\u003edefault_toast_compression\u003c/a\u003e setting at the time of data insertion to determine the method to use.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e [ NOT VALID ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form adds a new constraint to a table using the same constraint syntax as \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e, plus the option \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e, which is currently only allowed for foreign-key, \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e, and not-null constraints.\u003c/p\u003e\u003cp\u003eNormally, this form will cause a scan of the table to verify that all existing rows in the table satisfy the new constraint. But if the \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e option is used, this potentially-lengthy scan is skipped. The constraint will still be applied against subsequent inserts or updates (that is, they'll fail unless there is a matching row in the referenced table, in the case of foreign keys, or they'll fail unless the new row matches the specified check condition). But the database will not assume that the constraint holds for all rows in the table, until it is validated by using the \u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e option. See \u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"Notes\"\u003eNotes\u003c/a\u003e below for more information about using the \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e option.\u003c/p\u003e\u003cp\u003eAlthough most forms of \u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e require an \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock, \u003ccode class=\"literal\"\u003eADD FOREIGN KEY\u003c/code\u003e requires only a \u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e lock. Note that \u003ccode class=\"literal\"\u003eADD FOREIGN KEY\u003c/code\u003e also acquires a \u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e lock on the referenced table, in addition to the lock on the table on which the constraint is declared.\u003c/p\u003e\u003cp\u003eAdditional restrictions apply when unique or primary key constraints are added to partitioned tables; see \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form adds a new \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e constraint to a table based on an existing unique index. All the columns of the index will be included in the constraint.\u003c/p\u003e\u003cp\u003eThe index cannot have expression columns nor be a partial index. Also, it must be a b-tree index with default sort ordering. These restrictions ensure that the index is equivalent to one that would be built by a regular \u003ccode class=\"literal\"\u003eADD PRIMARY KEY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eADD UNIQUE\u003c/code\u003e command.\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e is specified, and the index's columns are not already marked \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e, then this command will attempt to do \u003ccode class=\"literal\"\u003eALTER COLUMN SET NOT NULL\u003c/code\u003e against each such column. That requires a full table scan to verify the column(s) contain no nulls. In all other cases, this is a fast operation.\u003c/p\u003e\u003cp\u003eIf a constraint name is provided then the index will be renamed to match the constraint name. Otherwise the constraint will be named the same as the index.\u003c/p\u003e\u003cp\u003eAfter this command is executed, the index is \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eowned\u003c/span\u003e”\u003c/span\u003e by the constraint, in the same way as if the index had been built by a regular \u003ccode class=\"literal\"\u003eADD PRIMARY KEY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eADD UNIQUE\u003c/code\u003e command. In particular, dropping the constraint will make the index disappear too.\u003c/p\u003e\u003cp\u003eThis form is not currently supported on partitioned tables.\u003c/p\u003e\u003cdiv class=\"note\"\u003e\u003ch3\u003eNote\u003c/h3\u003e\u003cp\u003eAdding a constraint using an existing index can be helpful in situations where a new constraint needs to be added without blocking table updates for a long time. To do that, create the index using \u003ccode class=\"command\"\u003eCREATE UNIQUE INDEX CONCURRENTLY\u003c/code\u003e, and then convert it to a constraint using this syntax. See the example below.\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form alters the attributes of a constraint that was previously created. Currently only foreign key constraints may be altered in this fashion, but see below.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT ... INHERIT\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT ... NO INHERIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms modify a inheritable constraint so that it becomes not inheritable, or vice-versa. Only not-null constraints may be altered in this fashion at present. In addition to changing the inheritability status of the constraint, in the case where a non-inheritable constraint is being marked inheritable, if the table has children, an equivalent constraint will be added to them. If marking an inheritable constraint as non-inheritable on a table with children, then the corresponding constraint on children will be marked as no longer inherited, but not removed.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form validates a foreign key, check, or not-null constraint that was previously created as \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e, by scanning the table to ensure there are no rows for which the constraint is not satisfied. If the constraint was set to \u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e, an error is thrown. Nothing happens if the constraint is already marked valid. (See \u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"Notes\"\u003eNotes\u003c/a\u003e below for an explanation of the usefulness of this command.)\u003c/p\u003e\u003cp\u003eThis command acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP CONSTRAINT [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form drops the specified constraint on a table, along with any index underlying the constraint. If \u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e is specified and the constraint does not exist, no error is thrown. In this case a notice is issued instead.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE [ REPLICA | ALWAYS ] TRIGGER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms configure the firing of trigger(s) belonging to the table. A disabled trigger is still known to the system, but is not executed when its triggering event occurs. (For a deferred trigger, the enable status is checked when the event occurs, not when the trigger function is actually executed.) One can disable or enable a single trigger specified by name, or all triggers on the table, or only user triggers (this option excludes internally generated constraint triggers, such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints). Disabling or enabling internally generated constraint triggers requires superuser privileges; it should be done with caution since of course the integrity of the constraint cannot be guaranteed if the triggers are not executed.\u003c/p\u003e\u003cp\u003eThe trigger firing mechanism is also affected by the configuration variable \u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\"\u003esession_replication_role\u003c/a\u003e. Simply enabled triggers (the default) will fire when the replication role is \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eorigin\u003c/span\u003e”\u003c/span\u003e (the default) or \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003elocal\u003c/span\u003e”\u003c/span\u003e. Triggers configured as \u003ccode class=\"literal\"\u003eENABLE REPLICA\u003c/code\u003e will only fire if the session is in \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003ereplica\u003c/span\u003e”\u003c/span\u003e mode, and triggers configured as \u003ccode class=\"literal\"\u003eENABLE ALWAYS\u003c/code\u003e will fire regardless of the current replication role.\u003c/p\u003e\u003cp\u003eThe effect of this mechanism is that in the default configuration, triggers do not fire on replicas. This is useful because if a trigger is used on the origin to propagate data between tables, then the replication system will also replicate the propagated data; so the trigger should not fire a second time on the replica, because that would lead to duplication. However, if a trigger is used for another purpose such as creating external alerts, then it might be appropriate to set it to \u003ccode class=\"literal\"\u003eENABLE ALWAYS\u003c/code\u003e so that it is also fired on replicas.\u003c/p\u003e\u003cp\u003eWhen this command is applied to a partitioned table, the states of corresponding clone triggers in the partitions are updated too, unless \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is specified.\u003c/p\u003e\u003cp\u003eThis command acquires a \u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE [ REPLICA | ALWAYS ] RULE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms configure the firing of rewrite rules belonging to the table. A disabled rule is still known to the system, but is not applied during query rewriting. The semantics are as for disabled/enabled triggers. This configuration is ignored for \u003ccode class=\"literal\"\u003eON SELECT\u003c/code\u003e rules, which are always applied in order to keep views working even if the current session is in a non-default replication role.\u003c/p\u003e\u003cp\u003eThe rule firing mechanism is also affected by the configuration variable \u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\"\u003esession_replication_role\u003c/a\u003e, analogous to triggers as described above.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms control the application of row security policies belonging to the table. If enabled and no policies exist for the table, then a default-deny policy is applied. Note that policies can exist for a table even if row-level security is disabled. In this case, the policies will \u003cspan class=\"emphasis\"\u003e\u003cem\u003enot\u003c/em\u003e\u003c/span\u003e be applied and the policies will be ignored. See also \u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\"\u003e\u003ccode class=\"command\"\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO FORCE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eFORCE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese forms control the application of row security policies belonging to the table when the user is the table owner. If enabled, row-level security policies will be applied when the user is the table owner. If disabled (the default) then row-level security will not be applied when the user is the table owner. See also \u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\"\u003e\u003ccode class=\"command\"\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCLUSTER ON\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form selects the default index for future \u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e operations. It does not actually re-cluster the table.\u003c/p\u003e\u003cp\u003eChanging cluster options acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET WITHOUT CLUSTER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form removes the most recently used \u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e index specification from the table. This affects future cluster operations that don't specify an index.\u003c/p\u003e\u003cp\u003eChanging cluster options acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET WITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eBackward-compatible syntax for removing the \u003ccode class=\"literal\"\u003eoid\u003c/code\u003e system column. As \u003ccode class=\"literal\"\u003eoid\u003c/code\u003e system columns cannot be added anymore, this never has an effect.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ACCESS METHOD\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the access method of the table by rewriting it using the indicated access method; specifying \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e selects the access method set as the \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e configuration parameter. See \u003ca href=\"/docs/18/tableam.html\" title=\"Chapter 62. Table Access Method Interface Definition\"\u003eChapter 62\u003c/a\u003e for more information.\u003c/p\u003e\u003cp\u003eWhen applied to a partitioned table, there is no data to rewrite, but partitions created afterwards will default to the given access method unless overridden by a \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause. Specifying \u003ccode class=\"varname\"\u003eDEFAULT\u003c/code\u003e removes a previous value, causing future partitions to default to \u003ccode class=\"varname\"\u003edefault_table_access_method\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET TABLESPACE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the table's tablespace to the specified tablespace and moves the data file(s) associated with the table to the new tablespace. Indexes on the table, if any, are not moved; but they can be moved separately with additional \u003ccode class=\"literal\"\u003eSET TABLESPACE\u003c/code\u003e commands. When applied to a partitioned table, nothing is moved, but any partitions created afterwards with \u003ccode class=\"command\"\u003eCREATE TABLE PARTITION OF\u003c/code\u003e will use that tablespace, unless overridden by a \u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e clause.\u003c/p\u003e\u003cp\u003eAll tables in the current database in a tablespace can be moved by using the \u003ccode class=\"literal\"\u003eALL IN TABLESPACE\u003c/code\u003e form, which will lock all tables to be moved first and then move each one. This form also supports \u003ccode class=\"literal\"\u003eOWNED BY\u003c/code\u003e, which will only move tables owned by the roles specified. If the \u003ccode class=\"literal\"\u003eNOWAIT\u003c/code\u003e option is specified then the command will fail if it is unable to acquire all of the locks required immediately. Note that system catalogs are not moved by this command; use \u003ccode class=\"command\"\u003eALTER DATABASE\u003c/code\u003e or explicit \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e invocations instead if desired. The \u003ccode class=\"literal\"\u003einformation_schema\u003c/code\u003e relations are not considered part of the system catalogs and will be moved. See also \u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLESPACE\u003c/code\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET { LOGGED | UNLOGGED }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the table from unlogged to logged or vice-versa (see \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-UNLOGGED\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/a\u003e). It cannot be applied to a temporary table.\u003c/p\u003e\u003cp\u003eThis also changes the persistence of any sequences linked to the table (for identity or serial columns). However, it is also possible to change the persistence of such sequences separately.\u003c/p\u003e\u003cp\u003eThis form is not supported for partitioned tables.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes one or more storage parameters for the table. See \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"Storage Parameters\"\u003eStorage Parameters\u003c/a\u003e in the \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e documentation for details on the available parameters. Note that the table contents will not be modified immediately by this command; depending on the parameter you might need to rewrite the table to get the desired effects. That can be done with \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e\u003c/a\u003e, \u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e or one of the forms of \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e that forces a table rewrite. For planner related parameters, changes will take effect from the next time the table is locked so currently executing queries will not be affected.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock will be taken for fillfactor, toast and autovacuum storage parameters, as well as the planner parameter \u003ccode class=\"varname\"\u003eparallel_workers\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form resets one or more storage parameters to their defaults. As with \u003ccode class=\"literal\"\u003eSET\u003c/code\u003e, a table rewrite might be needed to update the table entirely.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form adds the target table as a new child of the specified parent table. Subsequently, queries against the parent will include records of the target table. To be added as a child, the target table must already contain all the same columns as the parent (it could have additional columns, too). The columns must have matching data types.\u003c/p\u003e\u003cp\u003eIn addition, all \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e and \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraints on the parent must also exist on the child, except those marked non-inheritable (that is, created with \u003ccode class=\"literal\"\u003eALTER TABLE ... ADD CONSTRAINT ... NO INHERIT\u003c/code\u003e), which are ignored. All child-table constraints matched must not be marked non-inheritable. Currently \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e constraints are not considered, but this might change in the future.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form removes the target table from the list of children of the specified parent table. Queries against the parent table will no longer include records drawn from the target table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form links the table to a composite type as though \u003ccode class=\"command\"\u003eCREATE TABLE OF\u003c/code\u003e had formed it. The table's list of column names and types must precisely match that of the composite type. The table must not inherit from any other table. These restrictions ensure that \u003ccode class=\"command\"\u003eCREATE TABLE OF\u003c/code\u003e would permit an equivalent table definition.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT OF\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form dissociates a typed table from its type.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOWNER TO\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the owner of the table, sequence, view, materialized view, or foreign table to the specified user.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eREPLICA IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form changes the information which is written to the write-ahead log to identify rows which are updated or deleted. In most cases, the old value of each column is only logged if it differs from the new value; however, if the old value is stored externally, it is always logged regardless of whether it changed. This option has no effect except when logical replication is in use.\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRecords the old values of the columns of the primary key. This is the default for non-system tables. When there is no primary key, the behavior is the same as \u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRecords the old values of the columns covered by the named index, that must be unique, not partial, not deferrable, and include only columns marked \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e. If this index is dropped, the behavior is the same as \u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRecords the old values of all columns in the row.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRecords no information about the old row. This is the default for system tables.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e forms change the name of a table (or an index, sequence, view, materialized view, or foreign table), the name of an individual column in a table, or the name of a constraint of the table. When renaming a constraint that has an underlying index, the index is renamed as well. There is no effect on the stored data.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET SCHEMA\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form moves the table into another schema. Associated indexes, constraints, and sequences owned by table columns are moved as well.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eATTACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form attaches an existing table (which might itself be partitioned) as a partition of the target table. The table can be attached as a partition for specific values using \u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e or as a default partition by using \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e. For each index in the target table, if a valid equivalent index already exists in the partition, it will be attached to the target table's index, as if ALTER INDEX ATTACH PARTITION had been executed; otherwise, a new corresponding index will be created. Invalid indexes on the partition are skipped. Note that if the existing table is a foreign table, it is currently not allowed to attach the table as a partition of the target table if there are \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e indexes on the target table. (See also \u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e.) For each user-defined row-level trigger that exists in the target table, a corresponding one is created in the attached table.\u003c/p\u003e\u003cp\u003eA partition using \u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e uses same syntax for \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e as \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e. The partition bound specification must correspond to the partitioning strategy and partition key of the target table. The table to be attached must have all the same columns as the target table and no more; moreover, the column types must also match. Also, it must have all the \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e and \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints of the target table, not marked \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e. Currently \u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e constraints are not considered. \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e and \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e constraints from the parent table will be created in the partition, if they don't already exist.\u003c/p\u003e\u003cp\u003eIf the new partition is a regular table, a full table scan is performed to check that existing rows in the table do not violate the partition constraint. It is possible to avoid this scan by adding a valid \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint to the table that allows only rows satisfying the desired partition constraint before running this command. The \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint will be used to determine that the table need not be scanned to validate the partition constraint. This does not work, however, if any of the partition keys is an expression and the partition does not accept \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e values. If attaching a list partition that will not accept \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e values, also add a \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint to the partition key column, unless it's an expression.\u003c/p\u003e\u003cp\u003eIf the new partition is a foreign table, nothing is done to verify that all the rows in the foreign table obey the partition constraint. (See the discussion in \u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e about constraints on the foreign table.)\u003c/p\u003e\u003cp\u003eWhen a table has a default partition, defining a new partition changes the partition constraint for the default partition. The default partition can't contain any rows that would need to be moved to the new partition, and will be scanned to verify that none are present. This scan, like the scan of the new partition, can be avoided if an appropriate \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint is present. Also like the scan of the new partition, it is always skipped when the default partition is a foreign table.\u003c/p\u003e\u003cp\u003eAttaching a partition acquires a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock on the parent table, in addition to the \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e locks on the table being attached and on the default partition (if any).\u003c/p\u003e\u003cp\u003eFurther locks must also be held on all sub-partitions if the table being attached is itself a partitioned table. Likewise if the default partition is itself a partitioned table. The locking of the sub-partitions can be avoided by adding a \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint as described in \u003ca href=\"/docs/18/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE\" title=\"5.12.2.2. Partition Maintenance\"\u003eSection 5.12.2.2\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDETACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e [ CONCURRENTLY | FINALIZE ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form detaches the specified partition of the target table. The detached partition continues to exist as a standalone table, but no longer has any ties to the table from which it was detached. Any indexes that were attached to the target table's indexes are detached. Any triggers that were created as clones of those in the target table are removed. \u003ccode class=\"literal\"\u003eSHARE\u003c/code\u003e lock is obtained on any tables that reference this partitioned table in foreign key constraints.\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003eCONCURRENTLY\u003c/code\u003e is specified, it runs using a reduced lock level to avoid blocking other sessions that might be accessing the partitioned table. In this mode, two transactions are used internally. During the first transaction, a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock is taken on both parent table and partition, and the partition is marked as undergoing detach; at that point, the transaction is committed and all other transactions using the partitioned table are waited for. Once all those transactions have completed, the second transaction acquires \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e on the partitioned table and \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e on the partition, and the detach process completes. A \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint that duplicates the partition constraint is added to the partition. \u003ccode class=\"literal\"\u003eCONCURRENTLY\u003c/code\u003e cannot be run in a transaction block and is not allowed if the partitioned table contains a default partition.\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003eFINALIZE\u003c/code\u003e is specified, a previous \u003ccode class=\"literal\"\u003eDETACH CONCURRENTLY\u003c/code\u003e invocation that was canceled or interrupted is completed. At most one partition in a partitioned table can be pending detach at a time.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eAll the forms of \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e that act on a single table, except \u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e, \u003ccode class=\"literal\"\u003eSET SCHEMA\u003c/code\u003e, \u003ccode class=\"literal\"\u003eATTACH PARTITION\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eDETACH PARTITION\u003c/code\u003e can be combined into a list of multiple alterations to be applied together. For example, it is possible to add several columns and/or alter the type of several columns in a single command. This is particularly useful with large tables, since only one pass over the table need be made.\u003c/p\u003e\u003cp\u003eYou must own the table to use \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e. To change the schema or tablespace of a table, you must also have \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privilege on the new schema or tablespace. To add the table as a new child of a parent table, you must own the parent table as well. Also, to attach a table as a new partition of the table, you must own the table being attached. To alter the owner, you must be able to \u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e to the new owning role, and that role must have \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privilege on the table's schema. (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the table. However, a superuser can alter ownership of any table anyway.) To add a column or alter a column type or use the \u003ccode class=\"literal\"\u003eOF\u003c/code\u003e clause, you must also have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on the data type.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDo not throw an error if the table does not exist. A notice is issued in this case.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name (optionally schema-qualified) of an existing table to alter. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is specified before the table name, only that table is altered. If \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is not specified, the table and all its descendant tables (if any) are altered. Optionally, \u003ccode class=\"literal\"\u003e*\u003c/code\u003e can be specified after the table name to explicitly indicate that descendant tables are included.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eName of a new or existing column.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_column_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNew name for an existing column.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNew name for the table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eData type of the new column, or new data type for an existing column.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNew table constraint for the table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eName of a new or existing constraint.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAutomatically drop objects that depend on the dropped column or constraint (for example, views referencing the column), and in turn all objects that depend on those objects (see \u003ca href=\"/docs/18/ddl-depend.html\" title=\"5.15. Dependency Tracking\"\u003eSection 5.15\u003c/a\u003e).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eRefuse to drop the column or constraint if there are any dependent objects. This is the default behavior.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eName of a single trigger to disable or enable.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDisable or enable all triggers belonging to the table. (This requires superuser privilege if any of the triggers are internally generated constraint triggers, such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints.)\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDisable or enable all triggers belonging to the table except for internally generated constraint triggers, such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of an existing index.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of a table storage parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe new value for a table storage parameter. This might be a number or a word depending on the parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eA parent table to associate or de-associate with this table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_owner\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe user name of the new owner of the table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_access_method\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the access method to which the table will be converted.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the tablespace to which the table will be moved.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_schema\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the schema to which the table will be moved.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the table to attach as a new partition or to detach from this table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe partition bound specification for a new partition. Refer to \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e for more details on the syntax of the same.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eThe key word \u003ccode class=\"literal\"\u003eCOLUMN\u003c/code\u003e is noise and can be omitted.\u003c/p\u003e\u003cp\u003eWhen a column is added with \u003ccode class=\"literal\"\u003eADD COLUMN\u003c/code\u003e and a non-volatile \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e is specified, the default value is evaluated at the time of the statement and the result stored in the table's metadata, where it will be returned when any existing rows are accessed. The value will be only applied when the table is rewritten, making the \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e very fast even on large tables. If no column constraints are specified, NULL is used as the \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e. In neither case is a rewrite of the table required.\u003c/p\u003e\u003cp\u003eAdding a column with a volatile \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e (e.g., \u003ccode class=\"function\"\u003eclock_timestamp()\u003c/code\u003e), a stored generated column, an identity column, or a column with a domain data type that has constraints will cause the entire table and its indexes to be rewritten. Adding a virtual generated column never requires a rewrite.\u003c/p\u003e\u003cp\u003eChanging the type of an existing column will normally cause the entire table and its indexes to be rewritten. As an exception, when changing the type of an existing column, if the \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause does not change the column contents and the old type is either binary coercible to the new type or an unconstrained domain over the new type, a table rewrite is not needed. However, indexes will still be rebuilt unless the system can verify that the new index would be logically equivalent to the existing one. For example, if the collation for a column has been changed, an index rebuild is required because the new sort order might be different. However, in the absence of a collation change, a column can be changed from \u003ccode class=\"type\"\u003etext\u003c/code\u003e to \u003ccode class=\"type\"\u003evarchar\u003c/code\u003e (or vice versa) without rebuilding the indexes because these data types sort identically.\u003c/p\u003e\u003cp\u003eTable and/or index rebuilds may take a significant amount of time for a large table, and will temporarily require as much as double the disk space.\u003c/p\u003e\u003cp\u003eAdding a \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e or \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint requires scanning the table to verify that existing rows meet the constraint, but does not require a table rewrite. If a \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint is added as \u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e, no verification will be performed.\u003c/p\u003e\u003cp\u003eSimilarly, when attaching a new partition it may be scanned to verify that existing rows meet the partition constraint.\u003c/p\u003e\u003cp\u003eThe main reason for providing the option to specify multiple changes in a single \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e is that multiple table scans or rewrites can thereby be combined into a single pass over the table.\u003c/p\u003e\u003cp\u003eScanning a large table to verify new foreign-key, check, or not-null constraints can take a long time, and other updates to the table are locked out until the \u003ccode class=\"command\"\u003eALTER TABLE ADD CONSTRAINT\u003c/code\u003e command is committed. The main purpose of the \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e constraint option is to reduce the impact of adding a constraint on concurrent updates. With \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e, the \u003ccode class=\"command\"\u003eADD CONSTRAINT\u003c/code\u003e command does not scan the table and can be committed immediately. After that, a \u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e command can be issued to verify that existing rows satisfy the constraint. The validation step does not need to lock out concurrent updates, since it knows that other transactions will be enforcing the constraint for rows that they insert or update; only pre-existing rows need to be checked. Hence, validation acquires only a \u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e lock on the table being altered. (If the constraint is a foreign key then a \u003ccode class=\"literal\"\u003eROW SHARE\u003c/code\u003e lock is also required on the table referenced by the constraint.) In addition to improving concurrency, it can be useful to use \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e and \u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e in cases where the table is known to contain pre-existing violations. Once the constraint is in place, no new violations can be inserted, and the existing problems can be corrected at leisure until \u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e finally succeeds.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e form does not physically remove the column, but simply makes it invisible to SQL operations. Subsequent insert and update operations in the table will store a null value for the column. Thus, dropping a column is quick but it will not immediately reduce the on-disk size of your table, as the space occupied by the dropped column is not reclaimed. The space will be reclaimed over time as existing rows are updated.\u003c/p\u003e\u003cp\u003eTo force immediate reclamation of space occupied by a dropped column, you can execute one of the forms of \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e that performs a rewrite of the whole table. This results in reconstructing each row with the dropped column replaced by a null value.\u003c/p\u003e\u003cp\u003eThe rewriting forms of \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e are not MVCC-safe. After a table rewrite, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the rewrite occurred. See \u003ca href=\"/docs/18/mvcc-caveats.html\" title=\"13.6. Caveats\"\u003eSection 13.6\u003c/a\u003e for more details.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e option of \u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e can actually specify any expression involving the old values of the row; that is, it can refer to other columns as well as the one being converted. This allows very general conversions to be done with the \u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e syntax. Because of this flexibility, the \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e expression is not applied to the column's default value (if any); the result might not be a constant expression as required for a default. This means that when there is no implicit or assignment cast from old to new type, \u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e might fail to convert the default even though a \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause is supplied. In such cases, drop the default with \u003ccode class=\"literal\"\u003eDROP DEFAULT\u003c/code\u003e, perform the \u003ccode class=\"literal\"\u003eALTER TYPE\u003c/code\u003e, and then use \u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e to add a suitable new default. Similar considerations apply to indexes and constraints involving the column.\u003c/p\u003e\u003cp\u003eIf a table has any descendant tables, it is not permitted to add, rename, or change the type of a column in the parent table without doing the same to the descendants. This ensures that the descendants always have columns matching the parent. Similarly, a \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint cannot be renamed in the parent without also renaming it in all descendants, so that \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints also match between the parent and its descendants. (That restriction does not apply to index-based constraints, however.) Also, because selecting from the parent also selects from its descendants, a constraint on the parent cannot be marked valid unless it is also marked valid for those descendants. In all of these cases, \u003ccode class=\"command\"\u003eALTER TABLE ONLY\u003c/code\u003e will be rejected.\u003c/p\u003e\u003cp\u003eA recursive \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e operation will remove a descendant table's column only if the descendant does not inherit that column from any other parents and never had an independent definition of the column. A nonrecursive \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e (i.e., \u003ccode class=\"command\"\u003eALTER TABLE ONLY ... DROP COLUMN\u003c/code\u003e) never removes any descendant columns, but instead marks them as independently defined rather than inherited. A nonrecursive \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e command will fail for a partitioned table, because all partitions of a table must have the same columns as the partitioning root.\u003c/p\u003e\u003cp\u003eThe actions for identity columns (\u003ccode class=\"literal\"\u003eADD GENERATED\u003c/code\u003e, \u003ccode class=\"literal\"\u003eSET\u003c/code\u003e etc., \u003ccode class=\"literal\"\u003eDROP IDENTITY\u003c/code\u003e), as well as the actions \u003ccode class=\"literal\"\u003eCLUSTER\u003c/code\u003e, \u003ccode class=\"literal\"\u003eOWNER\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e never recurse to descendant tables; that is, they always act as though \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e were specified. Actions affecting trigger states recurse to partitions of partitioned tables (unless \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e is specified), but never to traditional-inheritance descendants. Adding a constraint recurses only for \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints that are not marked \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eChanging any part of a system catalog table is not permitted.\u003c/p\u003e\u003cp\u003eRefer to \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e for a further description of valid parameters. \u003ca href=\"/docs/18/ddl.html\" title=\"Chapter 5. Data Definition\"\u003eChapter 5\u003c/a\u003e has further information on inheritance.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eTo add a column of type \u003ccode class=\"type\"\u003evarchar\u003c/code\u003e to a table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD COLUMN address varchar(30);\n\u003c/pre\u003e\u003cp\u003eThat will cause all existing rows in the table to be filled with null values for the new column.\u003c/p\u003e\u003cp\u003eTo add a column with a non-null default:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurements\n  ADD COLUMN mtime timestamp with time zone DEFAULT now();\n\u003c/pre\u003e\u003cp\u003eExisting rows will be filled with the current time as the value of the new column, and then new rows will receive the time of their insertion.\u003c/p\u003e\u003cp\u003eTo add a column and fill it with a value different from the default to be used later:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE transactions\n  ADD COLUMN status varchar(30) DEFAULT 'old',\n  ALTER COLUMN status SET default 'current';\n\u003c/pre\u003e\u003cp\u003eExisting rows will be filled with \u003ccode class=\"literal\"\u003eold\u003c/code\u003e, but then the default for subsequent commands will be \u003ccode class=\"literal\"\u003ecurrent\u003c/code\u003e. The effects are the same as if the two sub-commands had been issued in separate \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e commands.\u003c/p\u003e\u003cp\u003eTo drop a column from a table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors DROP COLUMN address RESTRICT;\n\u003c/pre\u003e\u003cp\u003eTo change the types of two existing columns in one operation:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors\n    ALTER COLUMN address TYPE varchar(80),\n    ALTER COLUMN name TYPE varchar(100);\n\u003c/pre\u003e\u003cp\u003eTo change an integer column containing Unix timestamps to \u003ccode class=\"type\"\u003etimestamp with time zone\u003c/code\u003e via a \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e clause:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone\n    USING\n        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';\n\u003c/pre\u003e\u003cp\u003eThe same, when the column has a default expression that won't automatically cast to the new data type:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp DROP DEFAULT,\n    ALTER COLUMN foo_timestamp TYPE timestamp with time zone\n    USING\n        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',\n    ALTER COLUMN foo_timestamp SET DEFAULT now();\n\u003c/pre\u003e\u003cp\u003eTo rename an existing column:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME COLUMN address TO city;\n\u003c/pre\u003e\u003cp\u003eTo rename an existing table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME TO suppliers;\n\u003c/pre\u003e\u003cp\u003eTo rename an existing constraint:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;\n\u003c/pre\u003e\u003cp\u003eTo add a not-null constraint to a column:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ALTER COLUMN street SET NOT NULL;\n\u003c/pre\u003e\u003cp\u003eTo remove a not-null constraint from a column:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;\n\u003c/pre\u003e\u003cp\u003eTo add a check constraint to a table and all its children:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);\n\u003c/pre\u003e\u003cp\u003eTo add a check constraint only to a table and not to its children:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;\n\u003c/pre\u003e\u003cp\u003e(The check constraint will not be inherited by future children, either.)\u003c/p\u003e\u003cp\u003eTo remove a check constraint from a table and all its children:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003eTo remove a check constraint from one table only:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003e(The check constraint remains in place for any child tables.)\u003c/p\u003e\u003cp\u003eTo add a foreign key constraint to a table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);\n\u003c/pre\u003e\u003cp\u003eTo add a foreign key constraint to a table with the least impact on other work:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;\nALTER TABLE distributors VALIDATE CONSTRAINT distfk;\n\u003c/pre\u003e\u003cp\u003eTo add a (multicolumn) unique constraint to a table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);\n\u003c/pre\u003e\u003cp\u003eTo add an automatically named primary key constraint to a table, noting that a table can only ever have one primary key:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD PRIMARY KEY (dist_id);\n\u003c/pre\u003e\u003cp\u003eTo move a table to a different tablespace:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors SET TABLESPACE fasttablespace;\n\u003c/pre\u003e\u003cp\u003eTo move a table to a different schema:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE myschema.distributors SET SCHEMA yourschema;\n\u003c/pre\u003e\u003cp\u003eTo recreate a primary key constraint, without blocking updates while the index is rebuilt:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);\nALTER TABLE distributors DROP CONSTRAINT distributors_pkey,\n    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;\n\u003c/pre\u003e\u003cp\u003eTo attach a partition to a range-partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurement\n    ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');\n\u003c/pre\u003e\u003cp\u003eTo attach a partition to a list-partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE cities\n    ATTACH PARTITION cities_ab FOR VALUES IN ('a', 'b');\n\u003c/pre\u003e\u003cp\u003eTo attach a partition to a hash-partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE orders\n    ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003eTo attach a default partition to a partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE cities\n    ATTACH PARTITION cities_partdef DEFAULT;\n\u003c/pre\u003e\u003cp\u003eTo detach a partition from a partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurement\n    DETACH PARTITION measurement_y2015m12;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe forms \u003ccode class=\"literal\"\u003eADD [COLUMN]\u003c/code\u003e, \u003ccode class=\"literal\"\u003eDROP [COLUMN]\u003c/code\u003e, \u003ccode class=\"literal\"\u003eDROP IDENTITY\u003c/code\u003e, \u003ccode class=\"literal\"\u003eRESTART\u003c/code\u003e, \u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e, \u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e (without \u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e), \u003ccode class=\"literal\"\u003eSET GENERATED\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e conform with the SQL standard. The form \u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e conforms with the SQL standard when the \u003ccode class=\"literal\"\u003eUSING INDEX\u003c/code\u003e and \u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e clauses are omitted and the constraint type is one of \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e, \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, or \u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e. The other forms are \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extensions of the SQL standard. Also, the ability to specify more than one manipulation in a single \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e command is an extension.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eALTER TABLE DROP COLUMN\u003c/code\u003e can be used to drop the only column of a table, leaving a zero-column table. This is an extension of SQL, which disallows zero-column tables.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"ALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    \u003cem class=\"replaceable\"\u003e\u003ccode\u003eaction\u003c/code\u003e\u003c/em\u003e [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    RENAME [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_column_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    RENAME CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_constraint_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    RENAME TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    SET SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_schema\u003c/code\u003e\u003c/em\u003e\nALTER TABLE ALL IN TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ OWNED BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_name\u003c/code\u003e\u003c/em\u003e [, ... ] ]\n    SET TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    ATTACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    DETACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e [ CONCURRENTLY | FINALIZE ]\n\n\u003cspan class=\"phrase\"\u003ewhere \u003cem class=\"replaceable\"\u003e\u003ccode\u003eaction\u003c/code\u003e\u003c/em\u003e is one of:\u003c/span\u003e\n\n    ADD [ COLUMN ] [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e \u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    DROP [ COLUMN ] [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ RESTRICT | CASCADE ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ SET DATA ] TYPE \u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET DEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP DEFAULT\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e { SET | DROP } NOT NULL\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET EXPRESSION AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP EXPRESSION [ IF EXISTS ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e { SET GENERATED { ALWAYS | BY DEFAULT } | SET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e | RESTART [ [ WITH ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003erestart\u003c/code\u003e\u003c/em\u003e ] } [...]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP IDENTITY [ IF EXISTS ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET STATISTICS { \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e RESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\n    ADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e [ NOT VALID ]\n    ADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e\n    ALTER CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n    ALTER CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ INHERIT | NO INHERIT ]\n    VALIDATE CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\n    DROP CONSTRAINT [ IF EXISTS ]  \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ RESTRICT | CASCADE ]\n    DISABLE TRIGGER [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e | ALL | USER ]\n    ENABLE TRIGGER [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e | ALL | USER ]\n    ENABLE REPLICA TRIGGER \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE ALWAYS TRIGGER \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\n    DISABLE RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE REPLICA RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE ALWAYS RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    DISABLE ROW LEVEL SECURITY\n    ENABLE ROW LEVEL SECURITY\n    FORCE ROW LEVEL SECURITY\n    NO FORCE ROW LEVEL SECURITY\n    CLUSTER ON \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\n    SET WITHOUT CLUSTER\n    SET WITHOUT OIDS\n    SET ACCESS METHOD { \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_access_method\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n    SET TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e\n    SET { LOGGED | UNLOGGED }\n    SET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\n    RESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\n    NO INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\n    OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\n    NOT OF\n    OWNER TO { \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_owner\u003c/code\u003e\u003c/em\u003e | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\n    REPLICA IDENTITY { DEFAULT | USING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e | FULL | NOTHING }\n\n\u003cspan class=\"phrase\"\u003eand \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\nIN ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e [, ...] ) |\nFROM ( { \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e | MINVALUE | MAXVALUE } [, ...] )\n  TO ( { \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS \u003cem class=\"replaceable\"\u003e\u003ccode\u003enumeric_literal\u003c/code\u003e\u003c/em\u003e, REMAINDER \u003cem class=\"replaceable\"\u003e\u003ccode\u003enumeric_literal\u003c/code\u003e\u003c/em\u003e )\n\n\u003cspan class=\"phrase\"\u003eand \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\n[ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n{ NOT NULL [ NO INHERIT ] |\n  NULL |\n  CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] |\n  DEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003edefault_expr\u003c/code\u003e\u003c/em\u003e |\n  GENERATED ALWAYS AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003egeneration_expr\u003c/code\u003e\u003c/em\u003e ) [ STORED | VIRTUAL ] |\n  GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ] |\n  UNIQUE [ NULLS [ NOT ] DISTINCT ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  PRIMARY KEY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  REFERENCES \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n    [ ON DELETE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n\u003cspan class=\"phrase\"\u003eand \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\n[ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n{ CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] |\n  NOT NULL \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ NO INHERIT ] |\n  UNIQUE [ NULLS [ NOT ] DISTINCT ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  PRIMARY KEY ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  EXCLUDE [ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e WITH \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoperator\u003c/code\u003e\u003c/em\u003e [, ... ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e [ WHERE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e ) ] |\n  FOREIGN KEY ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e ] ) REFERENCES \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e [, ... ]  [, PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ] ) ]\n    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n\u003cspan class=\"phrase\"\u003eand \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\n    [ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n    { UNIQUE | PRIMARY KEY } USING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\n    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\n\u003cspan class=\"phrase\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e in \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraints are:\u003c/span\u003e\n\n[ INCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\n[ WITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] ) ]\n[ USING INDEX TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\n\u003cspan class=\"phrase\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e in an \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraint is:\u003c/span\u003e\n\n{ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e | ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) } [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass_parameter\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\n\u003cspan class=\"phrase\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e in a \u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e/\u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e constraint is:\u003c/span\u003e\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] | SET DEFAULT [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] }","synopsis_text":"ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\naction [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME [ COLUMN ] column_name TO new_column_name\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME CONSTRAINT constraint_name TO new_constraint_name\nALTER TABLE [ IF EXISTS ] name\nRENAME TO new_name\nALTER TABLE [ IF EXISTS ] name\nSET SCHEMA new_schema\nALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]\nSET TABLESPACE new_tablespace [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] name\nATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }\nALTER TABLE [ IF EXISTS ] name\nDETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]\n\nwhere action is one of:\n\nADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]\nDROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]\nALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]\nALTER [ COLUMN ] column_name SET DEFAULT expression\nALTER [ COLUMN ] column_name DROP DEFAULT\nALTER [ COLUMN ] column_name { SET | DROP } NOT NULL\nALTER [ COLUMN ] column_name SET EXPRESSION AS ( expression )\nALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]\nALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]\nALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...]\nALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ]\nALTER [ COLUMN ] column_name SET STATISTICS { integer | DEFAULT }\nALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )\nALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )\nALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\nALTER [ COLUMN ] column_name SET COMPRESSION compression_method\nADD table_constraint [ NOT VALID ]\nADD table_constraint_using_index\nALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\nALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ]\nVALIDATE CONSTRAINT constraint_name\nDROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]\nDISABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE REPLICA TRIGGER trigger_name\nENABLE ALWAYS TRIGGER trigger_name\nDISABLE RULE rewrite_rule_name\nENABLE RULE rewrite_rule_name\nENABLE REPLICA RULE rewrite_rule_name\nENABLE ALWAYS RULE rewrite_rule_name\nDISABLE ROW LEVEL SECURITY\nENABLE ROW LEVEL SECURITY\nFORCE ROW LEVEL SECURITY\nNO FORCE ROW LEVEL SECURITY\nCLUSTER ON index_name\nSET WITHOUT CLUSTER\nSET WITHOUT OIDS\nSET ACCESS METHOD { new_access_method | DEFAULT }\nSET TABLESPACE new_tablespace\nSET { LOGGED | UNLOGGED }\nSET ( storage_parameter [= value] [, ... ] )\nRESET ( storage_parameter [, ... ] )\nINHERIT parent_table\nNO INHERIT parent_table\nOF type_name\nNOT OF\nOWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\nREPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }\n\nand partition_bound_spec is:\n\nIN ( partition_bound_expr [, ...] ) |\nFROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numeric_literal, REMAINDER numeric_literal )\n\nand column_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL [ NO INHERIT ] |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT default_expr |\nGENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |\nGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |\nPRIMARY KEY index_parameters |\nREFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\nand table_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ CHECK ( expression ) [ NO INHERIT ] |\nNOT NULL column_name [ NO INHERIT ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nPRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nEXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |\nFOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ]\n[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\nand table_constraint_using_index is:\n\n[ CONSTRAINT constraint_name ]\n{ UNIQUE | PRIMARY KEY } USING INDEX index_name\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\nindex_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints are:\n\n[ INCLUDE ( column_name [, ... ] ) ]\n[ WITH ( storage_parameter [= value] [, ... ] ) ]\n[ USING INDEX TABLESPACE tablespace_name ]\n\nexclude_element in an EXCLUDE constraint is:\n\n{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\nreferential_action in a FOREIGN KEY/REFERENCES constraint is:\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"alter-table","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"ALTER TABLE","Summary":"更改一个表的定义","BodyHTML":"\u003cpre\u003eALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\naction [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME [ COLUMN ] column_name TO new_column_name\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME CONSTRAINT constraint_name TO new_constraint_name\nALTER TABLE [ IF EXISTS ] name\nRENAME TO new_name\nALTER TABLE [ IF EXISTS ] name\nSET SCHEMA new_schema\nALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]\nSET TABLESPACE new_tablespace [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] name\nATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }\nALTER TABLE [ IF EXISTS ] name\nDETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]\n\n其中action为以下之一：\n\nADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]\nDROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]\nALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]\nALTER [ COLUMN ] column_name SET DEFAULT expression\nALTER [ COLUMN ] column_name DROP DEFAULT\nALTER [ COLUMN ] column_name { SET | DROP } NOT NULL\nALTER [ COLUMN ] column_name SET EXPRESSION AS ( expression )\nALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]\nALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]\nALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...]\nALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ]\nALTER [ COLUMN ] column_name SET STATISTICS { integer | DEFAULT }\nALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )\nALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )\nALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\nALTER [ COLUMN ] column_name SET COMPRESSION compression_method\nADD table_constraint [ NOT VALID ]\nADD table_constraint_using_index\nALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\nALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ]\nVALIDATE CONSTRAINT constraint_name\nDROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]\nDISABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE REPLICA TRIGGER trigger_name\nENABLE ALWAYS TRIGGER trigger_name\nDISABLE RULE rewrite_rule_name\nENABLE RULE rewrite_rule_name\nENABLE REPLICA RULE rewrite_rule_name\nENABLE ALWAYS RULE rewrite_rule_name\nDISABLE ROW LEVEL SECURITY\nENABLE ROW LEVEL SECURITY\nFORCE ROW LEVEL SECURITY\nNO FORCE ROW LEVEL SECURITY\nCLUSTER ON index_name\nSET WITHOUT CLUSTER\nSET WITHOUT OIDS\nSET ACCESS METHOD { new_access_method | DEFAULT }\nSET TABLESPACE new_tablespace\nSET { LOGGED | UNLOGGED }\nSET ( storage_parameter [= value] [, ... ] )\nRESET ( storage_parameter [, ... ] )\nINHERIT parent_table\nNO INHERIT parent_table\nOF type_name\nNOT OF\nOWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\nREPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }\n\n而partition_bound_spec为：\n\nIN ( partition_bound_expr [, ...] ) |\nFROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numeric_literal, REMAINDER numeric_literal )\n\n而column_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL [ NO INHERIT ] |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT default_expr |\nGENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |\nGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |\nPRIMARY KEY index_parameters |\nREFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n而table_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ CHECK ( expression ) [ NO INHERIT ] |\nNOT NULL column_name [ NO INHERIT ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nPRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nEXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |\nFOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ]\n[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n而table_constraint_using_index为：\n\n[ CONSTRAINT constraint_name ]\n{ UNIQUE | PRIMARY KEY } USING INDEX index_name\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\nUNIQUE、PRIMARY KEY和EXCLUDE约束中的index_parameters为：\n\n[ INCLUDE ( column_name [, ... ] ) ]\n[ WITH ( storage_parameter [= value] [, ... ] ) ]\n[ USING INDEX TABLESPACE tablespace_name ]\n\nEXCLUDE约束中的exclude_element为：\n\n{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\nFOREIGN KEY/REFERENCES约束中的referential_action为：\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eALTER TABLE\u003c/code\u003e更改现有表的定义。下面描述了若干子形式。请注意，各子形式所需的锁级别可能不同。除非另有明确说明，否则将获取\u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。给出多个子命令时，获取的锁将是其中任一子命令所需的最严格级别。\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eADD [ COLUMN ] [ IF NOT EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式使用与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e相同的语法向表中添加新列。如果指定了\u003ccode\u003eIF NOT EXISTS\u003c/code\u003e，且同名列已经存在，则不会报错。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDROP [ COLUMN ] [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式从表中删除一列。涉及该列的索引和表约束也会自动删除。如果删除该列会使某个引用它的多元统计信息只剩下一列数据，那么该统计信息也会被移除。如果表外有任何对象依赖于该列，例如外键引用或视图，你就需要指定\u003ccode\u003eCASCADE\u003c/code\u003e。如果指定了\u003ccode\u003eIF EXISTS\u003c/code\u003e而该列不存在，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET DATA TYPE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式更改表中某一列的类型。涉及该列的索引和简单表约束会通过重新解析最初提供的表达式，自动转换为使用新的列类型。可选的\u003ccode\u003eCOLLATE\u003c/code\u003e子句为新列指定排序规则；如果省略，则使用新列类型的默认排序规则。可选的\u003ccode\u003eUSING\u003c/code\u003e子句指定如何根据旧值计算新列的值；如果省略，则默认转换与从旧数据类型到新数据类型的赋值类型转换相同。如果从旧类型到新类型不存在隐式或赋值类型转换，则必须提供\u003ccode\u003eUSING\u003c/code\u003e子句。\u003c/p\u003e\u003cp\u003e当使用该形式时，列的统计信息会被删除，因此建议随后在该表上运行 \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\" rel=\"nofollow\"\u003e\u003ccode\u003eANALYZE\u003c/code\u003e\u003c/a\u003e。对于虚拟生成列，则不需要\u003ccode\u003eANALYZE\u003c/code\u003e，因为这类列从不具有统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET\u003c/code\u003e/\u003ccode\u003eDROP DEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式设置或移除列的默认值（其中移除等同于将默认值设为 NULL）。新的默认值只会应用于后续的\u003ccode\u003eINSERT\u003c/code\u003e或\u003ccode\u003eUPDATE\u003c/code\u003e命令；它不会导致表中已有的行发生变化。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET\u003c/code\u003e/\u003ccode\u003eDROP NOT NULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式改变列是被标记为允许空值，还是被标记为拒绝空值。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eSET NOT NULL\u003c/code\u003e只能应用于满足以下条件的列：表中没有任何记录在该列上包含\u003ccode\u003eNULL\u003c/code\u003e值。通常，系统会在执行\u003ccode\u003eALTER TABLE\u003c/code\u003e时通过扫描整个表来检查这一点，除非指定了\u003ccode\u003eNOT VALID\u003c/code\u003e；不过，如果已存在一个有效的\u003ccode\u003eCHECK\u003c/code\u003e约束（并且不会在同一命令中被删除）能够证明不可能存在\u003ccode\u003eNULL\u003c/code\u003e，则会跳过表扫描。如果一列带有一个无效的非空约束，\u003ccode\u003eSET NOT NULL\u003c/code\u003e会对其进行验证。\u003c/p\u003e\u003cp\u003e如果该表是一个分区，而该列在父表中被标记为\u003ccode\u003eNOT NULL\u003c/code\u003e，则不能对该列执行\u003ccode\u003eDROP NOT NULL\u003c/code\u003e。要从所有分区中删除\u003ccode\u003eNOT NULL\u003c/code\u003e约束，请在父表上执行\u003ccode\u003eDROP NOT NULL\u003c/code\u003e。即使父表上没有\u003ccode\u003eNOT NULL\u003c/code\u003e约束，也仍然可以根据需要在单独的分区上添加此类约束；也就是说，即使父表允许空值，子表仍可禁止空值，但反过来不行。也可以仅从父表中删除\u003ccode\u003eNOT NULL\u003c/code\u003e约束（即使用\u003ccode\u003eONLY\u003c/code\u003e），这样不会从子表中移除该约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET EXPRESSION AS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会替换生成列的表达式。存储生成列中的现有数据会被重写，此后的所有更改都会应用新的生成表达式。\u003c/p\u003e\u003cp\u003e当该形式用于存储生成列时，其统计信息会被删除，因此建议随后在该表上运行 \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\" rel=\"nofollow\"\u003e\u003ccode\u003eANALYZE\u003c/code\u003e\u003c/a\u003e。对于虚拟生成列，则不需要\u003ccode\u003eANALYZE\u003c/code\u003e，因为这类列从不具有统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDROP EXPRESSION [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会把一个存储生成列转换成普通基础列。列中的现有数据会被保留，但今后的更改将不再应用生成表达式。\u003c/p\u003e\u003cp\u003e目前该形式只支持存储生成列（不支持虚拟生成列）。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003eDROP EXPRESSION IF EXISTS\u003c/code\u003e，而该列不是生成列，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eSET GENERATED { ALWAYS | BY DEFAULT }\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eDROP IDENTITY [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式改变列是否为标识列，或者更改现有标识列的生成属性。详情见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e。与\u003ccode\u003eSET DEFAULT\u003c/code\u003e类似，这些形式只影响后续\u003ccode\u003eINSERT\u003c/code\u003e和\u003ccode\u003eUPDATE\u003c/code\u003e命令的行为；它们不会导致表中已有的行发生变化。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003eDROP IDENTITY IF EXISTS\u003c/code\u003e，而该列不是标识列，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET \u003cem\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eRESTART\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式修改现有标识列底层的序列。\u003cem\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e是\u003ca href=\"/docs/18/sql-altersequence.html\" title=\"ALTER SEQUENCE\" rel=\"nofollow\"\u003e\u003ccode\u003eALTER SEQUENCE\u003c/code\u003e\u003c/a\u003e支持的一个选项，例如\u003ccode\u003eINCREMENT BY\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET STATISTICS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式为后续\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\" rel=\"nofollow\"\u003e\u003ccode\u003eANALYZE\u003c/code\u003e\u003c/a\u003e操作设置列级统计信息收集目标。该目标可以设置在 0 到 10000 之间。将其设为\u003ccode\u003eDEFAULT\u003c/code\u003e可以恢复使用系统默认的统计目标（\u003ca href=\"/docs/18/runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET\" rel=\"nofollow\"\u003edefault_statistics_target\u003c/a\u003e）。（设为 -1 也是一种已废弃的写法，效果相同。）有关\u003cspan\u003ePostgreSQL\u003c/span\u003e查询规划器如何使用统计信息的更多信息，请参见\u003ca href=\"/docs/18/planner-stats.html\" rel=\"nofollow\"\u003e第 14.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eSET STATISTICS\u003c/code\u003e会获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET ( \u003cem\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e = \u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eRESET ( \u003cem\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置或重置每个属性的选项。目前定义的每属性选项只有\u003ccode\u003en_distinct\u003c/code\u003e和\u003ccode\u003en_distinct_inherited\u003c/code\u003e，它们会覆盖后续\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\" rel=\"nofollow\"\u003e\u003ccode\u003eANALYZE\u003c/code\u003e\u003c/a\u003e操作对非重复值数量的估计。\u003ccode\u003en_distinct\u003c/code\u003e影响表本身的统计信息，而\u003ccode\u003en_distinct_inherited\u003c/code\u003e影响为该表及其继承子表收集的统计信息，以及为分区表收集的统计信息。当指定值为正数时，查询规划器将假定该列恰好包含指定数量的非空非重复值。也可以通过使用小于 0 且大于等于 -1 的值来指定小数值。这会指示查询规划器通过将指定数字的绝对值乘以表中估计行数来估算非重复值的数量。例如，值为 -1 表示该列中的所有值都不同，而值为 -0.5 表示每个值平均出现两次。当表的大小随时间变化时，这会很有用。有关\u003cspan\u003ePostgreSQL\u003c/span\u003e查询规划器如何使用统计信息的更多信息，请参见\u003ca href=\"/docs/18/planner-stats.html\" rel=\"nofollow\"\u003e第 14.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e更改每个属性的选项会获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置列的存储模式。这控制该列的数据是行内保存还是保存在辅助\u003cacronym\u003eTOAST\u003c/acronym\u003e表中，以及数据是否应被压缩。\u003ccode\u003ePLAIN\u003c/code\u003e必须用于诸如\u003ccode\u003einteger\u003c/code\u003e之类的固定长度值，表示行内且不压缩。\u003ccode\u003eMAIN\u003c/code\u003e用于行内、可压缩的数据。\u003ccode\u003eEXTERNAL\u003c/code\u003e用于行外、不压缩的数据，而\u003ccode\u003eEXTENDED\u003c/code\u003e用于行外、已压缩的数据。写成\u003ccode\u003eDEFAULT\u003c/code\u003e会把存储模式设置为该列数据类型的默认模式。对于大多数支持非\u003ccode\u003ePLAIN\u003c/code\u003e存储的数据类型，默认模式是\u003ccode\u003eEXTENDED\u003c/code\u003e。使用\u003ccode\u003eEXTERNAL\u003c/code\u003e会让对非常大的\u003ccode\u003etext\u003c/code\u003e和\u003ccode\u003ebytea\u003c/code\u003e值执行子串操作更快，但代价是占用更多存储空间。请注意，\u003ccode\u003eALTER TABLE ... SET STORAGE\u003c/code\u003e本身不会改变表中的任何内容；它只是设定未来更新表时要采用的策略。更多信息见\u003ca href=\"/docs/18/storage-toast.html\" rel=\"nofollow\"\u003e第 66.2 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET COMPRESSION \u003cem\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置列的压缩方法，用来决定未来插入的值将如何被压缩（前提是存储模式允许压缩）。这不会导致表被重写，因此现有数据仍可能采用其他压缩方法。如果使用\u003cspan\u003epg_restore\u003c/span\u003e恢复该表，那么所有值都会按配置的压缩方法重新写入。然而，当数据是从另一个关系插入时（例如通过\u003ccode\u003eINSERT ... SELECT\u003c/code\u003e），源表中的值不一定会被去 TOAST 化，因此先前已压缩的数据可能会保留原有的压缩方法，而不是按目标列的压缩方法重新压缩。支持的压缩方法有\u003ccode\u003epglz\u003c/code\u003e和\u003ccode\u003elz4\u003c/code\u003e。（\u003ccode\u003elz4\u003c/code\u003e仅在使用\u003ccode\u003e--with-lz4\u003c/code\u003e构建\u003cspan\u003ePostgreSQL\u003c/span\u003e时才可用。）此外，\u003cem\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e还可以是\u003ccode\u003edefault\u003c/code\u003e，表示在数据插入时查询\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TOAST-COMPRESSION\" rel=\"nofollow\"\u003edefault_toast_compression\u003c/a\u003e设置，以决定要使用的方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eADD \u003cem\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e [ NOT VALID ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式使用与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e相同的约束语法向表中添加一个新约束，另外还支持选项\u003ccode\u003eNOT VALID\u003c/code\u003e；目前该选项只允许用于外键、\u003ccode\u003eCHECK\u003c/code\u003e以及非空约束。\u003c/p\u003e\u003cp\u003e通常，该形式会扫描表，以验证表中所有现有行都满足新约束。但如果使用了\u003ccode\u003eNOT VALID\u003c/code\u003e选项，就会跳过这一步可能非常耗时的扫描。该约束仍会对后续的插入或更新生效（也就是说，对于外键，除非被引用表中存在匹配行，否则操作会失败；对于检查约束，除非新行满足指定的检查条件，否则操作会失败）。但是，在通过\u003ccode\u003eVALIDATE CONSTRAINT\u003c/code\u003e选项完成验证之前，数据库不会假定该约束对表中的所有行都成立。请参见下面的\u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"注解\" rel=\"nofollow\"\u003e注解\u003c/a\u003e，了解使用\u003ccode\u003eNOT VALID\u003c/code\u003e选项的更多信息。\u003c/p\u003e\u003cp\u003e尽管大多数形式的\u003ccode\u003eADD \u003cem\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e需要\u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，\u003ccode\u003eADD FOREIGN KEY\u003c/code\u003e只需要\u003ccode\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。请注意，\u003ccode\u003eADD FOREIGN KEY\u003c/code\u003e除了在声明约束的表上获取锁之外，还会在被引用的表上获取\u003ccode\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e当向分区表添加唯一约束或主键约束时，还会受到附加限制；请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eADD \u003cem\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式基于一个现有唯一索引向表中添加新的\u003ccode\u003ePRIMARY KEY\u003c/code\u003e或\u003ccode\u003eUNIQUE\u003c/code\u003e约束。该约束将包含该索引的全部列。\u003c/p\u003e\u003cp\u003e该索引不能包含表达式列，也不能是部分索引。此外，它必须是使用默认排序顺序的 B-树索引。这些限制保证该索引等价于通过常规\u003ccode\u003eADD PRIMARY KEY\u003c/code\u003e或\u003ccode\u003eADD UNIQUE\u003c/code\u003e命令构建出来的索引。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003ePRIMARY KEY\u003c/code\u003e，而该索引的列尚未被标记为\u003ccode\u003eNOT NULL\u003c/code\u003e，则此命令会尝试对每个这样的列执行\u003ccode\u003eALTER COLUMN SET NOT NULL\u003c/code\u003e。这需要进行一次全表扫描，以验证这些列不包含空值。在其他情况下，这都是一个快速操作。\u003c/p\u003e\u003cp\u003e如果提供了约束名，则索引会被重命名以匹配该约束名；否则，约束将使用与索引相同的名称。\u003c/p\u003e\u003cp\u003e执行此命令后，该索引会被该约束\u003cspan\u003e“\u003cspan\u003e拥有\u003c/span\u003e”\u003c/span\u003e，就像它是通过常规\u003ccode\u003eADD PRIMARY KEY\u003c/code\u003e或\u003ccode\u003eADD UNIQUE\u003c/code\u003e命令构建出来的一样。特别是，删除该约束也会使该索引一并消失。\u003c/p\u003e\u003cp\u003e该形式目前不支持分区表。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e注意\u003c/h3\u003e\u003cp\u003e在需要添加新约束、但又不希望长时间阻塞表更新的场景中，使用现有索引添加约束会很有帮助。为此，可以先使用\u003ccode\u003eCREATE UNIQUE INDEX CONCURRENTLY\u003c/code\u003e创建索引，然后再用此语法把它转换成约束。参见下面的示例。\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eALTER CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式修改一个先前创建的约束的属性。目前只有外键约束可以用这种方式修改，但见下文。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eALTER CONSTRAINT ... INHERIT\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eALTER CONSTRAINT ... NO INHERIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式会修改一个可继承约束，使其变为不可继承，或者反过来。当前只有非空约束可以这样修改。除了改变该约束的可继承状态之外，在把一个不可继承约束标记为可继承、且该表存在子表时，系统还会向这些子表添加一个等价约束。如果在存在子表的表上把一个可继承约束标记为不可继承，则子表上的对应约束会被标记为不再继承，但不会被移除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eVALIDATE CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式通过扫描表来验证一个先前以\u003ccode\u003eNOT VALID\u003c/code\u003e创建的外键、检查或非空约束，以确保不存在任何不满足该约束的行。如果该约束被设置为\u003ccode\u003eNOT ENFORCED\u003c/code\u003e，则会引发错误。如果该约束已经被标记为有效，则不会发生任何事情。（关于此命令有何用途，请参见下面的\u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"注解\" rel=\"nofollow\"\u003e注解\u003c/a\u003e。）\u003c/p\u003e\u003cp\u003e此命令会获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDROP CONSTRAINT [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式删除表上指定的约束，以及支撑该约束的任何索引。如果指定了\u003ccode\u003eIF EXISTS\u003c/code\u003e而该约束不存在，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDISABLE\u003c/code\u003e/\u003ccode\u003eENABLE [ REPLICA | ALWAYS ] TRIGGER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式配置表上触发器的触发行为。被禁用的触发器仍为系统所知，但在其触发事件发生时不会执行。（对于延迟触发器，启用状态是在事件发生时检查，而不是在触发器函数实际执行时检查。）可以禁用或启用按名称指定的单个触发器、表上的全部触发器，或者仅用户触发器（此选项排除内部生成的约束触发器，例如用于实现外键约束或可延迟的唯一约束和排他约束的触发器）。禁用或启用内部生成的约束触发器需要超级用户权限；应谨慎执行，因为如果这些触发器不执行，就无法保证约束的完整性。\u003c/p\u003e\u003cp\u003e触发器的触发机制也受配置变量\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\" rel=\"nofollow\"\u003esession_replication_role\u003c/a\u003e的影响。普通启用的触发器（默认）会在复制角色为\u003cspan\u003e“\u003cspan\u003eorigin\u003c/span\u003e”\u003c/span\u003e（默认）或\u003cspan\u003e“\u003cspan\u003elocal\u003c/span\u003e”\u003c/span\u003e时触发。配置为\u003ccode\u003eENABLE REPLICA\u003c/code\u003e的触发器仅会在会话处于\u003cspan\u003e“\u003cspan\u003ereplica\u003c/span\u003e”\u003c/span\u003e模式时触发，而配置为\u003ccode\u003eENABLE ALWAYS\u003c/code\u003e的触发器无论当前复制角色为何都会触发。\u003c/p\u003e\u003cp\u003e这种机制的效果是，在默认配置下，触发器不会在副本上触发。这很有用，因为如果在源端使用触发器在表之间传播数据，那么复制系统也会复制这些传播后的数据；因此触发器不应在副本上再次触发，否则会导致重复。不过，如果触发器用于其他目的，例如生成外部告警，那么把它设置为\u003ccode\u003eENABLE ALWAYS\u003c/code\u003e、使其在副本上也触发，可能就是合适的做法。\u003c/p\u003e\u003cp\u003e当该命令应用于分区表时，分区中对应的克隆触发器的状态也会被更新，除非指定了\u003ccode\u003eONLY\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e此命令会获取一个\u003ccode\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDISABLE\u003c/code\u003e/\u003ccode\u003eENABLE [ REPLICA | ALWAYS ] RULE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式配置属于表的重写规则是否应用。禁用的规则仍然为系统所知，但在查询重写期间不会应用。语义与禁用/启用触发器相同。对于\u003ccode\u003eON SELECT\u003c/code\u003e规则，此配置将被忽略，这些规则始终会被应用，以确保视图在当前会话处于非默认复制角色时仍然正常工作。\u003c/p\u003e\u003cp\u003e规则触发机制也受配置变量\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\" rel=\"nofollow\"\u003esession_replication_role\u003c/a\u003e的影响，类似于上面描述的触发器。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDISABLE\u003c/code\u003e/\u003ccode\u003eENABLE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式控制属于该表的行安全性策略的应用。如果启用后该表没有任何策略，则会应用默认拒绝策略。请注意，即使行级安全已禁用，表上仍然可以存在策略。在这种情况下，这些策略\u003cspan\u003e\u003cem\u003e不会\u003c/em\u003e\u003c/span\u003e被应用，而是会被忽略。另请参阅\u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNO FORCE\u003c/code\u003e/\u003ccode\u003eFORCE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式控制当用户是表所有者时，该表的行安全性策略是否生效。如果启用，则当用户是表所有者时也会应用行安全性策略；如果禁用（默认值），则当用户是表所有者时不会应用行级安全。另请参阅\u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCLUSTER ON\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式选择未来\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\" rel=\"nofollow\"\u003e\u003ccode\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e操作的默认索引。它实际上并不会重新对表执行聚簇。\u003c/p\u003e\u003cp\u003e更改聚簇选项会获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET WITHOUT CLUSTER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式从表中移除最近一次使用的\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\" rel=\"nofollow\"\u003e\u003ccode\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e所用索引设置。这会影响将来那些未指定索引的聚簇操作。\u003c/p\u003e\u003cp\u003e更改聚簇选项会获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET WITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这是用于移除\u003ccode\u003eoid\u003c/code\u003e系统列的向后兼容语法。由于如今已无法再添加\u003ccode\u003eoid\u003c/code\u003e系统列，因此它实际上永远不会产生任何效果。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET ACCESS METHOD\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式通过使用指定的访问方法重写表来改变其访问方法；指定\u003ccode\u003eDEFAULT\u003c/code\u003e则会选择由配置参数\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\" rel=\"nofollow\"\u003edefault_table_access_method\u003c/a\u003e设定的访问方法。更多信息请参见\u003ca href=\"/docs/18/tableam.html\" rel=\"nofollow\"\u003e第 62 章\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e当应用于分区表时，没有数据需要重写，但之后创建的分区默认会使用给定的访问方法，除非被\u003ccode\u003eUSING\u003c/code\u003e子句覆盖。指定\u003ccode\u003eDEFAULT\u003c/code\u003e会移除先前设置的值，使未来的分区重新默认使用\u003ccode\u003edefault_table_access_method\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET TABLESPACE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表的表空间更改为指定的表空间，并将与该表关联的数据文件移动到新的表空间。表上的索引（如果有）不会被移动，但可以通过额外的\u003ccode\u003eSET TABLESPACE\u003c/code\u003e命令单独移动。当应用于分区表时，不会移动任何内容，但之后通过\u003ccode\u003eCREATE TABLE PARTITION OF\u003c/code\u003e创建的分区会使用该表空间，除非被\u003ccode\u003eTABLESPACE\u003c/code\u003e子句覆盖。\u003c/p\u003e\u003cp\u003e使用\u003ccode\u003eALL IN TABLESPACE\u003c/code\u003e形式可以移动当前数据库中位于某个表空间中的所有表；该形式会先锁定所有待移动表，然后逐个移动。该形式还支持\u003ccode\u003eOWNED BY\u003c/code\u003e，从而只移动指定角色拥有的表。如果指定了\u003ccode\u003eNOWAIT\u003c/code\u003e，则一旦无法立即获取所需的全部锁，命令就会失败。请注意，系统目录不会通过此命令移动；如果需要，可改用\u003ccode\u003eALTER DATABASE\u003c/code\u003e或显式的\u003ccode\u003eALTER TABLE\u003c/code\u003e调用。\u003ccode\u003einformation_schema\u003c/code\u003e中的关系不被视为系统目录的一部分，因此会被移动。另请参阅\u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLESPACE\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET { LOGGED | UNLOGGED }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表从不记录 WAL 改为记录 WAL，或反过来（参见\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-UNLOGGED\" rel=\"nofollow\"\u003e\u003ccode\u003eUNLOGGED\u003c/code\u003e\u003c/a\u003e）。它不能应用于临时表。\u003c/p\u003e\u003cp\u003e这也会改变与该表关联的任何序列（用于标识列或 serial 列）的持久性。不过，也可以单独更改这些序列的持久性。\u003c/p\u003e\u003cp\u003e该形式不支持分区表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET ( \u003cem\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式更改表的一个或多个存储参数。可用参数的详细信息，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e文档中的\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"存储参数\" rel=\"nofollow\"\u003eStorage Parameters\u003c/a\u003e。请注意，此命令不会立即修改表内容；根据参数不同，你可能需要重写表才能获得预期效果。这可以通过\u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\" rel=\"nofollow\"\u003e\u003ccode\u003eVACUUM FULL\u003c/code\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\" rel=\"nofollow\"\u003e\u003ccode\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e，或者任何会强制重写表的\u003ccode\u003eALTER TABLE\u003c/code\u003e形式来完成。对于与规划器相关的参数，更改会从下一次锁定该表时起生效，因此当前正在执行的查询不会受到影响。\u003c/p\u003e\u003cp\u003e对于 fillfactor、toast 和 autovacuum 存储参数，以及规划器参数\u003ccode\u003eparallel_workers\u003c/code\u003e，会获取\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eRESET ( \u003cem\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把一个或多个存储参数重置为默认值。与\u003ccode\u003eSET\u003c/code\u003e一样，可能仍需要进行表重写才能让整张表完全更新。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINHERIT \u003cem\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把目标表添加为指定父表的新子表。此后，对父表的查询将包含目标表中的记录。要被添加为子表，目标表必须已经包含父表的所有同名列（也可以再有额外列）。这些列的数据类型必须匹配。\u003c/p\u003e\u003cp\u003e此外，父表上的所有\u003ccode\u003eCHECK\u003c/code\u003e和\u003ccode\u003eNOT NULL\u003c/code\u003e约束也都必须存在于子表上，但被标记为不可继承的约束（即使用\u003ccode\u003eALTER TABLE ... ADD CONSTRAINT ... NO INHERIT\u003c/code\u003e创建的约束）除外，这些约束会被忽略。所有与之匹配的子表约束都不能被标记为不可继承。目前不会考虑\u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e和\u003ccode\u003eFOREIGN KEY\u003c/code\u003e约束，但将来这可能会改变。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNO INHERIT \u003cem\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把目标表从指定父表的子表列表中移除。对父表的查询将不再包含来自目标表的记录。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eOF \u003cem\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表关联到一个复合类型，就好像它是通过\u003ccode\u003eCREATE TABLE OF\u003c/code\u003e创建的一样。表的列名和类型列表必须与该复合类型完全一致。该表还必须不继承自任何其他表。这些限制保证了\u003ccode\u003eCREATE TABLE OF\u003c/code\u003e会允许一个等价的表定义。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNOT OF\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会解除类型化表与其类型之间的关联。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eOWNER TO\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表、序列、视图、物化视图或外部表的所有者更改为指定用户。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eREPLICA IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式修改写入预写式日志的信息，以标识被更新或删除的行。在大多数情况下，只有当某列的旧值与新值不同，才记录该列的旧值；不过，如果旧值是外部存储的，则无论它是否改变，都会被记录。除非正在使用逻辑复制，否则该选项没有效果。\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录主键列的旧值。这是非系统表的默认值。如果没有主键，其行为与\u003ccode\u003eNOTHING\u003c/code\u003e相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUSING INDEX \u003cem\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录由指定索引覆盖的列的旧值。该索引必须是唯一的、非部分的、不可延迟的，并且只包含被标记为\u003ccode\u003eNOT NULL\u003c/code\u003e的列。如果该索引被删除，其行为与\u003ccode\u003eNOTHING\u003c/code\u003e相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录该行中所有列的旧值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNOTHING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e不记录关于旧行的任何信息。这是系统表的默认值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eRENAME\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eRENAME\u003c/code\u003e形式用于更改表（或索引、序列、视图、物化视图或外部表）的名称、表中某个列的名称，或者表上某个约束的名称。重命名一个带有底层索引的约束时，该索引也会一并重命名。已存储的数据不会受到影响。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET SCHEMA\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表移动到另一个模式。相关索引、约束以及由表列拥有的序列也会一并移动。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eATTACH PARTITION \u003cem\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e { FOR VALUES \u003cem\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把一个现有表（它本身也可能已分区）附加为目标表的一个分区。可以使用\u003ccode\u003eFOR VALUES\u003c/code\u003e把它附加为特定值的分区，也可以使用\u003ccode\u003eDEFAULT\u003c/code\u003e把它附加为默认分区。对于目标表中的每个索引，如果分区中已经存在有效的等价索引，则会把它附加到目标表的索引上，就像执行了 ALTER INDEX ATTACH PARTITION 一样；否则，将创建一个新的对应索引。分区上的无效索引会被跳过。请注意，如果现有表是外部表，而目标表上存在\u003ccode\u003eUNIQUE\u003c/code\u003e索引，则当前不允许把该表附加为目标表的分区。（另见\u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e。）对于目标表中存在的每个用户定义行级触发器，也都会在被附加表中创建一个对应触发器。\u003c/p\u003e\u003cp\u003e使用\u003ccode\u003eFOR VALUES\u003c/code\u003e的分区，其\u003cem\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e语法与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e中的相同。分区边界说明必须与目标表的分区策略和分区键相对应。待附加的表必须拥有与目标表完全相同且不多不少的列；此外，列类型也必须匹配。它还必须具有目标表上所有未标记为\u003ccode\u003eNO INHERIT\u003c/code\u003e的\u003ccode\u003eNOT NULL\u003c/code\u003e和\u003ccode\u003eCHECK\u003c/code\u003e约束。目前不会考虑\u003ccode\u003eFOREIGN KEY\u003c/code\u003e约束。如果父表中的\u003ccode\u003eUNIQUE\u003c/code\u003e和\u003ccode\u003ePRIMARY KEY\u003c/code\u003e约束尚不存在于该分区中，则会在该分区上创建这些约束。\u003c/p\u003e\u003cp\u003e如果新分区是普通表，则会执行一次全表扫描，以检查表中现有行不会违反分区约束。可以在执行此命令之前，先向该表添加一个有效的\u003ccode\u003eCHECK\u003c/code\u003e约束，使其只允许满足所需分区约束的行，从而避免这次扫描。系统会利用该\u003ccode\u003eCHECK\u003c/code\u003e约束来判断无须扫描该表以验证分区约束。不过，如果分区键中有任何一个是表达式，而该分区又不接受\u003ccode\u003eNULL\u003c/code\u003e值，则这一方法无效。如果附加的是一个不接受\u003ccode\u003eNULL\u003c/code\u003e值的列表分区，还应向分区键列添加\u003ccode\u003eNOT NULL\u003c/code\u003e约束，除非该分区键是表达式。\u003c/p\u003e\u003cp\u003e如果新分区是外部表，则不会执行任何检查来验证该外部表中的所有行都满足分区约束。（关于外部表上的约束，请参见\u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e中的讨论。）\u003c/p\u003e\u003cp\u003e当某个表具有默认分区时，定义一个新分区会改变默认分区的分区约束。默认分区不能包含任何本应移动到新分区中的行，因此系统会扫描默认分区以确认不存在此类行。和扫描新分区一样，如果存在适当的\u003ccode\u003eCHECK\u003c/code\u003e约束，也可以避免这次扫描；同样地，当默认分区是外部表时，这次扫描总会被跳过。\u003c/p\u003e\u003cp\u003e附加分区会在父表上获取一个\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁，此外还会在被附加的表以及默认分区（如果有）上获取\u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e如果被附加的表本身是分区表，则还必须在其所有子分区上持有锁；如果默认分区本身也是分区表，也是如此。添加\u003ccode\u003eCHECK\u003c/code\u003e约束（如\u003ca href=\"/docs/18/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE\" rel=\"nofollow\"\u003e第 5.12.2.2 节\u003c/a\u003e所述）可以避免对子分区加锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDETACH PARTITION \u003cem\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e [ CONCURRENTLY | FINALIZE ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会把目标表的指定分区分离出来。被分离的分区会继续作为一张独立表存在，但不再与原先所在的表保持任何关联。任何附加到目标表索引上的索引都会被分离。任何作为目标表中触发器克隆而创建的触发器都会被移除。对于任何在外键约束中引用该父分区表的表，都会获取\u003ccode\u003eSHARE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003eCONCURRENTLY\u003c/code\u003e，它将使用较低的锁级别运行，以避免阻塞其他可能正在访问该分区表的会话。在这种模式下，内部会使用两个事务。第一个事务中，会在父表和该分区上都获取\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁，并把该分区标记为正在分离；随后提交事务，并等待所有其他正在使用该分区表的事务结束。在所有这些事务完成后，第二个事务会在分区表上获取\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁、在分区上获取\u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，并完成分离过程。系统还会向该分区添加一个与分区约束等价的\u003ccode\u003eCHECK\u003c/code\u003e约束。\u003ccode\u003eCONCURRENTLY\u003c/code\u003e不能在事务块中运行；如果该分区表包含默认分区，也不允许使用它。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003eFINALIZE\u003c/code\u003e，则会完成此前被取消或中断的\u003ccode\u003eDETACH CONCURRENTLY\u003c/code\u003e调用。在任意时刻，一个分区表中最多只能有一个分区处于等待分离完成的状态。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e所有作用于单个表的 \u003ccode\u003eALTER TABLE\u003c/code\u003e 形式，除\u003ccode\u003eRENAME\u003c/code\u003e、\u003ccode\u003eSET SCHEMA\u003c/code\u003e、\u003ccode\u003eATTACH PARTITION\u003c/code\u003e和\u003ccode\u003eDETACH PARTITION\u003c/code\u003e外，都可以组合成一个多项修改列表，一并执行。例如，可以在一条命令中添加多个列并且/或者修改多个列的类型。对于大型表，这一点尤其有用，因为这样只需对表执行一次遍历。\u003c/p\u003e\u003cp\u003e要使用\u003ccode\u003eALTER TABLE\u003c/code\u003e，你必须拥有该表。要更改表的模式或表空间，你还必须在新模式或新表空间上拥有\u003ccode\u003eCREATE\u003c/code\u003e权限。要把一个表作为父表的新子表加入，你还必须拥有该父表。此外，要把一个表附加为另一个表的新分区，你必须拥有被附加的表。要更改所有者，你必须能够对新的所有者角色执行\u003ccode\u003eSET ROLE\u003c/code\u003e，并且该角色必须在该表所在模式上具有\u003ccode\u003eCREATE\u003c/code\u003e权限。（这些限制确保更改所有者不会做出任何你不能通过删除并重建该表来完成的事情。不过，超级用户无论如何都可以更改任何表的所有权。）要添加列、修改列类型或使用\u003ccode\u003eOF\u003c/code\u003e子句，你还必须在相应数据类型上具有\u003ccode\u003eUSAGE\u003c/code\u003e权限。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eIF EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果表不存在，则不要抛出错误。这种情况下会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要修改的一个现有表的名称（可以是模式限定的）。如果在表名前指定了 \u003ccode\u003eONLY\u003c/code\u003e，则只会修改该表。如果没有指定\u003ccode\u003eONLY\u003c/code\u003e，该表及其所有后代表（如果有）都会被修改。可选地，在表名后面可以指定 \u003ccode\u003e*\u003c/code\u003e用来显式地指示包括后代表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新列或现有列的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_column_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e现有列的新名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e表的新名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新列的数据类型，或现有列的新数据类型。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表的新的表约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个新约束或者现有约束的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动删除依赖于被删除列或约束的对象（例如引用该列的视图），以及所有进一步依赖于这些对象的对象（见\u003ca href=\"/docs/18/ddl-depend.html\" rel=\"nofollow\"\u003e第 5.15 节\u003c/a\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果存在任何依赖对象，则拒绝删除该列或约束。这是默认行为。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要禁用或启用的单个触发器的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e禁用或启用属于表的所有触发器。（如果任何触发器是内部生成的约束触发器，例如用于实现外键约束或可延迟唯一约束和排他约束的触发器，则需要超级用户权限。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUSER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e禁用或启用属于表的所有触发器，但不包括内部生成的约束触发器（例如用于实现外键约束或可延迟的唯一约束和排他约束的触发器）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个现有索引的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个表存储参数的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个表存储参数的新值。根据该参数，该值可能是一个数字或者一个词。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要与这个表关联或者解除关联的父表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_owner\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表的新拥有者的用户名。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_access_method\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要将表转换到的访问方法名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要把该表移入其中的表空间的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_schema\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表将被移动到的模式名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要附加为该表的新分区或从该表分离出去的表名。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新分区的分区边界说明。关于该语法的更多细节，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e关键字\u003ccode\u003eCOLUMN\u003c/code\u003e不影响语义，可以省略。\u003c/p\u003e\u003cp\u003e当使用\u003ccode\u003eADD COLUMN\u003c/code\u003e添加一列并指定了一个非易失性\u003ccode\u003eDEFAULT\u003c/code\u003e时，默认值会在该语句执行时计算，其结果会保存在表的元数据中；访问任何现有行时都会返回该值。只有在表被重写时，这个值才会真正应用到行中，因此即使面对大表，\u003ccode\u003eALTER TABLE\u003c/code\u003e也会非常快。如果没有指定列约束，则会使用 NULL 作为\u003ccode\u003eDEFAULT\u003c/code\u003e。在这两种情况下都不需要重写表。\u003c/p\u003e\u003cp\u003e添加带有易失性\u003ccode\u003eDEFAULT\u003c/code\u003e（例如\u003ccode\u003eclock_timestamp()\u003c/code\u003e）的列、存储生成列、标识列，或者数据类型为带约束域的列，都会导致整个表及其索引被重写。添加虚拟生成列则永远不需要重写。\u003c/p\u003e\u003cp\u003e更改现有列的类型通常会导致整个表及其索引被重写。作为例外，如果更改现有列的类型时，\u003ccode\u003eUSING\u003c/code\u003e子句并未改变列内容，且旧类型要么可以二进制强制转换为新类型，要么是基于新类型的无约束域，则不需要重写表。不过，除非系统能够验证新索引在逻辑上与现有索引等价，否则索引仍会被重建。例如，如果更改了列的排序规则，就必须重建索引，因为新的排序顺序可能不同。然而，在没有改变排序规则的情况下，可以在不重建索引的前提下将列从\u003ccode\u003etext\u003c/code\u003e改为\u003ccode\u003evarchar\u003c/code\u003e（反之亦然），因为这两种数据类型的排序方式相同。\u003c/p\u003e\u003cp\u003e对于大表，表和/或索引重建可能会耗费大量时间，并且临时需要多达两倍的磁盘空间。\u003c/p\u003e\u003cp\u003e添加\u003ccode\u003eCHECK\u003c/code\u003e或\u003ccode\u003eNOT NULL\u003c/code\u003e约束需要扫描表，以验证现有行满足该约束，但不需要重写表。如果把\u003ccode\u003eCHECK\u003c/code\u003e约束添加为\u003ccode\u003eNOT ENFORCED\u003c/code\u003e，则不会执行此验证。\u003c/p\u003e\u003cp\u003e类似地，在附加新分区时，也可能会扫描该分区以验证现有行满足分区约束。\u003c/p\u003e\u003cp\u003e在单个\u003ccode\u003eALTER TABLE\u003c/code\u003e中允许指定多个更改的主要原因，是可以把多次表扫描或重写合并为对表的一次遍历。\u003c/p\u003e\u003cp\u003e扫描大表以验证新的外键、检查或非空约束可能需要很长时间，并且在\u003ccode\u003eALTER TABLE ADD CONSTRAINT\u003c/code\u003e命令提交之前，会阻止对该表的其他更新。\u003ccode\u003eNOT VALID\u003c/code\u003e约束选项的主要目的，是减小添加约束对并发更新的影响。使用\u003ccode\u003eNOT VALID\u003c/code\u003e时，\u003ccode\u003eADD CONSTRAINT\u003c/code\u003e命令不会扫描表，因此可以立即提交。之后可以发出\u003ccode\u003eVALIDATE CONSTRAINT\u003c/code\u003e命令，以验证现有行满足该约束。验证步骤不需要阻止并发更新，因为它知道其他事务会对它们插入或更新的行强制执行该约束；只需检查预先存在的行。因此，验证只会在被修改的表上获取\u003ccode\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。（如果约束是外键，则被该约束引用的表上还需要\u003ccode\u003eROW SHARE\u003c/code\u003e锁。）除了改善并发性之外，在已知该表包含既有违规数据的情况下，\u003ccode\u003eNOT VALID\u003c/code\u003e和\u003ccode\u003eVALIDATE CONSTRAINT\u003c/code\u003e也很有用。一旦约束已经建立，就不能再插入新的违规数据，而现有问题则可以从容修正，直到\u003ccode\u003eVALIDATE CONSTRAINT\u003c/code\u003e最终成功。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eDROP COLUMN\u003c/code\u003e形式不会在物理上移除该列，而只是让它对 SQL 操作不可见。此后，对该表的插入和更新操作会为该列存储一个空值。因此，删除一列虽然很快，但不会立刻减少表占用的磁盘空间，因为被删除列所占用的空间尚未被回收。随着现有行被更新，这些空间会逐渐被回收。\u003c/p\u003e\u003cp\u003e若要强制立即回收已删除列所占的空间，可以执行任何一种会导致整表重写的\u003ccode\u003eALTER TABLE\u003c/code\u003e形式。这样会重建每一行，并用空值替换被删除的列。\u003c/p\u003e\u003cp\u003e会重写表的\u003ccode\u003eALTER TABLE\u003c/code\u003e形式对于 MVCC 来说并不安全。表重写完成后，如果并发事务使用的是在重写发生之前取得的快照，那么该表在这些并发事务看来会像一张空表。详见\u003ca href=\"/docs/18/mvcc-caveats.html\" rel=\"nofollow\"\u003e第 13.6 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eSET DATA TYPE\u003c/code\u003e的\u003ccode\u003eUSING\u003c/code\u003e选项实际上可以指定任何涉及该行旧值的表达式；也就是说，它既可以引用正在转换的列，也可以引用其他列。这使得使用\u003ccode\u003eSET DATA TYPE\u003c/code\u003e语法完成非常通用的转换成为可能。正因为这种灵活性，\u003ccode\u003eUSING\u003c/code\u003e表达式不会应用到列的默认值（如果有）上，因为其结果可能不是默认值所要求的常量表达式。这意味着，当从旧类型到新类型不存在隐式或赋值类型转换时，即便提供了\u003ccode\u003eUSING\u003c/code\u003e子句，\u003ccode\u003eSET DATA TYPE\u003c/code\u003e也可能仍然无法转换默认值。在这种情况下，可以先用\u003ccode\u003eDROP DEFAULT\u003c/code\u003e删除默认值，执行\u003ccode\u003eALTER TYPE\u003c/code\u003e，然后再用\u003ccode\u003eSET DEFAULT\u003c/code\u003e添加一个合适的新默认值。类似的考虑也适用于涉及该列的索引和约束。\u003c/p\u003e\u003cp\u003e如果某个表有任何后代表，那么在不对后代表执行相同操作的情况下，就不允许在父表中添加列、重命名列或更改列类型。这保证了后代表始终拥有与父表相匹配的列。类似地，如果不同时重命名所有后代上的\u003ccode\u003eCHECK\u003c/code\u003e约束，就不能只在父表上重命名该检查约束，这样\u003ccode\u003eCHECK\u003c/code\u003e约束才能在父表及其后代之间保持匹配。（不过，这一限制不适用于基于索引的约束。）此外，由于查询父表时也会同时查询其后代，父表上的约束除非在这些后代上也被标记为有效，否则就不能被标记为有效。在所有这些情况下，\u003ccode\u003eALTER TABLE ONLY\u003c/code\u003e都会被拒绝。\u003c/p\u003e\u003cp\u003e只有当某个后代表中的列既不是从其他父表继承而来，也从未有过该列的独立定义时，递归的\u003ccode\u003eDROP COLUMN\u003c/code\u003e操作才会移除该后代表中的此列。非递归的\u003ccode\u003eDROP COLUMN\u003c/code\u003e（即\u003ccode\u003eALTER TABLE ONLY ... DROP COLUMN\u003c/code\u003e）永远不会移除任何后代列，而只会把它们标记为独立定义，而非继承得到。对于分区表，非递归的\u003ccode\u003eDROP COLUMN\u003c/code\u003e命令会失败，因为一张表的所有分区都必须与分区根表拥有相同的列。\u003c/p\u003e\u003cp\u003e对于标识列的操作（\u003ccode\u003eADD GENERATED\u003c/code\u003e、\u003ccode\u003eSET\u003c/code\u003e 等、\u003ccode\u003eDROP IDENTITY\u003c/code\u003e），以及\u003ccode\u003eCLUSTER\u003c/code\u003e、\u003ccode\u003eOWNER\u003c/code\u003e和\u003ccode\u003eTABLESPACE\u003c/code\u003e等操作，永远不会递归到后代表；也就是说，它们总是像指定了\u003ccode\u003eONLY\u003c/code\u003e一样执行。影响触发器状态的操作会递归到分区表的各个分区（除非指定了\u003ccode\u003eONLY\u003c/code\u003e），但永远不会递归到传统继承体系中的后代表。添加约束时，只有未标记为\u003ccode\u003eNO INHERIT\u003c/code\u003e的\u003ccode\u003eCHECK\u003c/code\u003e约束才会递归下去。\u003c/p\u003e\u003cp\u003e不允许更改系统目录表的任何部分。\u003c/p\u003e\u003cp\u003e有关有效参数的进一步说明，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003ca href=\"/docs/18/ddl.html\" rel=\"nofollow\"\u003e第 5 章\u003c/a\u003e中还有关于继承的更多信息。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e要向表中添加一个\u003ccode\u003evarchar\u003c/code\u003e类型的列：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD COLUMN address varchar(30);\n\u003c/pre\u003e\u003cp\u003e这会使表中所有现有行的新列都填入空值。\u003c/p\u003e\u003cp\u003e要添加一个带非空默认值的列：\u003c/p\u003e\u003cpre\u003eALTER TABLE measurements\n  ADD COLUMN mtime timestamp with time zone DEFAULT now();\n\u003c/pre\u003e\u003cp\u003e现有行会以当前时间作为新列的值填充，之后新行会接收其插入时的时间。\u003c/p\u003e\u003cp\u003e要添加一列，并先用与之后默认值不同的值填充它：\u003c/p\u003e\u003cpre\u003eALTER TABLE transactions\n  ADD COLUMN status varchar(30) DEFAULT \u0026#39;old\u0026#39;,\n  ALTER COLUMN status SET default \u0026#39;current\u0026#39;;\n\u003c/pre\u003e\u003cp\u003e现有行会填入\u003ccode\u003eold\u003c/code\u003e，而后续命令的默认值将是\u003ccode\u003ecurrent\u003c/code\u003e。其效果与在两条单独的\u003ccode\u003eALTER TABLE\u003c/code\u003e命令中发出这两个子命令相同。\u003c/p\u003e\u003cp\u003e要从表中删除一列：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors DROP COLUMN address RESTRICT;\n\u003c/pre\u003e\u003cp\u003e要在一个操作中更改两个现有列的类型：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors\n    ALTER COLUMN address TYPE varchar(80),\n    ALTER COLUMN name TYPE varchar(100);\n\u003c/pre\u003e\u003cp\u003e要把一个包含 Unix 时间戳的整数列改为 \u003ccode\u003etimestamp with time zone\u003c/code\u003e，并通过\u003ccode\u003eUSING\u003c/code\u003e子句完成转换：\u003c/p\u003e\u003cpre\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone\n    USING\n        timestamp with time zone \u0026#39;epoch\u0026#39; + foo_timestamp * interval \u0026#39;1 second\u0026#39;;\n\u003c/pre\u003e\u003cp\u003e如果该列带有一个不能自动转换为新数据类型的默认值表达式，也是同样的做法：\u003c/p\u003e\u003cpre\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp DROP DEFAULT,\n    ALTER COLUMN foo_timestamp TYPE timestamp with time zone\n    USING\n        timestamp with time zone \u0026#39;epoch\u0026#39; + foo_timestamp * interval \u0026#39;1 second\u0026#39;,\n    ALTER COLUMN foo_timestamp SET DEFAULT now();\n\u003c/pre\u003e\u003cp\u003e要重命名一个现有列：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors RENAME COLUMN address TO city;\n\u003c/pre\u003e\u003cp\u003e重命名一个现有的表：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors RENAME TO suppliers;\n\u003c/pre\u003e\u003cp\u003e重命名一个现有的约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;\n\u003c/pre\u003e\u003cp\u003e为一列增加一个非空约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ALTER COLUMN street SET NOT NULL;\n\u003c/pre\u003e\u003cp\u003e从一列移除一个非空约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;\n\u003c/pre\u003e\u003cp\u003e要向一个表及其所有后代添加一个检查约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);\n\u003c/pre\u003e\u003cp\u003e要只向一个表本身添加检查约束，而不添加到其后代：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;\n\u003c/pre\u003e\u003cp\u003e（该检查约束也不会被未来的后代表继承。）\u003c/p\u003e\u003cp\u003e要从一个表及其所有后代移除一个检查约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003e只从一个表移除一个检查约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003e（该检查约束在所有子表上仍然保留。）\u003c/p\u003e\u003cp\u003e为一个表增加一个外键约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个外键约束，并且尽量不要影响其他工作：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;\nALTER TABLE distributors VALIDATE CONSTRAINT distfk;\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个（多列）唯一约束：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个自动命名的主键约束，注意一个表只能拥有一个主键：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors ADD PRIMARY KEY (dist_id);\n\u003c/pre\u003e\u003cp\u003e把一个表移动到一个不同的表空间：\u003c/p\u003e\u003cpre\u003eALTER TABLE distributors SET TABLESPACE fasttablespace;\n\u003c/pre\u003e\u003cp\u003e把一个表移动到一个不同的模式：\u003c/p\u003e\u003cpre\u003eALTER TABLE myschema.distributors SET SCHEMA yourschema;\n\u003c/pre\u003e\u003cp\u003e重建一个主键约束，并且在重建索引期间不阻塞更新：\u003c/p\u003e\u003cpre\u003eCREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);\nALTER TABLE distributors DROP CONSTRAINT distributors_pkey,\n    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到范围分区表上：\u003c/p\u003e\u003cpre\u003eALTER TABLE measurement\n    ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM (\u0026#39;2016-07-01\u0026#39;) TO (\u0026#39;2016-08-01\u0026#39;);\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到列表分区表上：\u003c/p\u003e\u003cpre\u003eALTER TABLE cities\n    ATTACH PARTITION cities_ab FOR VALUES IN (\u0026#39;a\u0026#39;, \u0026#39;b\u0026#39;);\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到 hash 分区表上：\u003c/p\u003e\u003cpre\u003eALTER TABLE orders\n    ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003e要把默认分区附加到分区表上：\u003c/p\u003e\u003cpre\u003eALTER TABLE cities\n    ATTACH PARTITION cities_partdef DEFAULT;\n\u003c/pre\u003e\u003cp\u003e从一个分区表分离一个分区：\u003c/p\u003e\u003cpre\u003eALTER TABLE measurement\n    DETACH PARTITION measurement_y2015m12;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e形式\u003ccode\u003eADD [COLUMN]\u003c/code\u003e、\u003ccode\u003eDROP [COLUMN]\u003c/code\u003e、\u003ccode\u003eDROP IDENTITY\u003c/code\u003e、\u003ccode\u003eRESTART\u003c/code\u003e、\u003ccode\u003eSET DEFAULT\u003c/code\u003e、\u003ccode\u003eSET DATA TYPE\u003c/code\u003e（不带\u003ccode\u003eUSING\u003c/code\u003e）、\u003ccode\u003eSET GENERATED\u003c/code\u003e以及\u003ccode\u003eSET \u003cem\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 符合 SQL 标准。形式\u003ccode\u003eADD \u003cem\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 在省略\u003ccode\u003eUSING INDEX\u003c/code\u003e和\u003ccode\u003eNOT VALID\u003c/code\u003e子句，且约束类型为 \u003ccode\u003eCHECK\u003c/code\u003e、\u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 或\u003ccode\u003eREFERENCES\u003c/code\u003e之一时，也符合 SQL 标准。其他形式都是 \u003cspan\u003ePostgreSQL\u003c/span\u003e对 SQL 标准的扩展。此外，在单个 \u003ccode\u003eALTER TABLE\u003c/code\u003e命令中指定多个修改也是一种扩展。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eALTER TABLE DROP COLUMN\u003c/code\u003e可以被用来删除一个表的唯一的列，从而留下一个零列的表。这是一种 SQL 的扩展，SQL 中不允许零列的表。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"9b5bebce6afb4c813b0c0afe77369565c69815217aa17867bd2447ec8967e25c","Payload":{"purpose_zh":"更改一个表的定义","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e更改现有表的定义。下面描述了若干子形式。请注意，各子形式所需的锁级别可能不同。除非另有明确说明，否则将获取\u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。给出多个子命令时，获取的锁将是其中任一子命令所需的最严格级别。\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD [ COLUMN ] [ IF NOT EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式使用与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e相同的语法向表中添加新列。如果指定了\u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e，且同名列已经存在，则不会报错。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP [ COLUMN ] [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式从表中删除一列。涉及该列的索引和表约束也会自动删除。如果删除该列会使某个引用它的多元统计信息只剩下一列数据，那么该统计信息也会被移除。如果表外有任何对象依赖于该列，例如外键引用或视图，你就需要指定\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e。如果指定了\u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e而该列不存在，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式更改表中某一列的类型。涉及该列的索引和简单表约束会通过重新解析最初提供的表达式，自动转换为使用新的列类型。可选的\u003ccode class=\"literal\"\u003eCOLLATE\u003c/code\u003e子句为新列指定排序规则；如果省略，则使用新列类型的默认排序规则。可选的\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句指定如何根据旧值计算新列的值；如果省略，则默认转换与从旧数据类型到新数据类型的赋值类型转换相同。如果从旧类型到新类型不存在隐式或赋值类型转换，则必须提供\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句。\u003c/p\u003e\u003cp\u003e当使用该形式时，列的统计信息会被删除，因此建议随后在该表上运行 \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e。对于虚拟生成列，则不需要\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e，因为这类列从不具有统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e/\u003ccode class=\"literal\"\u003eDROP DEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式设置或移除列的默认值（其中移除等同于将默认值设为 NULL）。新的默认值只会应用于后续的\u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e或\u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e命令；它不会导致表中已有的行发生变化。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e/\u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式改变列是被标记为允许空值，还是被标记为拒绝空值。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSET NOT NULL\u003c/code\u003e只能应用于满足以下条件的列：表中没有任何记录在该列上包含\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e值。通常，系统会在执行\u003ccode class=\"literal\"\u003eALTER TABLE\u003c/code\u003e时通过扫描整个表来检查这一点，除非指定了\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e；不过，如果已存在一个有效的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束（并且不会在同一命令中被删除）能够证明不可能存在\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e，则会跳过表扫描。如果一列带有一个无效的非空约束，\u003ccode class=\"literal\"\u003eSET NOT NULL\u003c/code\u003e会对其进行验证。\u003c/p\u003e\u003cp\u003e如果该表是一个分区，而该列在父表中被标记为\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e，则不能对该列执行\u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e。要从所有分区中删除\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束，请在父表上执行\u003ccode class=\"literal\"\u003eDROP NOT NULL\u003c/code\u003e。即使父表上没有\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束，也仍然可以根据需要在单独的分区上添加此类约束；也就是说，即使父表允许空值，子表仍可禁止空值，但反过来不行。也可以仅从父表中删除\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束（即使用\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e），这样不会从子表中移除该约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET EXPRESSION AS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会替换生成列的表达式。存储生成列中的现有数据会被重写，此后的所有更改都会应用新的生成表达式。\u003c/p\u003e\u003cp\u003e当该形式用于存储生成列时，其统计信息会被删除，因此建议随后在该表上运行 \u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e。对于虚拟生成列，则不需要\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e，因为这类列从不具有统计信息。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP EXPRESSION [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会把一个存储生成列转换成普通基础列。列中的现有数据会被保留，但今后的更改将不再应用生成表达式。\u003c/p\u003e\u003cp\u003e目前该形式只支持存储生成列（不支持虚拟生成列）。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003eDROP EXPRESSION IF EXISTS\u003c/code\u003e，而该列不是生成列，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET GENERATED { ALWAYS | BY DEFAULT }\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP IDENTITY [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式改变列是否为标识列，或者更改现有标识列的生成属性。详情见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e。与\u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e类似，这些形式只影响后续\u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e和\u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e命令的行为；它们不会导致表中已有的行发生变化。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003eDROP IDENTITY IF EXISTS\u003c/code\u003e，而该列不是标识列，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTART\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式修改现有标识列底层的序列。\u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e是\u003ca href=\"/docs/18/sql-altersequence.html\" title=\"ALTER SEQUENCE\"\u003e\u003ccode class=\"command\"\u003eALTER SEQUENCE\u003c/code\u003e\u003c/a\u003e支持的一个选项，例如\u003ccode class=\"literal\"\u003eINCREMENT BY\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET STATISTICS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式为后续\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e操作设置列级统计信息收集目标。该目标可以设置在 0 到 10000 之间。将其设为\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e可以恢复使用系统默认的统计目标（\u003ca href=\"/docs/18/runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET\"\u003edefault_statistics_target\u003c/a\u003e）。（设为 -1 也是一种已废弃的写法，效果相同。）有关\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e查询规划器如何使用统计信息的更多信息，请参见\u003ca href=\"/docs/18/planner-stats.html\" title=\"14.2. 规划器使用的统计信息\"\u003e第 14.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSET STATISTICS\u003c/code\u003e会获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置或重置每个属性的选项。目前定义的每属性选项只有\u003ccode class=\"literal\"\u003en_distinct\u003c/code\u003e和\u003ccode class=\"literal\"\u003en_distinct_inherited\u003c/code\u003e，它们会覆盖后续\u003ca href=\"/docs/18/sql-analyze.html\" title=\"ANALYZE\"\u003e\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e\u003c/a\u003e操作对非重复值数量的估计。\u003ccode class=\"literal\"\u003en_distinct\u003c/code\u003e影响表本身的统计信息，而\u003ccode class=\"literal\"\u003en_distinct_inherited\u003c/code\u003e影响为该表及其继承子表收集的统计信息，以及为分区表收集的统计信息。当指定值为正数时，查询规划器将假定该列恰好包含指定数量的非空非重复值。也可以通过使用小于 0 且大于等于 -1 的值来指定小数值。这会指示查询规划器通过将指定数字的绝对值乘以表中估计行数来估算非重复值的数量。例如，值为 -1 表示该列中的所有值都不同，而值为 -0.5 表示每个值平均出现两次。当表的大小随时间变化时，这会很有用。有关\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e查询规划器如何使用统计信息的更多信息，请参见\u003ca href=\"/docs/18/planner-stats.html\" title=\"14.2. 规划器使用的统计信息\"\u003e第 14.2 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e更改每个属性的选项会获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置列的存储模式。这控制该列的数据是行内保存还是保存在辅助\u003cacronym\u003eTOAST\u003c/acronym\u003e表中，以及数据是否应被压缩。\u003ccode class=\"literal\"\u003ePLAIN\u003c/code\u003e必须用于诸如\u003ccode class=\"type\"\u003einteger\u003c/code\u003e之类的固定长度值，表示行内且不压缩。\u003ccode class=\"literal\"\u003eMAIN\u003c/code\u003e用于行内、可压缩的数据。\u003ccode class=\"literal\"\u003eEXTERNAL\u003c/code\u003e用于行外、不压缩的数据，而\u003ccode class=\"literal\"\u003eEXTENDED\u003c/code\u003e用于行外、已压缩的数据。写成\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e会把存储模式设置为该列数据类型的默认模式。对于大多数支持非\u003ccode class=\"literal\"\u003ePLAIN\u003c/code\u003e存储的数据类型，默认模式是\u003ccode class=\"literal\"\u003eEXTENDED\u003c/code\u003e。使用\u003ccode class=\"literal\"\u003eEXTERNAL\u003c/code\u003e会让对非常大的\u003ccode class=\"type\"\u003etext\u003c/code\u003e和\u003ccode class=\"type\"\u003ebytea\u003c/code\u003e值执行子串操作更快，但代价是占用更多存储空间。请注意，\u003ccode class=\"literal\"\u003eALTER TABLE ... SET STORAGE\u003c/code\u003e本身不会改变表中的任何内容；它只是设定未来更新表时要采用的策略。更多信息见\u003ca href=\"/docs/18/storage-toast.html\" title=\"66.2. TOAST\"\u003e第 66.2 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式设置列的压缩方法，用来决定未来插入的值将如何被压缩（前提是存储模式允许压缩）。这不会导致表被重写，因此现有数据仍可能采用其他压缩方法。如果使用\u003cspan class=\"application\"\u003epg_restore\u003c/span\u003e恢复该表，那么所有值都会按配置的压缩方法重新写入。然而，当数据是从另一个关系插入时（例如通过\u003ccode class=\"command\"\u003eINSERT ... SELECT\u003c/code\u003e），源表中的值不一定会被去 TOAST 化，因此先前已压缩的数据可能会保留原有的压缩方法，而不是按目标列的压缩方法重新压缩。支持的压缩方法有\u003ccode class=\"literal\"\u003epglz\u003c/code\u003e和\u003ccode class=\"literal\"\u003elz4\u003c/code\u003e。（\u003ccode class=\"literal\"\u003elz4\u003c/code\u003e仅在使用\u003ccode class=\"option\"\u003e--with-lz4\u003c/code\u003e构建\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e时才可用。）此外，\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e还可以是\u003ccode class=\"literal\"\u003edefault\u003c/code\u003e，表示在数据插入时查询\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TOAST-COMPRESSION\"\u003edefault_toast_compression\u003c/a\u003e设置，以决定要使用的方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e [ NOT VALID ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式使用与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e相同的约束语法向表中添加一个新约束，另外还支持选项\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e；目前该选项只允许用于外键、\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e以及非空约束。\u003c/p\u003e\u003cp\u003e通常，该形式会扫描表，以验证表中所有现有行都满足新约束。但如果使用了\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e选项，就会跳过这一步可能非常耗时的扫描。该约束仍会对后续的插入或更新生效（也就是说，对于外键，除非被引用表中存在匹配行，否则操作会失败；对于检查约束，除非新行满足指定的检查条件，否则操作会失败）。但是，在通过\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e选项完成验证之前，数据库不会假定该约束对表中的所有行都成立。请参见下面的\u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"注解\"\u003e注解\u003c/a\u003e，了解使用\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e选项的更多信息。\u003c/p\u003e\u003cp\u003e尽管大多数形式的\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e需要\u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，\u003ccode class=\"literal\"\u003eADD FOREIGN KEY\u003c/code\u003e只需要\u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。请注意，\u003ccode class=\"literal\"\u003eADD FOREIGN KEY\u003c/code\u003e除了在声明约束的表上获取锁之外，还会在被引用的表上获取\u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e当向分区表添加唯一约束或主键约束时，还会受到附加限制；请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式基于一个现有唯一索引向表中添加新的\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e或\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e约束。该约束将包含该索引的全部列。\u003c/p\u003e\u003cp\u003e该索引不能包含表达式列，也不能是部分索引。此外，它必须是使用默认排序顺序的 B-树索引。这些限制保证该索引等价于通过常规\u003ccode class=\"literal\"\u003eADD PRIMARY KEY\u003c/code\u003e或\u003ccode class=\"literal\"\u003eADD UNIQUE\u003c/code\u003e命令构建出来的索引。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e，而该索引的列尚未被标记为\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e，则此命令会尝试对每个这样的列执行\u003ccode class=\"literal\"\u003eALTER COLUMN SET NOT NULL\u003c/code\u003e。这需要进行一次全表扫描，以验证这些列不包含空值。在其他情况下，这都是一个快速操作。\u003c/p\u003e\u003cp\u003e如果提供了约束名，则索引会被重命名以匹配该约束名；否则，约束将使用与索引相同的名称。\u003c/p\u003e\u003cp\u003e执行此命令后，该索引会被该约束\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e拥有\u003c/span\u003e”\u003c/span\u003e，就像它是通过常规\u003ccode class=\"literal\"\u003eADD PRIMARY KEY\u003c/code\u003e或\u003ccode class=\"literal\"\u003eADD UNIQUE\u003c/code\u003e命令构建出来的一样。特别是，删除该约束也会使该索引一并消失。\u003c/p\u003e\u003cp\u003e该形式目前不支持分区表。\u003c/p\u003e\u003cdiv class=\"note\"\u003e\u003ch3\u003e注意\u003c/h3\u003e\u003cp\u003e在需要添加新约束、但又不希望长时间阻塞表更新的场景中，使用现有索引添加约束会很有帮助。为此，可以先使用\u003ccode class=\"command\"\u003eCREATE UNIQUE INDEX CONCURRENTLY\u003c/code\u003e创建索引，然后再用此语法把它转换成约束。参见下面的示例。\u003c/p\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式修改一个先前创建的约束的属性。目前只有外键约束可以用这种方式修改，但见下文。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT ... INHERIT\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALTER CONSTRAINT ... NO INHERIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式会修改一个可继承约束，使其变为不可继承，或者反过来。当前只有非空约束可以这样修改。除了改变该约束的可继承状态之外，在把一个不可继承约束标记为可继承、且该表存在子表时，系统还会向这些子表添加一个等价约束。如果在存在子表的表上把一个可继承约束标记为不可继承，则子表上的对应约束会被标记为不再继承，但不会被移除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式通过扫描表来验证一个先前以\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e创建的外键、检查或非空约束，以确保不存在任何不满足该约束的行。如果该约束被设置为\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e，则会引发错误。如果该约束已经被标记为有效，则不会发生任何事情。（关于此命令有何用途，请参见下面的\u003ca href=\"/docs/18/sql-altertable.html#SQL-ALTERTABLE-NOTES\" title=\"注解\"\u003e注解\u003c/a\u003e。）\u003c/p\u003e\u003cp\u003e此命令会获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP CONSTRAINT [ IF EXISTS ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式删除表上指定的约束，以及支撑该约束的任何索引。如果指定了\u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e而该约束不存在，则不会报错；此时会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE [ REPLICA | ALWAYS ] TRIGGER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式配置表上触发器的触发行为。被禁用的触发器仍为系统所知，但在其触发事件发生时不会执行。（对于延迟触发器，启用状态是在事件发生时检查，而不是在触发器函数实际执行时检查。）可以禁用或启用按名称指定的单个触发器、表上的全部触发器，或者仅用户触发器（此选项排除内部生成的约束触发器，例如用于实现外键约束或可延迟的唯一约束和排他约束的触发器）。禁用或启用内部生成的约束触发器需要超级用户权限；应谨慎执行，因为如果这些触发器不执行，就无法保证约束的完整性。\u003c/p\u003e\u003cp\u003e触发器的触发机制也受配置变量\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\"\u003esession_replication_role\u003c/a\u003e的影响。普通启用的触发器（默认）会在复制角色为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eorigin\u003c/span\u003e”\u003c/span\u003e（默认）或\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003elocal\u003c/span\u003e”\u003c/span\u003e时触发。配置为\u003ccode class=\"literal\"\u003eENABLE REPLICA\u003c/code\u003e的触发器仅会在会话处于\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003ereplica\u003c/span\u003e”\u003c/span\u003e模式时触发，而配置为\u003ccode class=\"literal\"\u003eENABLE ALWAYS\u003c/code\u003e的触发器无论当前复制角色为何都会触发。\u003c/p\u003e\u003cp\u003e这种机制的效果是，在默认配置下，触发器不会在副本上触发。这很有用，因为如果在源端使用触发器在表之间传播数据，那么复制系统也会复制这些传播后的数据；因此触发器不应在副本上再次触发，否则会导致重复。不过，如果触发器用于其他目的，例如生成外部告警，那么把它设置为\u003ccode class=\"literal\"\u003eENABLE ALWAYS\u003c/code\u003e、使其在副本上也触发，可能就是合适的做法。\u003c/p\u003e\u003cp\u003e当该命令应用于分区表时，分区中对应的克隆触发器的状态也会被更新，除非指定了\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e此命令会获取一个\u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE [ REPLICA | ALWAYS ] RULE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式配置属于表的重写规则是否应用。禁用的规则仍然为系统所知，但在查询重写期间不会应用。语义与禁用/启用触发器相同。对于\u003ccode class=\"literal\"\u003eON SELECT\u003c/code\u003e规则，此配置将被忽略，这些规则始终会被应用，以确保视图在当前会话处于非默认复制角色时仍然正常工作。\u003c/p\u003e\u003cp\u003e规则触发机制也受配置变量\u003ca href=\"/docs/18/runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE\"\u003esession_replication_role\u003c/a\u003e的影响，类似于上面描述的触发器。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDISABLE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eENABLE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式控制属于该表的行安全性策略的应用。如果启用后该表没有任何策略，则会应用默认拒绝策略。请注意，即使行级安全已禁用，表上仍然可以存在策略。在这种情况下，这些策略\u003cspan class=\"emphasis\"\u003e\u003cem\u003e不会\u003c/em\u003e\u003c/span\u003e被应用，而是会被忽略。另请参阅\u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\"\u003e\u003ccode class=\"command\"\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO FORCE\u003c/code\u003e/\u003ccode class=\"literal\"\u003eFORCE ROW LEVEL SECURITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些形式控制当用户是表所有者时，该表的行安全性策略是否生效。如果启用，则当用户是表所有者时也会应用行安全性策略；如果禁用（默认值），则当用户是表所有者时不会应用行级安全。另请参阅\u003ca href=\"/docs/18/sql-createpolicy.html\" title=\"CREATE POLICY\"\u003e\u003ccode class=\"command\"\u003eCREATE POLICY\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCLUSTER ON\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式选择未来\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e操作的默认索引。它实际上并不会重新对表执行聚簇。\u003c/p\u003e\u003cp\u003e更改聚簇选项会获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET WITHOUT CLUSTER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式从表中移除最近一次使用的\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e所用索引设置。这会影响将来那些未指定索引的聚簇操作。\u003c/p\u003e\u003cp\u003e更改聚簇选项会获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET WITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这是用于移除\u003ccode class=\"literal\"\u003eoid\u003c/code\u003e系统列的向后兼容语法。由于如今已无法再添加\u003ccode class=\"literal\"\u003eoid\u003c/code\u003e系统列，因此它实际上永远不会产生任何效果。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ACCESS METHOD\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式通过使用指定的访问方法重写表来改变其访问方法；指定\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e则会选择由配置参数\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e设定的访问方法。更多信息请参见\u003ca href=\"/docs/18/tableam.html\" title=\"第 62 章 表访问方法接口定义\"\u003e第 62 章\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e当应用于分区表时，没有数据需要重写，但之后创建的分区默认会使用给定的访问方法，除非被\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句覆盖。指定\u003ccode class=\"varname\"\u003eDEFAULT\u003c/code\u003e会移除先前设置的值，使未来的分区重新默认使用\u003ccode class=\"varname\"\u003edefault_table_access_method\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET TABLESPACE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表的表空间更改为指定的表空间，并将与该表关联的数据文件移动到新的表空间。表上的索引（如果有）不会被移动，但可以通过额外的\u003ccode class=\"literal\"\u003eSET TABLESPACE\u003c/code\u003e命令单独移动。当应用于分区表时，不会移动任何内容，但之后通过\u003ccode class=\"command\"\u003eCREATE TABLE PARTITION OF\u003c/code\u003e创建的分区会使用该表空间，除非被\u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e子句覆盖。\u003c/p\u003e\u003cp\u003e使用\u003ccode class=\"literal\"\u003eALL IN TABLESPACE\u003c/code\u003e形式可以移动当前数据库中位于某个表空间中的所有表；该形式会先锁定所有待移动表，然后逐个移动。该形式还支持\u003ccode class=\"literal\"\u003eOWNED BY\u003c/code\u003e，从而只移动指定角色拥有的表。如果指定了\u003ccode class=\"literal\"\u003eNOWAIT\u003c/code\u003e，则一旦无法立即获取所需的全部锁，命令就会失败。请注意，系统目录不会通过此命令移动；如果需要，可改用\u003ccode class=\"command\"\u003eALTER DATABASE\u003c/code\u003e或显式的\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e调用。\u003ccode class=\"literal\"\u003einformation_schema\u003c/code\u003e中的关系不被视为系统目录的一部分，因此会被移动。另请参阅\u003ca href=\"/docs/18/sql-createtablespace.html\" title=\"CREATE TABLESPACE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLESPACE\u003c/code\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET { LOGGED | UNLOGGED }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表从不记录 WAL 改为记录 WAL，或反过来（参见\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-UNLOGGED\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/a\u003e）。它不能应用于临时表。\u003c/p\u003e\u003cp\u003e这也会改变与该表关联的任何序列（用于标识列或 serial 列）的持久性。不过，也可以单独更改这些序列的持久性。\u003c/p\u003e\u003cp\u003e该形式不支持分区表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式更改表的一个或多个存储参数。可用参数的详细信息，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e文档中的\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"存储参数\"\u003eStorage Parameters\u003c/a\u003e。请注意，此命令不会立即修改表内容；根据参数不同，你可能需要重写表才能获得预期效果。这可以通过\u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM FULL\u003c/code\u003e\u003c/a\u003e、\u003ca href=\"/docs/18/sql-cluster.html\" title=\"CLUSTER\"\u003e\u003ccode class=\"command\"\u003eCLUSTER\u003c/code\u003e\u003c/a\u003e，或者任何会强制重写表的\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e形式来完成。对于与规划器相关的参数，更改会从下一次锁定该表时起生效，因此当前正在执行的查询不会受到影响。\u003c/p\u003e\u003cp\u003e对于 fillfactor、toast 和 autovacuum 存储参数，以及规划器参数\u003ccode class=\"varname\"\u003eparallel_workers\u003c/code\u003e，会获取\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把一个或多个存储参数重置为默认值。与\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e一样，可能仍需要进行表重写才能让整张表完全更新。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把目标表添加为指定父表的新子表。此后，对父表的查询将包含目标表中的记录。要被添加为子表，目标表必须已经包含父表的所有同名列（也可以再有额外列）。这些列的数据类型必须匹配。\u003c/p\u003e\u003cp\u003e此外，父表上的所有\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e和\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束也都必须存在于子表上，但被标记为不可继承的约束（即使用\u003ccode class=\"literal\"\u003eALTER TABLE ... ADD CONSTRAINT ... NO INHERIT\u003c/code\u003e创建的约束）除外，这些约束会被忽略。所有与之匹配的子表约束都不能被标记为不可继承。目前不会考虑\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e和\u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e约束，但将来这可能会改变。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把目标表从指定父表的子表列表中移除。对父表的查询将不再包含来自目标表的记录。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表关联到一个复合类型，就好像它是通过\u003ccode class=\"command\"\u003eCREATE TABLE OF\u003c/code\u003e创建的一样。表的列名和类型列表必须与该复合类型完全一致。该表还必须不继承自任何其他表。这些限制保证了\u003ccode class=\"command\"\u003eCREATE TABLE OF\u003c/code\u003e会允许一个等价的表定义。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT OF\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会解除类型化表与其类型之间的关联。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOWNER TO\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表、序列、视图、物化视图或外部表的所有者更改为指定用户。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eREPLICA IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式修改写入预写式日志的信息，以标识被更新或删除的行。在大多数情况下，只有当某列的旧值与新值不同，才记录该列的旧值；不过，如果旧值是外部存储的，则无论它是否改变，都会被记录。除非正在使用逻辑复制，否则该选项没有效果。\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录主键列的旧值。这是非系统表的默认值。如果没有主键，其行为与\u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录由指定索引覆盖的列的旧值。该索引必须是唯一的、非部分的、不可延迟的，并且只包含被标记为\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e的列。如果该索引被删除，其行为与\u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e相同。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e记录该行中所有列的旧值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOTHING\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e不记录关于旧行的任何信息。这是系统表的默认值。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e形式用于更改表（或索引、序列、视图、物化视图或外部表）的名称、表中某个列的名称，或者表上某个约束的名称。重命名一个带有底层索引的约束时，该索引也会一并重命名。已存储的数据不会受到影响。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET SCHEMA\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把表移动到另一个模式。相关索引、约束以及由表列拥有的序列也会一并移动。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eATTACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式把一个现有表（它本身也可能已分区）附加为目标表的一个分区。可以使用\u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e把它附加为特定值的分区，也可以使用\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e把它附加为默认分区。对于目标表中的每个索引，如果分区中已经存在有效的等价索引，则会把它附加到目标表的索引上，就像执行了 ALTER INDEX ATTACH PARTITION 一样；否则，将创建一个新的对应索引。分区上的无效索引会被跳过。请注意，如果现有表是外部表，而目标表上存在\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e索引，则当前不允许把该表附加为目标表的分区。（另见\u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e。）对于目标表中存在的每个用户定义行级触发器，也都会在被附加表中创建一个对应触发器。\u003c/p\u003e\u003cp\u003e使用\u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e的分区，其\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e语法与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e中的相同。分区边界说明必须与目标表的分区策略和分区键相对应。待附加的表必须拥有与目标表完全相同且不多不少的列；此外，列类型也必须匹配。它还必须具有目标表上所有未标记为\u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e的\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e和\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束。目前不会考虑\u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e约束。如果父表中的\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e和\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e约束尚不存在于该分区中，则会在该分区上创建这些约束。\u003c/p\u003e\u003cp\u003e如果新分区是普通表，则会执行一次全表扫描，以检查表中现有行不会违反分区约束。可以在执行此命令之前，先向该表添加一个有效的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束，使其只允许满足所需分区约束的行，从而避免这次扫描。系统会利用该\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束来判断无须扫描该表以验证分区约束。不过，如果分区键中有任何一个是表达式，而该分区又不接受\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e值，则这一方法无效。如果附加的是一个不接受\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e值的列表分区，还应向分区键列添加\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束，除非该分区键是表达式。\u003c/p\u003e\u003cp\u003e如果新分区是外部表，则不会执行任何检查来验证该外部表中的所有行都满足分区约束。（关于外部表上的约束，请参见\u003ca href=\"/docs/18/sql-createforeigntable.html\" title=\"CREATE FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e中的讨论。）\u003c/p\u003e\u003cp\u003e当某个表具有默认分区时，定义一个新分区会改变默认分区的分区约束。默认分区不能包含任何本应移动到新分区中的行，因此系统会扫描默认分区以确认不存在此类行。和扫描新分区一样，如果存在适当的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束，也可以避免这次扫描；同样地，当默认分区是外部表时，这次扫描总会被跳过。\u003c/p\u003e\u003cp\u003e附加分区会在父表上获取一个\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁，此外还会在被附加的表以及默认分区（如果有）上获取\u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e如果被附加的表本身是分区表，则还必须在其所有子分区上持有锁；如果默认分区本身也是分区表，也是如此。添加\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束（如\u003ca href=\"/docs/18/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE\" title=\"5.12.2.2. 分区维护\"\u003e第 5.12.2.2 节\u003c/a\u003e所述）可以避免对子分区加锁。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDETACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e [ CONCURRENTLY | FINALIZE ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该形式会把目标表的指定分区分离出来。被分离的分区会继续作为一张独立表存在，但不再与原先所在的表保持任何关联。任何附加到目标表索引上的索引都会被分离。任何作为目标表中触发器克隆而创建的触发器都会被移除。对于任何在外键约束中引用该父分区表的表，都会获取\u003ccode class=\"literal\"\u003eSHARE\u003c/code\u003e锁。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003eCONCURRENTLY\u003c/code\u003e，它将使用较低的锁级别运行，以避免阻塞其他可能正在访问该分区表的会话。在这种模式下，内部会使用两个事务。第一个事务中，会在父表和该分区上都获取\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁，并把该分区标记为正在分离；随后提交事务，并等待所有其他正在使用该分区表的事务结束。在所有这些事务完成后，第二个事务会在分区表上获取\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁、在分区上获取\u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e锁，并完成分离过程。系统还会向该分区添加一个与分区约束等价的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束。\u003ccode class=\"literal\"\u003eCONCURRENTLY\u003c/code\u003e不能在事务块中运行；如果该分区表包含默认分区，也不允许使用它。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003eFINALIZE\u003c/code\u003e，则会完成此前被取消或中断的\u003ccode class=\"literal\"\u003eDETACH CONCURRENTLY\u003c/code\u003e调用。在任意时刻，一个分区表中最多只能有一个分区处于等待分离完成的状态。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e所有作用于单个表的 \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e 形式，除\u003ccode class=\"literal\"\u003eRENAME\u003c/code\u003e、\u003ccode class=\"literal\"\u003eSET SCHEMA\u003c/code\u003e、\u003ccode class=\"literal\"\u003eATTACH PARTITION\u003c/code\u003e和\u003ccode class=\"literal\"\u003eDETACH PARTITION\u003c/code\u003e外，都可以组合成一个多项修改列表，一并执行。例如，可以在一条命令中添加多个列并且/或者修改多个列的类型。对于大型表，这一点尤其有用，因为这样只需对表执行一次遍历。\u003c/p\u003e\u003cp\u003e要使用\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e，你必须拥有该表。要更改表的模式或表空间，你还必须在新模式或新表空间上拥有\u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e权限。要把一个表作为父表的新子表加入，你还必须拥有该父表。此外，要把一个表附加为另一个表的新分区，你必须拥有被附加的表。要更改所有者，你必须能够对新的所有者角色执行\u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e，并且该角色必须在该表所在模式上具有\u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e权限。（这些限制确保更改所有者不会做出任何你不能通过删除并重建该表来完成的事情。不过，超级用户无论如何都可以更改任何表的所有权。）要添加列、修改列类型或使用\u003ccode class=\"literal\"\u003eOF\u003c/code\u003e子句，你还必须在相应数据类型上具有\u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e权限。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果表不存在，则不要抛出错误。这种情况下会发出一条提示。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要修改的一个现有表的名称（可以是模式限定的）。如果在表名前指定了 \u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，则只会修改该表。如果没有指定\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e，该表及其所有后代表（如果有）都会被修改。可选地，在表名后面可以指定 \u003ccode class=\"literal\"\u003e*\u003c/code\u003e用来显式地指示包括后代表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新列或现有列的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_column_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e现有列的新名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e表的新名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新列的数据类型，或现有列的新数据类型。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表的新的表约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个新约束或者现有约束的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动删除依赖于被删除列或约束的对象（例如引用该列的视图），以及所有进一步依赖于这些对象的对象（见\u003ca href=\"/docs/18/ddl-depend.html\" title=\"5.15. 依赖跟踪\"\u003e第 5.15 节\u003c/a\u003e）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果存在任何依赖对象，则拒绝删除该列或约束。这是默认行为。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要禁用或启用的单个触发器的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e禁用或启用属于表的所有触发器。（如果任何触发器是内部生成的约束触发器，例如用于实现外键约束或可延迟唯一约束和排他约束的触发器，则需要超级用户权限。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSER\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e禁用或启用属于表的所有触发器，但不包括内部生成的约束触发器（例如用于实现外键约束或可延迟的唯一约束和排他约束的触发器）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个现有索引的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个表存储参数的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个表存储参数的新值。根据该参数，该值可能是一个数字或者一个词。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要与这个表关联或者解除关联的父表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_owner\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表的新拥有者的用户名。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_access_method\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要将表转换到的访问方法名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要把该表移入其中的表空间的名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_schema\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该表将被移动到的模式名称。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要附加为该表的新分区或从该表分离出去的表名。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e新分区的分区边界说明。关于该语法的更多细节，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e关键字\u003ccode class=\"literal\"\u003eCOLUMN\u003c/code\u003e不影响语义，可以省略。\u003c/p\u003e\u003cp\u003e当使用\u003ccode class=\"literal\"\u003eADD COLUMN\u003c/code\u003e添加一列并指定了一个非易失性\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e时，默认值会在该语句执行时计算，其结果会保存在表的元数据中；访问任何现有行时都会返回该值。只有在表被重写时，这个值才会真正应用到行中，因此即使面对大表，\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e也会非常快。如果没有指定列约束，则会使用 NULL 作为\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e。在这两种情况下都不需要重写表。\u003c/p\u003e\u003cp\u003e添加带有易失性\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e（例如\u003ccode class=\"function\"\u003eclock_timestamp()\u003c/code\u003e）的列、存储生成列、标识列，或者数据类型为带约束域的列，都会导致整个表及其索引被重写。添加虚拟生成列则永远不需要重写。\u003c/p\u003e\u003cp\u003e更改现有列的类型通常会导致整个表及其索引被重写。作为例外，如果更改现有列的类型时，\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句并未改变列内容，且旧类型要么可以二进制强制转换为新类型，要么是基于新类型的无约束域，则不需要重写表。不过，除非系统能够验证新索引在逻辑上与现有索引等价，否则索引仍会被重建。例如，如果更改了列的排序规则，就必须重建索引，因为新的排序顺序可能不同。然而，在没有改变排序规则的情况下，可以在不重建索引的前提下将列从\u003ccode class=\"type\"\u003etext\u003c/code\u003e改为\u003ccode class=\"type\"\u003evarchar\u003c/code\u003e（反之亦然），因为这两种数据类型的排序方式相同。\u003c/p\u003e\u003cp\u003e对于大表，表和/或索引重建可能会耗费大量时间，并且临时需要多达两倍的磁盘空间。\u003c/p\u003e\u003cp\u003e添加\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e或\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e约束需要扫描表，以验证现有行满足该约束，但不需要重写表。如果把\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束添加为\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e，则不会执行此验证。\u003c/p\u003e\u003cp\u003e类似地，在附加新分区时，也可能会扫描该分区以验证现有行满足分区约束。\u003c/p\u003e\u003cp\u003e在单个\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e中允许指定多个更改的主要原因，是可以把多次表扫描或重写合并为对表的一次遍历。\u003c/p\u003e\u003cp\u003e扫描大表以验证新的外键、检查或非空约束可能需要很长时间，并且在\u003ccode class=\"command\"\u003eALTER TABLE ADD CONSTRAINT\u003c/code\u003e命令提交之前，会阻止对该表的其他更新。\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e约束选项的主要目的，是减小添加约束对并发更新的影响。使用\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e时，\u003ccode class=\"command\"\u003eADD CONSTRAINT\u003c/code\u003e命令不会扫描表，因此可以立即提交。之后可以发出\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e命令，以验证现有行满足该约束。验证步骤不需要阻止并发更新，因为它知道其他事务会对它们插入或更新的行强制执行该约束；只需检查预先存在的行。因此，验证只会在被修改的表上获取\u003ccode class=\"literal\"\u003eSHARE UPDATE EXCLUSIVE\u003c/code\u003e锁。（如果约束是外键，则被该约束引用的表上还需要\u003ccode class=\"literal\"\u003eROW SHARE\u003c/code\u003e锁。）除了改善并发性之外，在已知该表包含既有违规数据的情况下，\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e和\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e也很有用。一旦约束已经建立，就不能再插入新的违规数据，而现有问题则可以从容修正，直到\u003ccode class=\"literal\"\u003eVALIDATE CONSTRAINT\u003c/code\u003e最终成功。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e形式不会在物理上移除该列，而只是让它对 SQL 操作不可见。此后，对该表的插入和更新操作会为该列存储一个空值。因此，删除一列虽然很快，但不会立刻减少表占用的磁盘空间，因为被删除列所占用的空间尚未被回收。随着现有行被更新，这些空间会逐渐被回收。\u003c/p\u003e\u003cp\u003e若要强制立即回收已删除列所占的空间，可以执行任何一种会导致整表重写的\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e形式。这样会重建每一行，并用空值替换被删除的列。\u003c/p\u003e\u003cp\u003e会重写表的\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e形式对于 MVCC 来说并不安全。表重写完成后，如果并发事务使用的是在重写发生之前取得的快照，那么该表在这些并发事务看来会像一张空表。详见\u003ca href=\"/docs/18/mvcc-caveats.html\" title=\"13.6. 注意事项\"\u003e第 13.6 节\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e的\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e选项实际上可以指定任何涉及该行旧值的表达式；也就是说，它既可以引用正在转换的列，也可以引用其他列。这使得使用\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e语法完成非常通用的转换成为可能。正因为这种灵活性，\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e表达式不会应用到列的默认值（如果有）上，因为其结果可能不是默认值所要求的常量表达式。这意味着，当从旧类型到新类型不存在隐式或赋值类型转换时，即便提供了\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句，\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e也可能仍然无法转换默认值。在这种情况下，可以先用\u003ccode class=\"literal\"\u003eDROP DEFAULT\u003c/code\u003e删除默认值，执行\u003ccode class=\"literal\"\u003eALTER TYPE\u003c/code\u003e，然后再用\u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e添加一个合适的新默认值。类似的考虑也适用于涉及该列的索引和约束。\u003c/p\u003e\u003cp\u003e如果某个表有任何后代表，那么在不对后代表执行相同操作的情况下，就不允许在父表中添加列、重命名列或更改列类型。这保证了后代表始终拥有与父表相匹配的列。类似地，如果不同时重命名所有后代上的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束，就不能只在父表上重命名该检查约束，这样\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束才能在父表及其后代之间保持匹配。（不过，这一限制不适用于基于索引的约束。）此外，由于查询父表时也会同时查询其后代，父表上的约束除非在这些后代上也被标记为有效，否则就不能被标记为有效。在所有这些情况下，\u003ccode class=\"command\"\u003eALTER TABLE ONLY\u003c/code\u003e都会被拒绝。\u003c/p\u003e\u003cp\u003e只有当某个后代表中的列既不是从其他父表继承而来，也从未有过该列的独立定义时，递归的\u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e操作才会移除该后代表中的此列。非递归的\u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e（即\u003ccode class=\"command\"\u003eALTER TABLE ONLY ... DROP COLUMN\u003c/code\u003e）永远不会移除任何后代列，而只会把它们标记为独立定义，而非继承得到。对于分区表，非递归的\u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e命令会失败，因为一张表的所有分区都必须与分区根表拥有相同的列。\u003c/p\u003e\u003cp\u003e对于标识列的操作（\u003ccode class=\"literal\"\u003eADD GENERATED\u003c/code\u003e、\u003ccode class=\"literal\"\u003eSET\u003c/code\u003e 等、\u003ccode class=\"literal\"\u003eDROP IDENTITY\u003c/code\u003e），以及\u003ccode class=\"literal\"\u003eCLUSTER\u003c/code\u003e、\u003ccode class=\"literal\"\u003eOWNER\u003c/code\u003e和\u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e等操作，永远不会递归到后代表；也就是说，它们总是像指定了\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e一样执行。影响触发器状态的操作会递归到分区表的各个分区（除非指定了\u003ccode class=\"literal\"\u003eONLY\u003c/code\u003e），但永远不会递归到传统继承体系中的后代表。添加约束时，只有未标记为\u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e的\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e约束才会递归下去。\u003c/p\u003e\u003cp\u003e不允许更改系统目录表的任何部分。\u003c/p\u003e\u003cp\u003e有关有效参数的进一步说明，请参见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003ca href=\"/docs/18/ddl.html\" title=\"第 5 章 数据定义\"\u003e第 5 章\u003c/a\u003e中还有关于继承的更多信息。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e要向表中添加一个\u003ccode class=\"type\"\u003evarchar\u003c/code\u003e类型的列：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD COLUMN address varchar(30);\n\u003c/pre\u003e\u003cp\u003e这会使表中所有现有行的新列都填入空值。\u003c/p\u003e\u003cp\u003e要添加一个带非空默认值的列：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurements\n  ADD COLUMN mtime timestamp with time zone DEFAULT now();\n\u003c/pre\u003e\u003cp\u003e现有行会以当前时间作为新列的值填充，之后新行会接收其插入时的时间。\u003c/p\u003e\u003cp\u003e要添加一列，并先用与之后默认值不同的值填充它：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE transactions\n  ADD COLUMN status varchar(30) DEFAULT 'old',\n  ALTER COLUMN status SET default 'current';\n\u003c/pre\u003e\u003cp\u003e现有行会填入\u003ccode class=\"literal\"\u003eold\u003c/code\u003e，而后续命令的默认值将是\u003ccode class=\"literal\"\u003ecurrent\u003c/code\u003e。其效果与在两条单独的\u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e命令中发出这两个子命令相同。\u003c/p\u003e\u003cp\u003e要从表中删除一列：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors DROP COLUMN address RESTRICT;\n\u003c/pre\u003e\u003cp\u003e要在一个操作中更改两个现有列的类型：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors\n    ALTER COLUMN address TYPE varchar(80),\n    ALTER COLUMN name TYPE varchar(100);\n\u003c/pre\u003e\u003cp\u003e要把一个包含 Unix 时间戳的整数列改为 \u003ccode class=\"type\"\u003etimestamp with time zone\u003c/code\u003e，并通过\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e子句完成转换：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone\n    USING\n        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';\n\u003c/pre\u003e\u003cp\u003e如果该列带有一个不能自动转换为新数据类型的默认值表达式，也是同样的做法：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE foo\n    ALTER COLUMN foo_timestamp DROP DEFAULT,\n    ALTER COLUMN foo_timestamp TYPE timestamp with time zone\n    USING\n        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',\n    ALTER COLUMN foo_timestamp SET DEFAULT now();\n\u003c/pre\u003e\u003cp\u003e要重命名一个现有列：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME COLUMN address TO city;\n\u003c/pre\u003e\u003cp\u003e重命名一个现有的表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME TO suppliers;\n\u003c/pre\u003e\u003cp\u003e重命名一个现有的约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;\n\u003c/pre\u003e\u003cp\u003e为一列增加一个非空约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ALTER COLUMN street SET NOT NULL;\n\u003c/pre\u003e\u003cp\u003e从一列移除一个非空约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;\n\u003c/pre\u003e\u003cp\u003e要向一个表及其所有后代添加一个检查约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);\n\u003c/pre\u003e\u003cp\u003e要只向一个表本身添加检查约束，而不添加到其后代：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;\n\u003c/pre\u003e\u003cp\u003e（该检查约束也不会被未来的后代表继承。）\u003c/p\u003e\u003cp\u003e要从一个表及其所有后代移除一个检查约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003e只从一个表移除一个检查约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;\n\u003c/pre\u003e\u003cp\u003e（该检查约束在所有子表上仍然保留。）\u003c/p\u003e\u003cp\u003e为一个表增加一个外键约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个外键约束，并且尽量不要影响其他工作：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) NOT VALID;\nALTER TABLE distributors VALIDATE CONSTRAINT distfk;\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个（多列）唯一约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);\n\u003c/pre\u003e\u003cp\u003e为一个表增加一个自动命名的主键约束，注意一个表只能拥有一个主键：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors ADD PRIMARY KEY (dist_id);\n\u003c/pre\u003e\u003cp\u003e把一个表移动到一个不同的表空间：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE distributors SET TABLESPACE fasttablespace;\n\u003c/pre\u003e\u003cp\u003e把一个表移动到一个不同的模式：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE myschema.distributors SET SCHEMA yourschema;\n\u003c/pre\u003e\u003cp\u003e重建一个主键约束，并且在重建索引期间不阻塞更新：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);\nALTER TABLE distributors DROP CONSTRAINT distributors_pkey,\n    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到范围分区表上：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurement\n    ATTACH PARTITION measurement_y2016m07 FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到列表分区表上：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE cities\n    ATTACH PARTITION cities_ab FOR VALUES IN ('a', 'b');\n\u003c/pre\u003e\u003cp\u003e要把一个分区附加到 hash 分区表上：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE orders\n    ATTACH PARTITION orders_p4 FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003e要把默认分区附加到分区表上：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE cities\n    ATTACH PARTITION cities_partdef DEFAULT;\n\u003c/pre\u003e\u003cp\u003e从一个分区表分离一个分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eALTER TABLE measurement\n    DETACH PARTITION measurement_y2015m12;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e形式\u003ccode class=\"literal\"\u003eADD [COLUMN]\u003c/code\u003e、\u003ccode class=\"literal\"\u003eDROP [COLUMN]\u003c/code\u003e、\u003ccode class=\"literal\"\u003eDROP IDENTITY\u003c/code\u003e、\u003ccode class=\"literal\"\u003eRESTART\u003c/code\u003e、\u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e、\u003ccode class=\"literal\"\u003eSET DATA TYPE\u003c/code\u003e（不带\u003ccode class=\"literal\"\u003eUSING\u003c/code\u003e）、\u003ccode class=\"literal\"\u003eSET GENERATED\u003c/code\u003e以及\u003ccode class=\"literal\"\u003eSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 符合 SQL 标准。形式\u003ccode class=\"literal\"\u003eADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e 在省略\u003ccode class=\"literal\"\u003eUSING INDEX\u003c/code\u003e和\u003ccode class=\"literal\"\u003eNOT VALID\u003c/code\u003e子句，且约束类型为 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e、\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 或\u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e之一时，也符合 SQL 标准。其他形式都是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e对 SQL 标准的扩展。此外，在单个 \u003ccode class=\"command\"\u003eALTER TABLE\u003c/code\u003e命令中指定多个修改也是一种扩展。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eALTER TABLE DROP COLUMN\u003c/code\u003e可以被用来删除一个表的唯一的列，从而留下一个零列的表。这是一种 SQL 的扩展，SQL 中不允许零列的表。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"ALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    \u003cem class=\"replaceable\"\u003e\u003ccode\u003eaction\u003c/code\u003e\u003c/em\u003e [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    RENAME [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_column_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] [ ONLY ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ * ]\n    RENAME CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_constraint_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    RENAME TO \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_name\u003c/code\u003e\u003c/em\u003e\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    SET SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_schema\u003c/code\u003e\u003c/em\u003e\nALTER TABLE ALL IN TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e [ OWNED BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_name\u003c/code\u003e\u003c/em\u003e [, ... ] ]\n    SET TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    ATTACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\nALTER TABLE [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\n    DETACH PARTITION \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_name\u003c/code\u003e\u003c/em\u003e [ CONCURRENTLY | FINALIZE ]\n\n\u003cspan class=\"phrase\"\u003e其中\u003cem class=\"replaceable\"\u003e\u003ccode\u003eaction\u003c/code\u003e\u003c/em\u003e为以下之一：\u003c/span\u003e\n\n    ADD [ COLUMN ] [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e \u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    DROP [ COLUMN ] [ IF EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ RESTRICT | CASCADE ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ SET DATA ] TYPE \u003cem class=\"replaceable\"\u003e\u003ccode\u003edata_type\u003c/code\u003e\u003c/em\u003e [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET DEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP DEFAULT\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e { SET | DROP } NOT NULL\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET EXPRESSION AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP EXPRESSION [ IF EXISTS ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e { SET GENERATED { ALWAYS | BY DEFAULT } | SET \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_option\u003c/code\u003e\u003c/em\u003e | RESTART [ [ WITH ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003erestart\u003c/code\u003e\u003c/em\u003e ] } [...]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e DROP IDENTITY [ IF EXISTS ]\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET STATISTICS { \u003cem class=\"replaceable\"\u003e\u003ccode\u003einteger\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e RESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eattribute_option\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\n    ALTER [ COLUMN ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e SET COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\n    ADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e [ NOT VALID ]\n    ADD \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e\n    ALTER CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n    ALTER CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ INHERIT | NO INHERIT ]\n    VALIDATE CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\n    DROP CONSTRAINT [ IF EXISTS ]  \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e [ RESTRICT | CASCADE ]\n    DISABLE TRIGGER [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e | ALL | USER ]\n    ENABLE TRIGGER [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e | ALL | USER ]\n    ENABLE REPLICA TRIGGER \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE ALWAYS TRIGGER \u003cem class=\"replaceable\"\u003e\u003ccode\u003etrigger_name\u003c/code\u003e\u003c/em\u003e\n    DISABLE RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE REPLICA RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    ENABLE ALWAYS RULE \u003cem class=\"replaceable\"\u003e\u003ccode\u003erewrite_rule_name\u003c/code\u003e\u003c/em\u003e\n    DISABLE ROW LEVEL SECURITY\n    ENABLE ROW LEVEL SECURITY\n    FORCE ROW LEVEL SECURITY\n    NO FORCE ROW LEVEL SECURITY\n    CLUSTER ON \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\n    SET WITHOUT CLUSTER\n    SET WITHOUT OIDS\n    SET ACCESS METHOD { \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_access_method\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n    SET TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_tablespace\u003c/code\u003e\u003c/em\u003e\n    SET { LOGGED | UNLOGGED }\n    SET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] )\n    RESET ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [, ... ] )\n    INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\n    NO INHERIT \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e\n    OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e\n    NOT OF\n    OWNER TO { \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_owner\u003c/code\u003e\u003c/em\u003e | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\n    REPLICA IDENTITY { DEFAULT | USING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e | FULL | NOTHING }\n\n\u003cspan class=\"phrase\"\u003e而\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\nIN ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e [, ...] ) |\nFROM ( { \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e | MINVALUE | MAXVALUE } [, ...] )\n  TO ( { \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS \u003cem class=\"replaceable\"\u003e\u003ccode\u003enumeric_literal\u003c/code\u003e\u003c/em\u003e, REMAINDER \u003cem class=\"replaceable\"\u003e\u003ccode\u003enumeric_literal\u003c/code\u003e\u003c/em\u003e )\n\n\u003cspan class=\"phrase\"\u003e而\u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n[ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n{ NOT NULL [ NO INHERIT ] |\n  NULL |\n  CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] |\n  DEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003edefault_expr\u003c/code\u003e\u003c/em\u003e |\n  GENERATED ALWAYS AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003egeneration_expr\u003c/code\u003e\u003c/em\u003e ) [ STORED | VIRTUAL ] |\n  GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ] |\n  UNIQUE [ NULLS [ NOT ] DISTINCT ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  PRIMARY KEY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  REFERENCES \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n    [ ON DELETE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n\u003cspan class=\"phrase\"\u003e而\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n[ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n{ CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] |\n  NOT NULL \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ NO INHERIT ] |\n  UNIQUE [ NULLS [ NOT ] DISTINCT ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  PRIMARY KEY ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e |\n  EXCLUDE [ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e ] ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e WITH \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoperator\u003c/code\u003e\u003c/em\u003e [, ... ] ) \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e [ WHERE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e ) ] |\n  FOREIGN KEY ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e ] ) REFERENCES \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e [, ... ]  [, PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ] ) ]\n    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n\u003cspan class=\"phrase\"\u003e而\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint_using_index\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n    [ CONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e ]\n    { UNIQUE | PRIMARY KEY } USING INDEX \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_name\u003c/code\u003e\u003c/em\u003e\n    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\n\u003cspan class=\"phrase\"\u003e\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e和\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e约束中的\u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n[ INCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\n[ WITH ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003estorage_parameter\u003c/code\u003e\u003c/em\u003e [= \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e] [, ... ] ) ]\n[ USING INDEX TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\n\u003cspan class=\"phrase\"\u003e\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e约束中的\u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n{ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e | ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) } [ COLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass\u003c/code\u003e\u003c/em\u003e [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass_parameter\u003c/code\u003e\u003c/em\u003e = \u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\n\u003cspan class=\"phrase\"\u003e\u003ccode class=\"literal\"\u003eFOREIGN KEY\u003c/code\u003e/\u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e约束中的\u003cem class=\"replaceable\"\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] | SET DEFAULT [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ] }","synopsis_text":"ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\naction [, ... ]\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME [ COLUMN ] column_name TO new_column_name\nALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]\nRENAME CONSTRAINT constraint_name TO new_constraint_name\nALTER TABLE [ IF EXISTS ] name\nRENAME TO new_name\nALTER TABLE [ IF EXISTS ] name\nSET SCHEMA new_schema\nALTER TABLE ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]\nSET TABLESPACE new_tablespace [ NOWAIT ]\nALTER TABLE [ IF EXISTS ] name\nATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }\nALTER TABLE [ IF EXISTS ] name\nDETACH PARTITION partition_name [ CONCURRENTLY | FINALIZE ]\n\n其中action为以下之一：\n\nADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]\nDROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]\nALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]\nALTER [ COLUMN ] column_name SET DEFAULT expression\nALTER [ COLUMN ] column_name DROP DEFAULT\nALTER [ COLUMN ] column_name { SET | DROP } NOT NULL\nALTER [ COLUMN ] column_name SET EXPRESSION AS ( expression )\nALTER [ COLUMN ] column_name DROP EXPRESSION [ IF EXISTS ]\nALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]\nALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | SET sequence_option | RESTART [ [ WITH ] restart ] } [...]\nALTER [ COLUMN ] column_name DROP IDENTITY [ IF EXISTS ]\nALTER [ COLUMN ] column_name SET STATISTICS { integer | DEFAULT }\nALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )\nALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )\nALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\nALTER [ COLUMN ] column_name SET COMPRESSION compression_method\nADD table_constraint [ NOT VALID ]\nADD table_constraint_using_index\nALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\nALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ]\nVALIDATE CONSTRAINT constraint_name\nDROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]\nDISABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE TRIGGER [ trigger_name | ALL | USER ]\nENABLE REPLICA TRIGGER trigger_name\nENABLE ALWAYS TRIGGER trigger_name\nDISABLE RULE rewrite_rule_name\nENABLE RULE rewrite_rule_name\nENABLE REPLICA RULE rewrite_rule_name\nENABLE ALWAYS RULE rewrite_rule_name\nDISABLE ROW LEVEL SECURITY\nENABLE ROW LEVEL SECURITY\nFORCE ROW LEVEL SECURITY\nNO FORCE ROW LEVEL SECURITY\nCLUSTER ON index_name\nSET WITHOUT CLUSTER\nSET WITHOUT OIDS\nSET ACCESS METHOD { new_access_method | DEFAULT }\nSET TABLESPACE new_tablespace\nSET { LOGGED | UNLOGGED }\nSET ( storage_parameter [= value] [, ... ] )\nRESET ( storage_parameter [, ... ] )\nINHERIT parent_table\nNO INHERIT parent_table\nOF type_name\nNOT OF\nOWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }\nREPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }\n\n而partition_bound_spec为：\n\nIN ( partition_bound_expr [, ...] ) |\nFROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )\nTO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |\nWITH ( MODULUS numeric_literal, REMAINDER numeric_literal )\n\n而column_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL [ NO INHERIT ] |\nNULL |\nCHECK ( expression ) [ NO INHERIT ] |\nDEFAULT default_expr |\nGENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] |\nGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] index_parameters |\nPRIMARY KEY index_parameters |\nREFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]\n[ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n而table_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ CHECK ( expression ) [ NO INHERIT ] |\nNOT NULL column_name [ NO INHERIT ] |\nUNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nPRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters |\nEXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |\nFOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ]\n[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] }\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]\n\n而table_constraint_using_index为：\n\n[ CONSTRAINT constraint_name ]\n{ UNIQUE | PRIMARY KEY } USING INDEX index_name\n[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\n\nUNIQUE、PRIMARY KEY和EXCLUDE约束中的index_parameters为：\n\n[ INCLUDE ( column_name [, ... ] ) ]\n[ WITH ( storage_parameter [= value] [, ... ] ) ]\n[ USING INDEX TABLESPACE tablespace_name ]\n\nEXCLUDE约束中的exclude_element为：\n\n{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\nFOREIGN KEY/REFERENCES约束中的referential_action为：\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
