{"Entry":{"collection":"sql","key":"create-table","name":"CREATE TABLE","aliases":["createtable"],"metadata":{"aliases":["createtable"],"changed_in":["6.5","7.1","7.2","7.4","8.0","8.1","8.2","8.3","9.0","9.1","9.2","10","11","12","13","14","15","16","18","19"],"changes":[{"from":"6.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["other","usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE [ TEMPORARY | TEMP ] TABLE table (","[ NULL | NOT NULL ] [ UNIQUE ] [ DEFAULT value ]"],"removed":["[ DEFAULT value]","[, NOT NULL ] [ ,UNIQUE ]"]},"to":"6.5"},{"from":"6.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","other","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"7.0"},{"from":"7.0","purpose_changed":false,"renamed":{"from_file":"sql-createtable.htm","to_file":"sql-createtable.html"},"sections":{"added":[],"changed":["description","other","usage","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE [ TEMPORARY | TEMP ] TABLE table_name (","{ column_name type [ column_constraint [ ... ] ]","| table_constraint }  [, ... ]",") [ INHERITS ( parent_table [, ... ] ) ]","where column_constraint can be:","[ CONSTRAINT constraint_name ]","{ NOT NULL | NULL | UNIQUE | PRIMARY KEY | DEFAULT value | CHECK (condition) |","REFERENCES table [ ( column ) ] [ MATCH FULL | MATCH PARTIAL ]","[ ON DELETE action ] [ ON UPDATE action ]","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]","}","and table_constraint can be:","[ CONSTRAINT constraint_name ]","{ UNIQUE ( column_name [, ... ] ) |","PRIMARY KEY ( column_name [, ... ] ) |","CHECK ( condition ) |","FOREIGN KEY ( column_name [, ... ] ) REFERENCES table [ ( column [, ... ] ) ]","[ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ]","[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]","}"],"removed":["CREATE [ TEMPORARY | TEMP ] TABLE table (","column type","[ NULL | NOT NULL ] [ UNIQUE ] [ DEFAULT value ]","[column_constraint_clause | PRIMARY KEY } [ ... ] ]","[, CHECK ( condition ) ]","[, table_constraint_clause ]",") [ INHERITS ( inherited_table [, ...] ) ]"]},"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":["parameters","diagnostics","notes","examples","see_also"],"changed":["description","compatibility"],"removed":["other","usage"]},"status":"changed","synopsis":{"added":["CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (","{ column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]","[ WITH OIDS | WITHOUT OIDS ]","where column_constraint is:","CHECK (expression) |","REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]","[ ON DELETE action ] [ ON UPDATE action ] }","and table_constraint is:","CHECK ( expression ) |","FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]","[ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }"],"removed":["{ column_name type [ column_constraint [ ... ] ]","where column_constraint can be:","{ NOT NULL | NULL | UNIQUE | PRIMARY KEY | DEFAULT value | CHECK (condition) |","REFERENCES table [ ( column ) ] [ MATCH FULL | MATCH PARTIAL ]","}","and table_constraint can be:","CHECK ( condition ) |","FOREIGN KEY ( column_name [, ... ] ) REFERENCES table [ ( column [, ... ] ) ]","}"]},"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","diagnostics","see_also"],"removed":[]},"status":"changed","synopsis":null,"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples","compatibility"],"removed":["diagnostics"]},"status":"changed","synopsis":{"added":["CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (","| LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ] }  [, ... ]","[ WITH OIDS | WITHOUT OIDS ]","[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]","REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]","[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] }"],"removed":["{ column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]"]},"to":"7.4"},{"from":"7.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples","compatibility","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["[ TABLESPACE tablespace ]","UNIQUE [ USING INDEX TABLESPACE tablespace ] |","PRIMARY KEY [ USING INDEX TABLESPACE tablespace ] |","{ UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |","PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |"],"removed":[]},"to":"8.0"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( [","[, ... ]"],"removed":[]},"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["| LIKE parent_table [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS } ] ... }","[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","UNIQUE index_parameters |","PRIMARY KEY index_parameters |","{ UNIQUE ( column_name [, ... ] ) index_parameters |","PRIMARY KEY ( column_name [, ... ] ) index_parameters |","index_parameters in UNIQUE and PRIMARY KEY constraints are:","[ WITH ( storage_parameter [= value] [, ... ] ) ]","[ USING INDEX TABLESPACE tablespace ]"],"removed":["UNIQUE [ USING INDEX TABLESPACE tablespace ] |","PRIMARY KEY [ USING INDEX TABLESPACE tablespace ] |","{ UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |","PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |"]},"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["| LIKE parent_table [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ] ... }"],"removed":[]},"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","examples","compatibility","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["| LIKE parent_table [ like_option ... ] }","CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name","OF type_name [ (","{ column_name WITH OPTIONS [ DEFAULT default_expr ] [ column_constraint [ ... ] ]","| table_constraint }","[, ... ]",") ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]","[ TABLESPACE tablespace ]","CHECK ( expression ) |","{ CHECK ( expression ) |","EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] |","{ INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES | STORAGE | COMMENTS | ALL }","{ column | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]"],"removed":["| LIKE parent_table [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ] ... }","where column_constraint is:","PRIMARY KEY index_parameters |","CHECK ( expression ) |","and table_constraint is:","CHECK ( expression ) |","index_parameters in UNIQUE and PRIMARY KEY constraints are:"]},"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [","{ column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]","CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name","CHECK ( expression ) |","DEFAULT default_expr |"],"removed":["{ column_name data_type [ DEFAULT default_expr ] [ column_constraint [ ... ] ]","{ column_name WITH OPTIONS [ DEFAULT default_expr ] [ column_constraint [ ... ] ]"]},"to":"9.1"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["| LIKE source_table [ like_option ... ] }","[ TABLESPACE tablespace_name ]","[ TABLESPACE tablespace_name ]","CHECK ( expression ) [ NO INHERIT ] |","{ CHECK ( expression ) [ NO INHERIT ] |","[ USING INDEX TABLESPACE tablespace_name ]","{ column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]"],"removed":["| LIKE parent_table [ like_option ... ] }","[ TABLESPACE tablespace ]","[ TABLESPACE tablespace ]","[ USING INDEX TABLESPACE tablespace ]","{ column | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]"]},"to":"9.2"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","examples","see_also"],"removed":[]},"status":"changed","synopsis":null,"to":"9.3"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ INHERITS ( parent_table [, ... ] ) ]","[ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]","{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]","[, ... ]",") ]","[ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]","[ TABLESPACE tablespace_name ]","CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name","PARTITION OF parent_table [ (","{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]","| table_constraint }","[, ... ]",") ] FOR VALUES partition_bound_spec","[ PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]","DEFAULT default_expr |","GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] |","{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }","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 } [, ...] )"],"removed":["{ INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES | STORAGE | COMMENTS | ALL }"]},"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]","[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]",") ] { FOR VALUES partition_bound_spec | DEFAULT }","[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]","TO ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] ) |","WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )","[ INCLUDE ( column_name [, ... ] ) ]"],"removed":[]},"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ USING method ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]","[ USING method ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]","[ USING method ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]","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 ] }","{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }","IN ( partition_bound_expr [, ...] ) |","FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )","TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |"],"removed":["[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","[ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]","[ ON DELETE action ] [ ON UPDATE action ] }","[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] }","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 } [, ...] ) |"]},"to":"12"},{"from":"12","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["{ 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":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["{ column_name data_type [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]","{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }"],"removed":[]},"to":"14"},{"from":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["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":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["{ column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]"],"removed":[]},"to":"16"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["{ 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":{"added":["CREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name ( [","{ column_name data_type [ column_storage ] [ column_compression ] [ COLLATE collation ] [ column_constraint [ ... ] ]","CREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name","CREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name","{ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } } | UNLOGGED","STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }","COMPRESSION compression_method"],"removed":["CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [","{ column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]","CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name","CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name"]},"to":"19"},{"from":"19","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"20"}],"content_hash":"38bdcc288bdcbc86bdb9b2723e0b87bcf59fc63d06f8e1e7ed8ca0c23b393d8f","editorial":{},"first_version":"6.4","group":"table","imported_at":"2026-09-30T17:43:36.155279+08:00","last_version":"20","name":"CREATE TABLE","object":"TABLE","position":8,"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":"define a new table","purpose_zh":"","related":["alter-table","drop-table","create-table-as","create-tablespace","create-type"],"slug":"create-table","source_rev":"a709ab85","synopsis":"CREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ column_storage ] [ column_compression ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name\nOF type_name [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ persistence_mode ] TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ] { FOR VALUES partition_bound_spec | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nwhere persistence_mode is:\n\n{ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } } | UNLOGGED\n\nand column_storage is:\n\nSTORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\n\nand column_compression is:\n\nCOMPRESSION compression_method\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 like_option is:\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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 index_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\nand exclude_element in an EXCLUDE constraint is:\n\n{ column_name | ( expression ) } [ COLLATE collation ] [ opclass [ ( opclass_parameter = value [, ... ] ) ] ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]\n\nand referential_action in a FOREIGN KEY/REFERENCES constraint is:\n\n{ NO ACTION | RESTRICT | CASCADE | SET NULL [ ( column_name [, ... ] ) ] | SET DEFAULT [ ( column_name [, ... ] ) ] }","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-table","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-table","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATETABLE","file":"sql-createtable.html","lang":"en","name":"CREATE TABLE","purpose":"define a new table","purpose_zh":"","related":["alter-table","drop-table","create-table-as","create-tablespace","create-type"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e will create a new, initially empty table in the current database. The table will be owned by the user issuing the command.\u003c/p\u003e\u003cp\u003eIf a schema name is given (for example, \u003ccode class=\"literal\"\u003eCREATE TABLE myschema.mytable ...\u003c/code\u003e) then the table is created in the specified schema. Otherwise it is created in the current schema. Temporary tables exist in a special schema, so a schema name cannot be given when creating a temporary table. The name of the table must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e also automatically creates a data type that represents the composite type corresponding to one row of the table. Therefore, tables cannot have the same name as any existing data type in the same schema.\u003c/p\u003e\u003cp\u003eThe optional constraint clauses specify constraints (tests) that new or updated rows must satisfy for an insert or update operation to succeed. A constraint is an SQL object that helps define the set of valid values in the table in various ways.\u003c/p\u003e\u003cp\u003eThere are two ways to define constraints: table constraints and column constraints. A column constraint is defined as part of a column definition. A table constraint definition is not tied to a particular column, and it can encompass more than one column. Every column constraint can also be written as a table constraint; a column constraint is only a notational convenience for use when the constraint only affects one column.\u003c/p\u003e\u003cp\u003eTo be able to create a table, you must have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on all column types or the type in the \u003ccode class=\"literal\"\u003eOF\u003c/code\u003e clause, respectively.\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\"\u003eTEMPORARY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e below). The default search_path includes the temporary schema first and so identically named existing permanent tables are not chosen for new plans while the temporary table exists, unless they are referenced with schema-qualified names. Any indexes created on a temporary table are automatically temporary as well.\u003c/p\u003e\u003cp\u003eThe \u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. The Autovacuum Daemon\"\u003eautovacuum daemon\u003c/a\u003e cannot access and therefore cannot vacuum or analyze temporary tables. For this reason, appropriate vacuum and analyze operations should be performed via session SQL commands. For example, if a temporary table is going to be used in complex queries, it is wise to run \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e on the temporary table after it is populated.\u003c/p\u003e\u003cp\u003eOptionally, \u003ccode class=\"literal\"\u003eGLOBAL\u003c/code\u003e or \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e can be written before \u003ccode class=\"literal\"\u003eTEMPORARY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e. This presently makes no difference in \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e and is deprecated; see \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-COMPATIBILITY\" title=\"Compatibility\"\u003eCompatibility\u003c/a\u003e below.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf specified, the table is created as an unlogged table. Data written to unlogged tables is not written to the write-ahead log (see \u003ca href=\"/docs/18/wal.html\" title=\"Chapter 28. Reliability and the Write-Ahead Log\"\u003eChapter 28\u003c/a\u003e), which makes them considerably faster than ordinary tables. However, they are not crash-safe: an unlogged table is automatically truncated after a crash or unclean shutdown. The contents of an unlogged table are also not replicated to standby servers. Any indexes created on an unlogged table are automatically unlogged as well.\u003c/p\u003e\u003cp\u003eIf this is specified, any sequences created together with the unlogged table (for identity or serial columns) are also created as unlogged.\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\"\u003eIF NOT EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDo not throw an error if a relation with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing relation is anything like the one that would have been created.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name (optionally schema-qualified) of the table to be created.\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\u003eCreates a \u003cem class=\"firstterm\"\u003etyped table\u003c/em\u003e, which takes its structure from the specified stand-alone composite type (that is, one created using \u003ca href=\"/docs/18/sql-createtype.html\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e) though it still produces a new composite type as well. The table will have a dependency on the referenced type, meaning that cascaded alter and drop actions on that type will propagate to the table.\u003c/p\u003e\u003cp\u003eA typed table always has the same column names and data types as the type it is derived from, so you cannot specify additional columns. But the \u003ccode class=\"literal\"\u003eCREATE TABLE\u003c/code\u003e command can add defaults and constraints to the table, as well as specify storage parameters.\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\u003eThe name of a column to be created in the new 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\u003eThe data type of the column. This can include array specifiers. For more information on the data types supported by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e, refer to \u003ca href=\"/docs/18/datatype.html\" title=\"Chapter 8. Data Types\"\u003eChapter 8\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCOLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eCOLLATE\u003c/code\u003e clause assigns a collation to the column (which must be of a collatable data type). If not specified, the column data type's default collation is used.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSTORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis form sets the storage mode for the 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. 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\"\u003eCOMPRESSION \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\u003eThe \u003ccode class=\"literal\"\u003eCOMPRESSION\u003c/code\u003e clause sets the compression method for the column. Compression is supported only for variable-width data types, and is used only when the column's storage mode is \u003ccode class=\"literal\"\u003emain\u003c/code\u003e or \u003ccode class=\"literal\"\u003eextended\u003c/code\u003e. (See \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e for information on column storage modes.) Setting this property for a partitioned table has no direct effect, because such tables have no storage of their own, but the configured value will be inherited by newly-created partitions. 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 to explicitly specify the default behavior, which is to consult 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\"\u003eINHERITS ( \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\u003eThe optional \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e clause specifies a list of tables from which the new table automatically inherits all columns. Parent tables can be plain tables or foreign tables.\u003c/p\u003e\u003cp\u003eUse of \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e creates a persistent relationship between the new child table and its parent table(s). Schema modifications to the parent(s) normally propagate to children as well, and by default the data of the child table is included in scans of the parent(s).\u003c/p\u003e\u003cp\u003eIf the same column name exists in more than one parent table, an error is reported unless the data types of the columns match in each of the parent tables. If there is no conflict, then the duplicate columns are merged to form a single column in the new table. If the column name list of the new table contains a column name that is also inherited, the data type must likewise match the inherited column(s), and the column definitions are merged into one. If the new table explicitly specifies a default value for the column, this default overrides any defaults from inherited declarations of the column. Otherwise, any parents that specify default values for the column must all specify the same default, or an error will be reported.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints are merged in essentially the same way as columns: if multiple parent tables and/or the new table definition contain identically-named \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints, these constraints must all have the same check expression, or an error will be reported. Constraints having the same name and expression will be merged into one copy. A constraint marked \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e in a parent will not be considered. Notice that an unnamed \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraint in the new table will never be merged, since a unique name will always be chosen for it.\u003c/p\u003e\u003cp\u003eColumn \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e settings are also copied from parent tables.\u003c/p\u003e\u003cp\u003eIf a column in the parent table is an identity column, that property is not inherited. A column in the child table can be declared identity column if desired.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARTITION BY { RANGE | LIST | HASH } ( { \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 ) } [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass\u003c/code\u003e\u003c/em\u003e ] [, ...] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe optional \u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e clause specifies a strategy of partitioning the table. The table thus created is called a \u003cem class=\"firstterm\"\u003epartitioned\u003c/em\u003e table. The parenthesized list of columns or expressions forms the \u003cem class=\"firstterm\"\u003epartition key\u003c/em\u003e for the table. When using range or hash partitioning, the partition key can include multiple columns or expressions (up to 32, but this limit can be altered when building \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e), but for list partitioning, the partition key must consist of a single column or expression.\u003c/p\u003e\u003cp\u003eRange and list partitioning require a btree operator class, while hash partitioning requires a hash operator class. If no operator class is specified explicitly, the default operator class of the appropriate type will be used; if no default operator class exists, an error will be raised. When hash partitioning is used, the operator class used must implement support function 2 (see \u003ca href=\"/docs/18/xindex.html#XINDEX-SUPPORT\" title=\"36.16.3. Index Method Support Routines\"\u003eSection 36.16.3\u003c/a\u003e for details).\u003c/p\u003e\u003cp\u003eA partitioned table is divided into sub-tables (called partitions), which are created using separate \u003ccode class=\"literal\"\u003eCREATE TABLE\u003c/code\u003e commands. The partitioned table is itself empty. A data row inserted into the table is routed to a partition based on the value of columns or expressions in the partition key. If no existing partition matches the values in the new row, an error will be reported.\u003c/p\u003e\u003cp\u003eSee \u003ca href=\"/docs/18/ddl-partitioning.html\" title=\"5.12. Table Partitioning\"\u003eSection 5.12\u003c/a\u003e for more discussion on table partitioning.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARTITION OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\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\u003eCreates the table as a \u003cem class=\"firstterm\"\u003epartition\u003c/em\u003e of the specified parent table. The table can be created either as a partition for specific values using \u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e or as a default partition using \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e. Any indexes, constraints and user-defined row-level triggers that exist in the parent table are cloned on the new partition.\u003c/p\u003e\u003cp\u003eThe \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e must correspond to the partitioning method and partition key of the parent table, and must not overlap with any existing partition of that parent. The form with \u003ccode class=\"literal\"\u003eIN\u003c/code\u003e is used for list partitioning, the form with \u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e and \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e is used for range partitioning, and the form with \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e is used for hash partitioning.\u003c/p\u003e\u003cp\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e is any variable-free expression (subqueries, window functions, aggregate functions, and set-returning functions are not allowed). Its data type must match the data type of the corresponding partition key column. The expression is evaluated once at table creation time, so it can even contain volatile expressions such as \u003ccode class=\"literal\"\u003e\u003ccode class=\"function\"\u003eCURRENT_TIMESTAMP\u003c/code\u003e\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eWhen creating a list partition, \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e can be specified to signify that the partition allows the partition key column to be null. However, there cannot be more than one such list partition for a given parent table. \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e cannot be specified for range partitions.\u003c/p\u003e\u003cp\u003eWhen creating a range partition, the lower bound specified with \u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e is an inclusive bound, whereas the upper bound specified with \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e is an exclusive bound. That is, the values specified in the \u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e list are valid values of the corresponding partition key columns for this partition, whereas those in the \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e list are not. Note that this statement must be understood according to the rules of row-wise comparison (\u003ca href=\"/docs/18/functions-comparisons.html#ROW-WISE-COMPARISON\" title=\"9.25.5. Row Constructor Comparison\"\u003eSection 9.25.5\u003c/a\u003e). For example, given \u003ccode class=\"literal\"\u003ePARTITION BY RANGE (x,y)\u003c/code\u003e, a partition bound \u003ccode class=\"literal\"\u003eFROM (1, 2) TO (3, 4)\u003c/code\u003e allows \u003ccode class=\"literal\"\u003ex=1\u003c/code\u003e with any \u003ccode class=\"literal\"\u003ey\u0026gt;=2\u003c/code\u003e, \u003ccode class=\"literal\"\u003ex=2\u003c/code\u003e with any non-null \u003ccode class=\"literal\"\u003ey\u003c/code\u003e, and \u003ccode class=\"literal\"\u003ex=3\u003c/code\u003e with any \u003ccode class=\"literal\"\u003ey\u0026lt;4\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eThe special values \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e may be used when creating a range partition to indicate that there is no lower or upper bound on the column's value. For example, a partition defined using \u003ccode class=\"literal\"\u003eFROM (MINVALUE) TO (10)\u003c/code\u003e allows any values less than 10, and a partition defined using \u003ccode class=\"literal\"\u003eFROM (10) TO (MAXVALUE)\u003c/code\u003e allows any values greater than or equal to 10.\u003c/p\u003e\u003cp\u003eWhen creating a range partition involving more than one column, it can also make sense to use \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e as part of the lower bound, and \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e as part of the upper bound. For example, a partition defined using \u003ccode class=\"literal\"\u003eFROM (0, MAXVALUE) TO (10, MAXVALUE)\u003c/code\u003e allows any rows where the first partition key column is greater than 0 and less than or equal to 10. Similarly, a partition defined using \u003ccode class=\"literal\"\u003eFROM ('a', MINVALUE) TO ('b', MINVALUE)\u003c/code\u003e allows any rows where the first partition key column starts with \"a\".\u003c/p\u003e\u003cp\u003eNote that if \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e or \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e is used for one column of a partitioning bound, the same value must be used for all subsequent columns. For example, \u003ccode class=\"literal\"\u003e(10, MINVALUE, 0)\u003c/code\u003e is not a valid bound; you should write \u003ccode class=\"literal\"\u003e(10, MINVALUE, MINVALUE)\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eAlso note that some element types, such as \u003ccode class=\"literal\"\u003etimestamp\u003c/code\u003e, have a notion of \"infinity\", which is just another value that can be stored. This is different from \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e, which are not real values that can be stored, but rather they are ways of saying that the value is unbounded. \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e can be thought of as being greater than any other value, including \"infinity\" and \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e as being less than any other value, including \"minus infinity\". Thus the range \u003ccode class=\"literal\"\u003eFROM ('infinity') TO (MAXVALUE)\u003c/code\u003e is not an empty range; it allows precisely one value to be stored — \"infinity\".\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e is specified, the table will be created as the default partition of the parent table. This option is not available for hash-partitioned tables. A partition key value not fitting into any other partition of the given parent will be routed to the default partition.\u003c/p\u003e\u003cp\u003eWhen a table has an existing \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e partition and a new partition is added to it, the default partition must be scanned to verify that it does not contain any rows which properly belong in the new partition. If the default partition contains a large number of rows, this may be slow. The scan will be skipped if the default partition is a foreign table or if it has a constraint which proves that it cannot contain rows which should be placed in the new partition.\u003c/p\u003e\u003cp\u003eWhen creating a hash partition, a modulus and remainder must be specified. The modulus must be a positive integer, and the remainder must be a non-negative integer less than the modulus. Typically, when initially setting up a hash-partitioned table, you should choose a modulus equal to the number of partitions and assign every table the same modulus and a different remainder (see examples, below). However, it is not required that every partition have the same modulus, only that every modulus which occurs among the partitions of a hash-partitioned table is a factor of the next larger modulus. This allows the number of partitions to be increased incrementally without needing to move all the data at once. For example, suppose you have a hash-partitioned table with 8 partitions, each of which has modulus 8, but find it necessary to increase the number of partitions to 16. You can detach one of the modulus-8 partitions, create two new modulus-16 partitions covering the same portion of the key space (one with a remainder equal to the remainder of the detached partition, and the other with a remainder equal to that value plus 8), and repopulate them with data. You can then repeat this -- perhaps at a later time -- for each modulus-8 partition until none remain. While this may still involve a large amount of data movement at each step, it is still better than having to create a whole new table and move all the data at once.\u003c/p\u003e\u003cp\u003eA partition must have the same column names and types as the partitioned table to which it belongs. Modifications to the column names or types of a partitioned table will automatically propagate to all partitions. \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints will be inherited automatically by every partition, but an individual partition may specify additional \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints; additional constraints with the same name and condition as in the parent will be merged with the parent constraint. Defaults may be specified separately for each partition. But note that a partition's default value is not applied when inserting a tuple through a partitioned table.\u003c/p\u003e\u003cp\u003eRows inserted into a partitioned table will be automatically routed to the correct partition. If no suitable partition exists, an error will occur.\u003c/p\u003e\u003cp\u003eOperations such as \u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e which normally affect a table and all of its inheritance children will cascade to all partitions, but may also be performed on an individual partition.\u003c/p\u003e\u003cp\u003eNote that creating a partition using \u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e requires taking an \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock on the parent partitioned table. Likewise, dropping a partition with \u003ccode class=\"command\"\u003eDROP TABLE\u003c/code\u003e requires taking an \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e lock on the parent table. It is possible to use \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003ccode class=\"command\"\u003eALTER TABLE ATTACH/DETACH PARTITION\u003c/code\u003e\u003c/a\u003e to perform these operations with a weaker lock, thus reducing interference with concurrent operations on the partitioned table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eLIKE \u003cem class=\"replaceable\"\u003e\u003ccode\u003esource_table\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e ... ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints.\u003c/p\u003e\u003cp\u003eUnlike \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e, the new table and original table are completely decoupled after creation is complete. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table.\u003c/p\u003e\u003cp\u003eAlso unlike \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e, columns and constraints copied by \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e are not merged with similarly named columns and constraints. If the same name is specified explicitly or in another \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e clause, an error is signaled.\u003c/p\u003e\u003cp\u003eThe optional \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e clauses specify which additional properties of the original table to copy. Specifying \u003ccode class=\"literal\"\u003eINCLUDING\u003c/code\u003e copies the property, specifying \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e omits the property. \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e is the default. If multiple specifications are made for the same kind of object, the last one is used. The available options are:\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING COMMENTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eComments for the copied columns, check constraints, not-null constraints, indexes, and extended statistics will be copied. The default behavior is to exclude comments, resulting in the corresponding objects in the new table having no comments.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING COMPRESSION\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eCompression method of the columns will be copied. The default behavior is to exclude compression methods, resulting in columns having the default compression method.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING CONSTRAINTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints will be copied. No distinction is made between column constraints and table constraints. Not-null constraints are always copied to the new table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING DEFAULTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDefault expressions for the copied column definitions will be copied. Otherwise, default expressions are not copied, resulting in the copied columns in the new table having null defaults. Note that copying defaults that call database-modification functions, such as \u003ccode class=\"function\"\u003enextval\u003c/code\u003e, may create a functional linkage between the original and new tables.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING GENERATED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAny generation expressions as well as the stored/virtual choice of copied column definitions will be copied. By default, new columns will be regular base columns.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAny identity specifications of copied column definitions will be copied. A new sequence is created for each identity column of the new table, separate from the sequences associated with the old table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING INDEXES\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIndexes, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraints on the original table will be created on the new table. Names for the new indexes and constraints are chosen according to the default rules, regardless of how the originals were named. (This behavior avoids possible duplicate-name failures for the new indexes.)\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING STATISTICS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eExtended statistics are copied to the new table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING STORAGE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e settings for the copied column definitions will be copied. The default behavior is to exclude \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e settings, resulting in the copied columns in the new table having type-specific default settings. For more on \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e settings, see \u003ca href=\"/docs/18/storage-toast.html\" title=\"66.2. TOAST\"\u003eSection 66.2\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING ALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eINCLUDING ALL\u003c/code\u003e is an abbreviated form selecting all the available individual options. (It could be useful to write individual \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e clauses after \u003ccode class=\"literal\"\u003eINCLUDING ALL\u003c/code\u003e to select all but some specific options.)\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e clause can also be used to copy column definitions from views, foreign tables, or composite types. Inapplicable options (e.g., \u003ccode class=\"literal\"\u003eINCLUDING INDEXES\u003c/code\u003e from a view) are ignored.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAn optional name for a column or table constraint. If the constraint is violated, the constraint name is present in error messages, so constraint names like \u003ccode class=\"literal\"\u003ecol must be positive\u003c/code\u003e can be used to communicate helpful constraint information to client applications. (Double-quotes are needed to specify constraint names that contain spaces.) If a constraint name is not specified, the system generates a name.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT NULL [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe column is not allowed to contain null values.\u003c/p\u003e\u003cp\u003eA constraint marked with \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e will not propagate to child tables.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe column is allowed to contain null values. This is the default.\u003c/p\u003e\u003cp\u003eThis clause is only provided for compatibility with non-standard SQL databases. Its use is discouraged in new applications.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e clause specifies an expression producing a Boolean result which new or updated rows must satisfy for an insert or update operation to succeed. Expressions evaluating to TRUE or UNKNOWN succeed. Should any row of an insert or update operation produce a FALSE result, an error exception is raised and the insert or update does not alter the database. A check constraint specified as a column constraint should reference that column's value only, while an expression appearing in a table constraint can reference multiple columns.\u003c/p\u003e\u003cp\u003eCurrently, \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e expressions cannot contain subqueries nor refer to variables other than columns of the current row (see \u003ca href=\"/docs/18/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS\" title=\"5.5.1. Check Constraints\"\u003eSection 5.5.1\u003c/a\u003e). The system column \u003ccode class=\"literal\"\u003etableoid\u003c/code\u003e may be referenced, but not any other system column.\u003c/p\u003e\u003cp\u003eA constraint marked with \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e will not propagate to child tables.\u003c/p\u003e\u003cp\u003eWhen a table has multiple \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints, they will be tested for each row in alphabetical order by name, after checking \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraints. (\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e versions before 9.5 did not honor any particular firing order for \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints.)\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003edefault_expr\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e clause assigns a default data value for the column whose column definition it appears within. The value is any variable-free expression (in particular, cross-references to other columns in the current table are not allowed). Subqueries are not allowed either. The data type of the default expression must match the data type of the column.\u003c/p\u003e\u003cp\u003eThe default expression will be used in any insert operation that does not specify a value for the column. If there is no default for a column, then the default is null.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eGENERATED ALWAYS AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003egeneration_expr\u003c/code\u003e\u003c/em\u003e ) [ STORED | VIRTUAL ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis clause creates the column as a \u003cem class=\"firstterm\"\u003egenerated column\u003c/em\u003e. The column cannot be written to, and when read the result of the specified expression will be returned.\u003c/p\u003e\u003cp\u003eWhen \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e is specified, the column will be computed when it is read, and it will not occupy any storage. When \u003ccode class=\"literal\"\u003eSTORED\u003c/code\u003e is specified, the column will be computed on write and will be stored on disk. \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e is the default.\u003c/p\u003e\u003cp\u003eThe generation expression can refer to other columns in the table, but not other generated columns. Any functions and operators used must be immutable. References to other tables are not allowed.\u003c/p\u003e\u003cp\u003eA virtual generated column cannot have a user-defined type, and the generation expression of a virtual generated column must not reference user-defined functions or types, that is, it can only use built-in functions or types. This applies also indirectly, such as for functions or types that underlie operators or casts. (This restriction does not exist for stored generated columns.)\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis clause creates the column as an \u003cem class=\"firstterm\"\u003eidentity column\u003c/em\u003e. It will have an implicit sequence attached to it and in newly-inserted rows the column will automatically have values from the sequence assigned to it. Such a column is implicitly \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eThe clauses \u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e and \u003ccode class=\"literal\"\u003eBY DEFAULT\u003c/code\u003e determine how explicitly user-specified values are handled in \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e and \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e commands.\u003c/p\u003e\u003cp\u003eIn an \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e command, if \u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e is selected, a user-specified value is only accepted if the \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e statement specifies \u003ccode class=\"literal\"\u003eOVERRIDING SYSTEM VALUE\u003c/code\u003e. If \u003ccode class=\"literal\"\u003eBY DEFAULT\u003c/code\u003e is selected, then the user-specified value takes precedence. See \u003ca href=\"/docs/18/sql-insert.html\" title=\"INSERT\"\u003e\u003cspan class=\"refentrytitle\"\u003eINSERT\u003c/span\u003e\u003c/a\u003e for details. (In the \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e command, user-specified values are always used regardless of this setting.)\u003c/p\u003e\u003cp\u003eIn an \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e command, if \u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e is selected, any update of the column to any value other than \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e will be rejected. If \u003ccode class=\"literal\"\u003eBY DEFAULT\u003c/code\u003e is selected, the column can be updated normally. (There is no \u003ccode class=\"literal\"\u003eOVERRIDING\u003c/code\u003e clause for the \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e command.)\u003c/p\u003e\u003cp\u003eThe optional \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e clause can be used to override the parameters of the sequence. The available options include those shown for \u003ca href=\"/docs/18/sql-createsequence.html\" title=\"CREATE SEQUENCE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SEQUENCE\u003c/span\u003e\u003c/a\u003e, plus \u003ccode class=\"literal\"\u003eSEQUENCE NAME \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e, \u003ccode class=\"literal\"\u003eLOGGED\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e, which allow selection of the name and persistence level of the sequence. Without \u003ccode class=\"literal\"\u003eSEQUENCE NAME\u003c/code\u003e, the system chooses an unused name for the sequence. Without \u003ccode class=\"literal\"\u003eLOGGED\u003c/code\u003e or \u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e, the sequence will have the same persistence level as the table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNIQUE [ NULLS [ NOT ] DISTINCT ]\u003c/code\u003e (column constraint)\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNIQUE [ 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 ] )\u003c/code\u003e [ \u003cspan class=\"optional\"\u003e\u003ccode class=\"literal\"\u003eINCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ] (table constraint)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e constraint specifies that a group of one or more columns of a table can contain only unique values. The behavior of a unique table constraint is the same as that of a unique column constraint, with the additional capability to span multiple columns. The constraint therefore enforces that any two rows must differ in at least one of these columns.\u003c/p\u003e\u003cp\u003eIf the \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e option is specified for the last column, then that column is checked for overlaps instead of equality. In that case, the other columns of the constraint will allow duplicates so long as the duplicates don't overlap in the \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e column. (This is sometimes called a temporal key, if the column is a range of dates or timestamps, but \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e allows ranges over any base type.) In effect, such a constraint is enforced with an \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraint rather than a \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e constraint. So for example \u003ccode class=\"literal\"\u003eUNIQUE (id, valid_at WITHOUT OVERLAPS)\u003c/code\u003e behaves like \u003ccode class=\"literal\"\u003eEXCLUDE USING GIST (id WITH =, valid_at WITH \u0026amp;\u0026amp;)\u003c/code\u003e. The \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e column must have a range or multirange type. Empty ranges/multiranges are not permitted. The non-\u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e columns of the constraint can be any type that can be compared for equality in a GiST index. By default, only range types are supported, but you can use other types by adding the \u003ca href=\"/docs/18/btree-gist.html\" title=\"F.8. btree_gist — GiST operator classes with B-tree behavior\"\u003ebtree_gist\u003c/a\u003e extension (which is the expected way to use this feature).\u003c/p\u003e\u003cp\u003eFor the purpose of a unique constraint, null values are not considered equal, unless \u003ccode class=\"literal\"\u003eNULLS NOT DISTINCT\u003c/code\u003e is specified.\u003c/p\u003e\u003cp\u003eEach unique constraint should name a set of columns that is different from the set of columns named by any other unique or primary key constraint defined for the table. (Otherwise, redundant unique constraints will be discarded.)\u003c/p\u003e\u003cp\u003eWhen establishing a unique constraint for a multi-level partition hierarchy, all the columns in the partition key of the target partitioned table, as well as those of all its descendant partitioned tables, must be included in the constraint definition.\u003c/p\u003e\u003cp\u003eAdding a unique constraint will automatically create a unique btree index on the column or group of columns used in the constraint. But if the constraint includes a \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e clause, it will use a GiST index. The created index has the same name as the unique constraint.\u003c/p\u003e\u003cp\u003eThe optional \u003ccode class=\"literal\"\u003eINCLUDE\u003c/code\u003e clause adds to that index one or more columns that are simply \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003epayload\u003c/span\u003e”\u003c/span\u003e: uniqueness is not enforced on them, and the index cannot be searched on the basis of those columns. However they can be retrieved by an index-only scan. Note that although the constraint is not enforced on included columns, it still depends on them. Consequently, some operations on such columns (e.g., \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e) can cause cascaded constraint and index deletion.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e (column constraint)\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRIMARY 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 ] )\u003c/code\u003e [ \u003cspan class=\"optional\"\u003e\u003ccode class=\"literal\"\u003eINCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ] (table constraint)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e constraint specifies that a column or columns of a table can contain only unique (non-duplicate), nonnull values. Only one primary key can be specified for a table, whether as a column constraint or a table constraint.\u003c/p\u003e\u003cp\u003eThe primary key constraint should name a set of columns that is different from the set of columns named by any unique constraint defined for the same table. (Otherwise, the unique constraint is redundant and will be discarded.)\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e enforces the same data constraints as a combination of \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e. However, identifying a set of columns as the primary key also provides metadata about the design of the schema, since a primary key implies that other tables can rely on this set of columns as a unique identifier for rows.\u003c/p\u003e\u003cp\u003eWhen placed on a partitioned table, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e constraints share the restrictions previously described for \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e constraints.\u003c/p\u003e\u003cp\u003eAdding a \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e constraint will automatically create a unique btree index on the column or group of columns used in the constraint, or GiST if \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e was specified.\u003c/p\u003e\u003cp\u003eThe optional \u003ccode class=\"literal\"\u003eINCLUDE\u003c/code\u003e clause adds to that index one or more columns that are simply \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003epayload\u003c/span\u003e”\u003c/span\u003e: uniqueness is not enforced on them, and the index cannot be searched on the basis of those columns. However they can be retrieved by an index-only scan. Note that although the constraint is not enforced on included columns, it still depends on them. Consequently, some operations on such columns (e.g., \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e) can cause cascaded constraint and index deletion.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eEXCLUDE [ 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 ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e clause defines an exclusion constraint, which guarantees that if any two rows are compared on the specified column(s) or expression(s) using the specified operator(s), not all of these comparisons will return \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e. If all of the specified operators test for equality, this is equivalent to a \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e constraint, although an ordinary unique constraint will be faster. However, exclusion constraints can specify constraints that are more general than simple equality. For example, you can specify a constraint that no two rows in the table contain overlapping circles (see \u003ca href=\"/docs/18/datatype-geometric.html\" title=\"8.8. Geometric Types\"\u003eSection 8.8\u003c/a\u003e) by using the \u003ccode class=\"literal\"\u003e\u0026amp;\u0026amp;\u003c/code\u003e operator. The operator(s) are required to be commutative.\u003c/p\u003e\u003cp\u003eExclusion constraints are implemented using an index that has the same name as the constraint, so each specified operator must be associated with an appropriate operator class (see \u003ca href=\"/docs/18/indexes-opclass.html\" title=\"11.10. Operator Classes and Operator Families\"\u003eSection 11.10\u003c/a\u003e) for the index access method \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e. Each \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e defines a column of the index, so it can optionally specify a collation, an operator class, operator class parameters, and/or ordering options; these are described fully under \u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eThe access method must support \u003ccode class=\"literal\"\u003eamgettuple\u003c/code\u003e (see \u003ca href=\"/docs/18/indexam.html\" title=\"Chapter 63. Index Access Method Interface Definition\"\u003eChapter 63\u003c/a\u003e); at present this means \u003cacronym\u003eGIN\u003c/acronym\u003e cannot be used. Although it's allowed, there is little point in using B-tree or hash indexes with an exclusion constraint, because this does nothing that an ordinary unique constraint doesn't do better. So in practice the access method will always be \u003cacronym\u003eGiST\u003c/acronym\u003e or \u003cacronym\u003eSP-GiST\u003c/acronym\u003e.\u003c/p\u003e\u003cp\u003eThe \u003cem class=\"replaceable\"\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e allows you to specify an exclusion constraint on a subset of the table; internally this creates a partial index. Note that parentheses are required around the predicate.\u003c/p\u003e\u003cp\u003eWhen establishing an exclusion constraint for a multi-level partition hierarchy, all the columns in the partition key of the target partitioned table, as well as those of all its descendant partitioned tables, must be included in the constraint definition. Additionally, those columns must be compared using the equality operator. These restrictions ensure that potentially-conflicting rows will exist in the same partition. The constraint may also refer to other columns which are not a part of any partition key, which can be compared using any appropriate operator.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eREFERENCES \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 \u003cem class=\"replaceable\"\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ 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 ]\u003c/code\u003e (column constraint)\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFOREIGN 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 ] ) ] [ MATCH \u003cem class=\"replaceable\"\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ 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 ]\u003c/code\u003e (table constraint)\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThese clauses specify a foreign key constraint, which requires that a group of one or more columns of the new table must only contain values that match values in the referenced column(s) of some row of the referenced table. If the \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e list is omitted, the primary key of the \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e is used. Otherwise, the \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e list must refer to the columns of a non-deferrable unique or primary key constraint or be the columns of a non-partial unique index.\u003c/p\u003e\u003cp\u003eIf the last column is marked with \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e, it is treated in a special way. While the non-\u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e columns are compared for equality (and there must be at least one of them), the \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e column is not. Instead, the constraint is considered satisfied if the referenced table has matching records (based on the non-\u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e parts of the key) whose combined \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e values completely cover the referencing record's. In other words, the reference must have a referent for its entire duration. This column must be a range or multirange type. In addition, the referenced table must have a primary key or unique constraint declared with \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e. Finally, if the foreign key has a PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e specification the corresponding \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e, if present, must also be marked \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e. If the \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e clause is omitted, and thus the reftable's primary key constraint chosen, the primary key must have its final column marked \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eFor each pair of referencing and referenced column, if they are of a collatable data type, then the collations must either be both deterministic or else both the same. This ensures that both columns have a consistent notion of equality.\u003c/p\u003e\u003cp\u003eThe user must have \u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e permission on the referenced table (either the whole table, or the specific referenced columns). The addition of a foreign key constraint requires a \u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e lock on the referenced table. Note that foreign key constraints cannot be defined between temporary tables and permanent tables.\u003c/p\u003e\u003cp\u003eA value inserted into the referencing column(s) is matched against the values of the referenced table and referenced columns using the given match type. There are three match types: \u003ccode class=\"literal\"\u003eMATCH FULL\u003c/code\u003e, \u003ccode class=\"literal\"\u003eMATCH PARTIAL\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eMATCH SIMPLE\u003c/code\u003e (which is the default). \u003ccode class=\"literal\"\u003eMATCH FULL\u003c/code\u003e will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null; if they are all null, the row is not required to have a match in the referenced table. \u003ccode class=\"literal\"\u003eMATCH SIMPLE\u003c/code\u003e allows any of the foreign key columns to be null; if any of them are null, the row is not required to have a match in the referenced table. \u003ccode class=\"literal\"\u003eMATCH PARTIAL\u003c/code\u003e is not yet implemented. (Of course, \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraints can be applied to the referencing column(s) to prevent these cases from arising.)\u003c/p\u003e\u003cp\u003eIn addition, when the data in the referenced columns is changed, certain actions are performed on the data in this table's columns. The \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e clause specifies the action to perform when a referenced row in the referenced table is being deleted. Likewise, the \u003ccode class=\"literal\"\u003eON UPDATE\u003c/code\u003e clause specifies the action to perform when a referenced column in the referenced table is being updated to a new value. If the row is updated, but the referenced column is not actually changed, no action is done. Referential actions are executed as part of the data changing command, even if the constraint is deferred. There are the following possible actions for each clause:\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO ACTION\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eProduce an error if the deletion or update would create a foreign key constraint violation. If the constraint is deferred, this error will be produced at constraint check time if there still exist any referencing rows. This is the default action.\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\u003eProduce an error if a row to be deleted or updated matches a row in the referencing table. This prevents the action even if the state after the action would not violate the foreign key constraint. In particular, it prevents updates of referenced rows to values that are distinct but compare as equal. (But it does not prevent \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eno-op\u003c/span\u003e”\u003c/span\u003e updates that update a column to the same value.)\u003c/p\u003e\u003cp\u003eIn a temporal foreign key, this option is not supported.\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\u003eDelete any rows referencing the deleted row, or update the values of the referencing column(s) to the new values of the referenced columns, respectively.\u003c/p\u003e\u003cp\u003eIn a temporal foreign key, this option is not supported.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET NULL [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSet all of the referencing columns, or a specified subset of the referencing columns, to null. A subset of columns can only be specified for \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e actions.\u003c/p\u003e\u003cp\u003eIn a temporal foreign key, this option is not supported.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET DEFAULT [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eSet all of the referencing columns, or a specified subset of the referencing columns, to their default values. A subset of columns can only be specified for \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e actions. (There must be a row in the referenced table matching the default values, if they are not null, or the operation will fail.)\u003c/p\u003e\u003cp\u003eIn a temporal foreign key, this option is not supported.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eIf the referenced column(s) are changed frequently, it might be wise to add an index to the referencing column(s) so that referential actions associated with the foreign key constraint can be performed more efficiently.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFERRABLE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT DEFERRABLE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis controls whether the constraint can be deferred. A constraint that is not deferrable will be checked immediately after every command. Checking of constraints that are deferrable can be postponed until the end of the transaction (using the \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\"\u003e\u003ccode class=\"command\"\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e command). \u003ccode class=\"literal\"\u003eNOT DEFERRABLE\u003c/code\u003e is the default. Currently, only \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e (foreign key) constraints accept this clause. \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e and \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints are not deferrable. Note that deferrable constraints cannot be used as conflict arbiters in an \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e statement that includes an \u003ccode class=\"literal\"\u003eON CONFLICT\u003c/code\u003e clause.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINITIALLY DEFERRED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf a constraint is deferrable, this clause specifies the default time to check the constraint. If the constraint is \u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e, it is checked after each statement. This is the default. If the constraint is \u003ccode class=\"literal\"\u003eINITIALLY DEFERRED\u003c/code\u003e, it is checked only at the end of the transaction. The constraint check time can be altered with the \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\"\u003e\u003ccode class=\"command\"\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e command.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eENFORCED\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eWhen the constraint is \u003ccode class=\"literal\"\u003eENFORCED\u003c/code\u003e, then the database system will ensure that the constraint is satisfied, by checking the constraint at appropriate times (after each statement or at the end of the transaction, as appropriate). That is the default. If the constraint is \u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e, the database system will not check the constraint. It is then up to the application code to ensure that the constraints are satisfied. The database system might still assume that the data actually satisfies the constraint for optimization decisions where this does not affect the correctness of the result.\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e constraints can be useful as documentation if the actual checking of the constraint at run time is too expensive.\u003c/p\u003e\u003cp\u003eThis is currently only supported for foreign key and \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e constraints.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis optional clause specifies the table access method to use to store the contents for the new table; the method needs be an access method of type \u003ccode class=\"literal\"\u003eTABLE\u003c/code\u003e. See \u003ca href=\"/docs/18/tableam.html\" title=\"Chapter 62. Table Access Method Interface Definition\"\u003eChapter 62\u003c/a\u003e for more information. If this option is not specified, the default table access method is chosen for the new table. See \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e for more information.\u003c/p\u003e\u003cp\u003eWhen creating a partition, the table access method is the access method of its partitioned table, if set.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITH ( \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 clause specifies optional storage parameters for a table or index; see \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"Storage Parameters\"\u003eStorage Parameters\u003c/a\u003e below for more information. For backward-compatibility the \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e clause for a table can also include \u003ccode class=\"literal\"\u003eOIDS=FALSE\u003c/code\u003e to specify that rows of the new table should not contain OIDs (object identifiers), \u003ccode class=\"literal\"\u003eOIDS=TRUE\u003c/code\u003e is not supported anymore.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis is backward-compatible syntax for declaring a table \u003ccode class=\"literal\"\u003eWITHOUT OIDS\u003c/code\u003e, creating a table \u003ccode class=\"literal\"\u003eWITH OIDS\u003c/code\u003e is not supported anymore.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe behavior of temporary tables at the end of a transaction block can be controlled using \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e. The three options are:\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRESERVE ROWS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eNo special action is taken at the ends of transactions. This is the default behavior.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDELETE ROWS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAll rows in the temporary table will be deleted at the end of each transaction block. Essentially, an automatic \u003ca href=\"/docs/18/sql-truncate.html\" title=\"TRUNCATE\"\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e\u003c/a\u003e is done at each commit. When used on a partitioned table, this is not cascaded to its partitions.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe temporary table will be dropped at the end of the current transaction block. When used on a partitioned table, this action drops its partitions and when used on tables with inheritance children, it drops the dependent children.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eTABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e is the name of the tablespace in which the new table is to be created. If not specified, \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\"\u003edefault_tablespace\u003c/a\u003e is consulted, or \u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\"\u003etemp_tablespaces\u003c/a\u003e if the table is temporary. For partitioned tables, since no storage is required for the table itself, the tablespace specified overrides \u003ccode class=\"literal\"\u003edefault_tablespace\u003c/code\u003e as the default tablespace to use for any newly created partitions when no other tablespace is explicitly specified.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING INDEX TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis clause allows selection of the tablespace in which the index associated with a \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, or \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraint will be created. If not specified, \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\"\u003edefault_tablespace\u003c/a\u003e is consulted, or \u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\"\u003etemp_tablespaces\u003c/a\u003e if the table is temporary.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eStorage Parameters\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e clause can specify \u003cem class=\"firstterm\"\u003estorage parameters\u003c/em\u003e for tables, and for indexes associated with a \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, or \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraint. Storage parameters for indexes are documented in \u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e. The storage parameters currently available for tables are listed below. For many of these parameters, as shown, there is an additional parameter with the same name prefixed with \u003ccode class=\"literal\"\u003etoast.\u003c/code\u003e, which controls the behavior of the table's secondary \u003cacronym\u003eTOAST\u003c/acronym\u003e table, if any (see \u003ca href=\"/docs/18/storage-toast.html\" title=\"66.2. TOAST\"\u003eSection 66.2\u003c/a\u003e for more information about TOAST). If a table parameter value is set and the equivalent \u003ccode class=\"literal\"\u003etoast.\u003c/code\u003e parameter is not, the TOAST table will use the table's parameter value. Specifying these parameters for partitioned tables is not supported, but you may specify them for individual leaf partitions.\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"varname\"\u003efillfactor\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe fillfactor for a table is a percentage between 10 and 100. 100 (complete packing) is the default. When a smaller fillfactor is specified, \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e operations pack table pages only to the indicated percentage; the remaining space on each page is reserved for updating rows on that page. This gives \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e a chance to place the updated copy of a row on the same page as the original, which is more efficient than placing it on a different page, and makes \u003ca href=\"/docs/18/storage-hot.html\" title=\"66.7. Heap-Only Tuples (HOT)\"\u003eheap-only tuple updates\u003c/a\u003e more likely. For a table whose entries are never updated, complete packing is the best choice, but in heavily updated tables smaller fillfactors are appropriate. This parameter cannot be set for TOAST tables.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003etoast_tuple_target\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe toast_tuple_target specifies the minimum tuple length required before we try to compress and/or move long column values into TOAST tables, and is also the target length we try to reduce the length below once toasting begins. This affects columns marked as External (for move), Main (for compression), or Extended (for both) and applies only to new tuples. There is no effect on existing rows. By default this parameter is set to allow at least 4 tuples per block, which with the default block size will be 2040 bytes. Valid values are between 128 bytes and the (block size - header), by default 8160 bytes. Changing this value may not be useful for very short or very long rows. Note that the default setting is often close to optimal, and it is possible that setting this parameter could have negative effects in some cases. This parameter cannot be set for TOAST tables.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eparallel_workers\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThis sets the number of workers that should be used to assist a parallel scan of this table. If not set, the system will determine a value based on the relation size. The actual number of workers chosen by the planner or by utility statements that use parallel scans may be less, for example due to the setting of \u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES\"\u003emax_worker_processes\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_enabled\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_enabled\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eEnables or disables the autovacuum daemon for a particular table. If true, the autovacuum daemon will perform automatic \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e and/or \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e operations on this table following the rules discussed in \u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. The Autovacuum Daemon\"\u003eSection 24.1.6\u003c/a\u003e. If false, this table will not be autovacuumed, except to prevent transaction ID wraparound. See \u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. Preventing Transaction ID Wraparound Failures\"\u003eSection 24.1.5\u003c/a\u003e for more about wraparound prevention. Note that the autovacuum daemon does not run at all (except to prevent transaction ID wraparound) if the \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM\"\u003eautovacuum\u003c/a\u003e parameter is false; setting individual tables' storage parameters does not override that. Therefore there is seldom much point in explicitly setting this storage parameter to \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e, only to \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_index_cleanup\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_index_cleanup\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eForces or disables index cleanup when \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e is run on this table. The default value is \u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e. With \u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e, index cleanup is disabled, with \u003ccode class=\"literal\"\u003eON\u003c/code\u003e it is enabled, and with \u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e a decision is made dynamically, each time \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e runs. The dynamic behavior allows \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to avoid needlessly scanning indexes to remove very few dead tuples. Forcibly disabling all index cleanup can speed up \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e very significantly, but may also lead to severely bloated indexes if table modifications are frequent. The \u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e parameter of \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e\u003c/a\u003e, if specified, overrides the value of this option.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_truncate\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_truncate\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\"\u003evacuum_truncate\u003c/a\u003e parameter. The \u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e parameter of \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e\u003c/a\u003e, if specified, overrides the value of this option.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD\"\u003eautovacuum_vacuum_threshold\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_max_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_max_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-MAX-THRESHOLD\"\u003eautovacuum_vacuum_max_threshold\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_scale_factor\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR\"\u003eautovacuum_vacuum_scale_factor\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_insert_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_insert_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-THRESHOLD\"\u003eautovacuum_vacuum_insert_threshold\u003c/a\u003e parameter. The special value of -1 may be used to disable insert vacuums on the table.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_insert_scale_factor\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_insert_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-SCALE-FACTOR\"\u003eautovacuum_vacuum_insert_scale_factor\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_analyze_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-THRESHOLD\"\u003eautovacuum_analyze_threshold\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_analyze_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-SCALE-FACTOR\"\u003eautovacuum_analyze_scale_factor\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_cost_delay\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_cost_delay\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-DELAY\"\u003eautovacuum_vacuum_cost_delay\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_cost_limit\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_cost_limit\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-LIMIT\"\u003eautovacuum_vacuum_cost_limit\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_min_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_min_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\"\u003evacuum_freeze_min_age\u003c/a\u003e parameter. Note that autovacuum will ignore per-table \u003ccode class=\"literal\"\u003eautovacuum_freeze_min_age\u003c/code\u003e parameters that are larger than half the system-wide \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\"\u003eautovacuum_freeze_max_age\u003c/a\u003e setting.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_max_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_max_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\"\u003eautovacuum_freeze_max_age\u003c/a\u003e parameter. Note that autovacuum will ignore per-table \u003ccode class=\"literal\"\u003eautovacuum_freeze_max_age\u003c/code\u003e parameters that are larger than the system-wide setting (it can only be set smaller).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_table_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_table_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\"\u003evacuum_freeze_table_age\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_min_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-MIN-AGE\"\u003evacuum_multixact_freeze_min_age\u003c/a\u003e parameter. Note that autovacuum will ignore per-table \u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e parameters that are larger than half the system-wide \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e setting.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_max_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e parameter. Note that autovacuum will ignore per-table \u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e parameters that are larger than the system-wide setting (it can only be set smaller).\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_table_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_table_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-TABLE-AGE\"\u003evacuum_multixact_freeze_table_age\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003elog_autovacuum_min_duration\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.log_autovacuum_min_duration\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-logging.html#GUC-LOG-AUTOVACUUM-MIN-DURATION\"\u003elog_autovacuum_min_duration\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_max_eager_freeze_failure_rate\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_max_eager_freeze_failure_rate\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003ePer-table value for \u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MAX-EAGER-FREEZE-FAILURE-RATE\"\u003evacuum_max_eager_freeze_failure_rate\u003c/a\u003e parameter.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003euser_catalog_table\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDeclare the table as an additional catalog table for purposes of logical replication. See \u003ca href=\"/docs/18/logicaldecoding-output-plugin.html#LOGICALDECODING-CAPABILITIES\" title=\"47.6.2. Capabilities\"\u003eSection 47.6.2\u003c/a\u003e for details. This parameter cannot be set for TOAST tables.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e automatically creates an index for each unique constraint and primary key constraint to enforce uniqueness. Thus, it is not necessary to create an index explicitly for primary key columns. (See \u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e for more information.)\u003c/p\u003e\u003cp\u003eUnique constraints and primary keys are not inherited in the current implementation. This makes the combination of inheritance and unique constraints rather dysfunctional.\u003c/p\u003e\u003cp\u003eA table cannot have more than 1600 columns. (In practice, the effective limit is usually lower because of tuple-length constraints.)\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eCreate table \u003ccode class=\"structname\"\u003efilms\u003c/code\u003e and table \u003ccode class=\"structname\"\u003edistributors\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5) CONSTRAINT firstkey PRIMARY KEY,\n    title       varchar(40) NOT NULL,\n    did         integer NOT NULL,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute\n);\n\nCREATE TABLE distributors (\n     did    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,\n     name   varchar(40) NOT NULL CHECK (name \u0026lt;\u0026gt; '')\n);\n\u003c/pre\u003e\u003cp\u003eCreate a table with a 2-dimensional array:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE array_int (\n    vector  int[][]\n);\n\u003c/pre\u003e\u003cp\u003eDefine a unique table constraint for the table \u003ccode class=\"literal\"\u003efilms\u003c/code\u003e. Unique table constraints can be defined on one or more columns of the table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT production UNIQUE(date_prod)\n);\n\u003c/pre\u003e\u003cp\u003eDefine a check column constraint:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer CHECK (did \u0026gt; 100),\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003eDefine a check table constraint:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    CONSTRAINT con1 CHECK (did \u0026gt; 100 AND name \u0026lt;\u0026gt; '')\n);\n\u003c/pre\u003e\u003cp\u003eDefine a primary key table constraint for the table \u003ccode class=\"structname\"\u003efilms\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT code_title PRIMARY KEY(code,title)\n);\n\u003c/pre\u003e\u003cp\u003eDefine a primary key constraint for table \u003ccode class=\"structname\"\u003edistributors\u003c/code\u003e. The following two examples are equivalent, the first using the table constraint syntax, the second the column constraint syntax:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    PRIMARY KEY(did)\n);\n\nCREATE TABLE distributors (\n    did     integer PRIMARY KEY,\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003eAssign a literal constant default value for the column \u003ccode class=\"literal\"\u003ename\u003c/code\u003e, arrange for the default value of column \u003ccode class=\"literal\"\u003edid\u003c/code\u003e to be generated by selecting the next value of a sequence object, and make the default value of \u003ccode class=\"literal\"\u003emodtime\u003c/code\u003e be the time at which the row is inserted:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    name      varchar(40) DEFAULT 'Luso Films',\n    did       integer DEFAULT nextval('distributors_serial'),\n    modtime   timestamp DEFAULT current_timestamp\n);\n\u003c/pre\u003e\u003cp\u003eDefine two \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e column constraints on the table \u003ccode class=\"classname\"\u003edistributors\u003c/code\u003e, one of which is explicitly given a name:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer CONSTRAINT no_null NOT NULL,\n    name    varchar(40) NOT NULL\n);\n\u003c/pre\u003e\u003cp\u003eDefine a unique constraint for the \u003ccode class=\"literal\"\u003ename\u003c/code\u003e column:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40) UNIQUE\n);\n\u003c/pre\u003e\u003cp\u003eThe same, specified as a table constraint:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name)\n);\n\u003c/pre\u003e\u003cp\u003eCreate the same table, specifying 70% fill factor for both the table and its unique index:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name) WITH (fillfactor=70)\n)\nWITH (fillfactor=70);\n\u003c/pre\u003e\u003cp\u003eCreate table \u003ccode class=\"structname\"\u003ecircles\u003c/code\u003e with an exclusion constraint that prevents any two circles from overlapping:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE circles (\n    c circle,\n    EXCLUDE USING gist (c WITH \u0026amp;\u0026amp;)\n);\n\u003c/pre\u003e\u003cp\u003eCreate table \u003ccode class=\"structname\"\u003ecinemas\u003c/code\u003e in tablespace \u003ccode class=\"structname\"\u003ediskvol1\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cinemas (\n        id serial,\n        name text,\n        location text\n) TABLESPACE diskvol1;\n\u003c/pre\u003e\u003cp\u003eCreate a composite type and a typed table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TYPE employee_type AS (name text, salary numeric);\n\nCREATE TABLE employees OF employee_type (\n    PRIMARY KEY (name),\n    salary WITH OPTIONS DEFAULT 1000\n);\n\u003c/pre\u003e\u003cp\u003eCreate a range partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (logdate);\n\u003c/pre\u003e\u003cp\u003eCreate a range partitioned table with multiple columns in the partition key:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_year_month (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (EXTRACT(YEAR FROM logdate), EXTRACT(MONTH FROM logdate));\n\u003c/pre\u003e\u003cp\u003eCreate a list partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities (\n    city_id      bigserial not null,\n    name         text not null,\n    population   bigint\n) PARTITION BY LIST (left(lower(name), 1));\n\u003c/pre\u003e\u003cp\u003eCreate a hash partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE orders (\n    order_id     bigint not null,\n    cust_id      bigint not null,\n    status       text\n) PARTITION BY HASH (order_id);\n\u003c/pre\u003e\u003cp\u003eCreate partition of a range partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_y2016m07\n    PARTITION OF measurement (\n    unitsales DEFAULT 0\n) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');\n\u003c/pre\u003e\u003cp\u003eCreate a few partitions of a range partitioned table with multiple columns in the partition key:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_ym_older\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (MINVALUE, MINVALUE) TO (2016, 11);\n\nCREATE TABLE measurement_ym_y2016m11\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 11) TO (2016, 12);\n\nCREATE TABLE measurement_ym_y2016m12\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 12) TO (2017, 01);\n\nCREATE TABLE measurement_ym_y2017m01\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2017, 01) TO (2017, 02);\n\u003c/pre\u003e\u003cp\u003eCreate partition of a list partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN ('a', 'b');\n\u003c/pre\u003e\u003cp\u003eCreate partition of a list partitioned table that is itself further partitioned and then add a partition to it:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN ('a', 'b') PARTITION BY RANGE (population);\n\nCREATE TABLE cities_ab_10000_to_100000\n    PARTITION OF cities_ab FOR VALUES FROM (10000) TO (100000);\n\u003c/pre\u003e\u003cp\u003eCreate partitions of a hash partitioned table:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE orders_p1 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 0);\nCREATE TABLE orders_p2 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 1);\nCREATE TABLE orders_p3 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 2);\nCREATE TABLE orders_p4 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003eCreate a default partition:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_partdef\n    PARTITION OF cities DEFAULT;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe \u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e command conforms to the \u003cacronym\u003eSQL\u003c/acronym\u003e standard, with exceptions listed below.\u003c/p\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eTemporary Tables\u003c/h3\u003e\u003cp\u003eAlthough the syntax of \u003ccode class=\"literal\"\u003eCREATE TEMPORARY TABLE\u003c/code\u003e resembles that of the SQL standard, the effect is not the same. In the standard, temporary tables are defined just once and automatically exist (starting with empty contents) in every session that needs them. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e instead requires each session to issue its own \u003ccode class=\"literal\"\u003eCREATE TEMPORARY TABLE\u003c/code\u003e command for each temporary table to be used. This allows different sessions to use the same temporary table name for different purposes, whereas the standard's approach constrains all instances of a given temporary table name to have the same table structure.\u003c/p\u003e\u003cp\u003eThe standard's definition of the behavior of temporary tables is widely ignored. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e's behavior on this point is similar to that of several other SQL databases.\u003c/p\u003e\u003cp\u003eThe SQL standard also distinguishes between global and local temporary tables, where a local temporary table has a separate set of contents for each SQL module within each session, though its definition is still shared across sessions. Since \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e does not support SQL modules, this distinction is not relevant in \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.\u003c/p\u003e\u003cp\u003eFor compatibility's sake, \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e will accept the \u003ccode class=\"literal\"\u003eGLOBAL\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e keywords in a temporary table declaration, but they currently have no effect. Use of these keywords is discouraged, since future versions of \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e might adopt a more standard-compliant interpretation of their meaning.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e clause for temporary tables also resembles the SQL standard, but has some differences. If the \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e clause is omitted, SQL specifies that the default behavior is \u003ccode class=\"literal\"\u003eON COMMIT DELETE ROWS\u003c/code\u003e. However, the default behavior in \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e is \u003ccode class=\"literal\"\u003eON COMMIT PRESERVE ROWS\u003c/code\u003e. The \u003ccode class=\"literal\"\u003eON COMMIT DROP\u003c/code\u003e option does not exist in SQL.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eNon-Deferred Uniqueness Constraints\u003c/h3\u003e\u003cp\u003eWhen a \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e or \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e constraint is not deferrable, \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e checks for uniqueness immediately whenever a row is inserted or modified. The SQL standard says that uniqueness should be enforced only at the end of the statement; this makes a difference when, for example, a single command updates multiple key values. To obtain standard-compliant behavior, declare the constraint as \u003ccode class=\"literal\"\u003eDEFERRABLE\u003c/code\u003e but not deferred (i.e., \u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e). Be aware that this can be significantly slower than immediate uniqueness checking.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eColumn Check Constraints\u003c/h3\u003e\u003cp\u003eThe SQL standard says that \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e column constraints can only refer to the column they apply to; only \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e table constraints can refer to multiple columns. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e does not enforce this restriction; it treats column and table check constraints alike.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e Constraint\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraint type is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eForeign Key Constraints\u003c/h3\u003e\u003cp\u003eThe ability to specify column lists in the foreign key actions \u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e and \u003ccode class=\"literal\"\u003eSET NULL\u003c/code\u003e is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e\u003cp\u003eIt is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension that a foreign key constraint may reference columns of a unique index instead of columns of a primary key or unique constraint.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eConstraint\u003c/span\u003e”\u003c/span\u003e\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003econstraint\u003c/span\u003e”\u003c/span\u003e (actually a non-constraint) is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension to the SQL standard that is included for compatibility with some other database systems (and for symmetry with the \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e constraint). Since it is the default for any column, its presence is simply noise.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eConstraint Naming\u003c/h3\u003e\u003cp\u003eThe SQL standard says that table and domain constraints must have names that are unique across the schema containing the table or domain. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e is laxer: it only requires constraint names to be unique across the constraints attached to a particular table or domain. However, this extra freedom does not exist for index-based constraints (\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e, \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e, and \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e constraints), because the associated index is named the same as the constraint, and index names must be unique across all relations within the same schema.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eInheritance\u003c/h3\u003e\u003cp\u003eMultiple inheritance via the \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e clause is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e language extension. SQL:1999 and later define single inheritance using a different syntax and different semantics. SQL:1999-style inheritance is not yet supported by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eZero-Column Tables\u003c/h3\u003e\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e allows a table of no columns to be created (for example, \u003ccode class=\"literal\"\u003eCREATE TABLE foo();\u003c/code\u003e). This is an extension from the SQL standard, which does not allow zero-column tables. Zero-column tables are not in themselves very useful, but disallowing them creates odd special cases for \u003ccode class=\"command\"\u003eALTER TABLE DROP COLUMN\u003c/code\u003e, so it seems cleaner to ignore this spec restriction.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eMultiple Identity Columns\u003c/h3\u003e\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e allows a table to have more than one identity column. The standard specifies that a table can have at most one identity column. This is relaxed mainly to give more flexibility for doing schema changes or migrations. Note that the \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e command supports only one override clause that applies to the entire statement, so having multiple identity columns with different behaviors is not well supported.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eGenerated Columns\u003c/h3\u003e\u003cp\u003eThe options \u003ccode class=\"literal\"\u003eSTORED\u003c/code\u003e and \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e are not standard but are also used by other SQL implementations. The SQL standard does not specify the storage of generated columns.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e Clause\u003c/h3\u003e\u003cp\u003eWhile a \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e clause exists in the SQL standard, many of the options that \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e accepts for it are not in the standard, and some of the standard's options are not implemented by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e Clause\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e clause is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension; storage parameters are not in the standard.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eTablespaces\u003c/h3\u003e\u003cp\u003eThe \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e concept of tablespaces is not part of the standard. Hence, the clauses \u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e and \u003ccode class=\"literal\"\u003eUSING INDEX TABLESPACE\u003c/code\u003e are extensions.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003eTyped Tables\u003c/h3\u003e\u003cp\u003eTyped tables implement a subset of the SQL standard. According to the standard, a typed table has columns corresponding to the underlying composite type as well as one other column that is the \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003eself-referencing column\u003c/span\u003e”\u003c/span\u003e. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e does not support self-referencing columns explicitly.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e Clause\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e clause is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e Clause\u003c/h3\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e clause is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e\u003c/div\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-table/?v=18\" title=\"ALTER TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-table/?v=18\" title=\"DROP TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-tablespace/?v=18\" title=\"CREATE TABLESPACE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e ( [\n  { \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 [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\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    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\n    | LIKE \u003cem class=\"replaceable\"\u003e\u003ccode\u003esource_table\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e ... ] }\n    [, ... ]\n] )\n[ INHERITS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\n    OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e [ (\n  { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ WITH OPTIONS ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e }\n    [, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\n    PARTITION OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e [ (\n  { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ WITH OPTIONS ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e }\n    [, ... ]\n) ] { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\n\u003cspan class=\"phrase\"\u003ewhere \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\u003elike_option\u003c/code\u003e\u003c/em\u003e is:\u003c/span\u003e\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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\"\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":"CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nOF type_name [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ] { FOR VALUES partition_bound_spec | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nwhere 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 like_option is:\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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\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":"create-table","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE TABLE","Summary":"定义一个新表","BodyHTML":"\u003cpre\u003eCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nOF type_name [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ] { FOR VALUES partition_bound_spec | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\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而like_option为：\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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\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\u003eCREATE TABLE\u003c/code\u003e 将在当前数据库中创建一个新的、初始为空的表。该表归发出该命令的用户所有。\u003c/p\u003e\u003cp\u003e如果给出了模式名（例如 \u003ccode\u003eCREATE TABLE myschema.mytable ...\u003c/code\u003e），则表将在指定模式中创建。否则，它将在当前模式中创建。临时表存在于一个特殊模式中，因此创建临时表时不能给出模式名。表名必须与同一模式中任何其他关系（表、序列、索引、视图、物化视图或外部表）的名称不同。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e 还会自动创建一种数据类型，用以表示与该表一行对应的复合类型。因此，表名不能与同一模式中任何已有数据类型同名。\u003c/p\u003e\u003cp\u003e可选的约束子句指定插入或更新要成功时，新行或更新后的行必须满足的约束（测试）。约束是一种 SQL 对象，可用多种方式帮助定义表中允许的值集合。\u003c/p\u003e\u003cp\u003e定义约束有两种方式：表约束和列约束。列约束作为列定义的一部分定义。表约束则不绑定到特定列，并且可以涵盖多个列。每个列约束也都可以写成表约束；当约束只影响一列时，列约束只是一种书写上的方便。\u003c/p\u003e\u003cp\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\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定该选项，表将创建为临时表。临时表会在会话结束时自动删除，或者也可在当前事务结束时删除（见下文 \u003ccode\u003eON COMMIT\u003c/code\u003e）。默认的 search_path 会首先包含临时模式，因此在临时表存在期间，除非使用带模式限定的名称引用，否则不会在新计划中选中同名的现有永久表。在临时表上创建的任何索引也都会自动成为临时索引。\u003c/p\u003e\u003cp\u003e\u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" rel=\"nofollow\"\u003e自动清理守护进程\u003c/a\u003e不能访问并且因此也不能清理或分析临时表。由于这个原因，应该通过会话的 SQL 命令执行合适的清理和分析操作。例如，如果一个临时表将要被用于复杂的查询，最好在把它填充完毕后在其上运行\u003ccode\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e可以在 \u003ccode\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode\u003eTEMP\u003c/code\u003e 前写 \u003ccode\u003eGLOBAL\u003c/code\u003e 或 \u003ccode\u003eLOCAL\u003c/code\u003e。这在当前的 \u003cspan\u003ePostgreSQL\u003c/span\u003e 中没有区别，而且已弃用；见下文\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-COMPATIBILITY\" title=\"兼容性\" rel=\"nofollow\"\u003eCompatibility\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定该选项，表将创建为不记录 WAL 的表。写入不记录 WAL 的表的数据不会写入预写式日志（见\u003ca href=\"/docs/18/wal.html\" rel=\"nofollow\"\u003e第 28 章\u003c/a\u003e），因此它们比普通表快得多。不过，它们不具备崩溃安全性：在崩溃或非正常关闭后，不记录 WAL 的表会被自动截断。不记录 WAL 的表的内容也不会复制到备库。在不记录 WAL 的表上创建的任何索引也都会自动成为不记录 WAL 的。\u003c/p\u003e\u003cp\u003e如果指定该选项，与不记录 WAL 的表一起创建的任何序列（用于标识列或 serial 列）也会创建为不记录 WAL 的。\u003c/p\u003e\u003cp\u003e这种形式不支持分区表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eIF NOT 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\u003etable_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\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创建一个\u003cem\u003e类型化表\u003c/em\u003e，其结构取自指定的独立复合类型（即通过\u003ca href=\"/docs/18/sql-createtype.html\" title=\"CREATE TYPE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e创建的类型），同时该表自身也会生成一个新的复合类型。该表会依赖于所引用的类型，这意味着对该类型执行级联的 ALTER 或 DROP 操作会传播到该表。\u003c/p\u003e\u003cp\u003e类型化表的列名和数据类型始终与其所基于的类型相同，因此不能再指定额外的列。但 \u003ccode\u003eCREATE TABLE\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\u003edata_type\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列的数据类型，可以包含数组说明符。有关\u003cspan\u003ePostgreSQL\u003c/span\u003e支持的数据类型的更多信息，参见\u003ca href=\"/docs/18/datatype.html\" rel=\"nofollow\"\u003e第 8 章\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCOLLATE \u003cem\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eCOLLATE\u003c/code\u003e子句为列指定排序规则（该列必须属于支持排序规则的数据类型）。如果未指定，则使用列数据类型的默认排序规则。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSTORAGE { 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 值上的子串操作速度，但代价是占用更多存储空间。详见\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\u003eCOMPRESSION \u003cem\u003e\u003ccode\u003ecompression_method\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eCOMPRESSION\u003c/code\u003e 子句设置列的压缩方法。压缩仅支持变宽数据类型，并且只会在列的存储模式为 \u003ccode\u003emain\u003c/code\u003e 或 \u003ccode\u003eextended\u003c/code\u003e 时使用。（有关列存储模式的信息，见\u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e。）为分区表设置该属性没有直接影响，因为此类表自身不存储数据，但新创建的分区会继承该配置值。支持的压缩方法是 \u003ccode\u003epglz\u003c/code\u003e 和 \u003ccode\u003elz4\u003c/code\u003e。（只有在构建 \u003cspan\u003ePostgreSQL\u003c/span\u003e 时使用了 \u003ccode\u003e--with-lz4\u003c/code\u003e，\u003ccode\u003elz4\u003c/code\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\u003eINHERITS ( \u003cem\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e [, ... ] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可选的 \u003ccode\u003eINHERITS\u003c/code\u003e 子句指定一组表，新表将自动从中继承所有列。父表可以是普通表或外部表。\u003c/p\u003e\u003cp\u003e使用 \u003ccode\u003eINHERITS\u003c/code\u003e 会在新子表与其父表之间建立持久关系。对父表的模式修改通常也会传播到子表，且默认情况下，对父表的扫描会包含子表的数据。\u003c/p\u003e\u003cp\u003e如果同一列名出现在多个父表中，除非这些父表中该列的数据类型全部匹配，否则会报错。如果没有冲突，这些重复列会合并为新表中的单个列。如果新表的列名列表中包含一个同样来自继承的列名，其数据类型也必须与继承列匹配，并且列定义会合并为一个。如果新表显式为该列指定了默认值，该默认值会覆盖继承声明中的任何默认值。否则，任何为该列指定默认值的父表都必须指定相同的默认值，否则会报错。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eCHECK\u003c/code\u003e 约束基本上也按与列相同的方式合并：如果多个父表和/或新表定义中包含同名的 \u003ccode\u003eCHECK\u003c/code\u003e 约束，则这些约束必须拥有相同的检查表达式，否则会报错。同名且表达式相同的约束将合并为一份。父表中标记为 \u003ccode\u003eNO INHERIT\u003c/code\u003e 的约束不会被考虑。注意，新表中未命名的 \u003ccode\u003eCHECK\u003c/code\u003e 约束永远不会被合并，因为系统总会为它选择一个唯一名称。\u003c/p\u003e\u003cp\u003e列的 \u003ccode\u003eSTORAGE\u003c/code\u003e 设置也会从父表复制过来。\u003c/p\u003e\u003cp\u003e如果父表中的列是标识列，则该属性不会被继承。若需要，可将子表中的列声明为标识列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePARTITION BY { RANGE | LIST | HASH } ( { \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e | ( \u003cem\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) } [ \u003cem\u003e\u003ccode\u003eopclass\u003c/code\u003e\u003c/em\u003e ] [, ...] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可选的 \u003ccode\u003ePARTITION BY\u003c/code\u003e 子句指定表的分区策略。这样创建的表称为\u003cem\u003e分区表\u003c/em\u003e。括号中的列或表达式列表构成该表的\u003cem\u003e分区键\u003c/em\u003e。使用范围分区或 hash 分区时，分区键可以包含多个列或表达式（最多 32 个，但该限制可在构建 \u003cspan\u003ePostgreSQL\u003c/span\u003e 时调整）；而列表分区的分区键必须由单个列或表达式组成。\u003c/p\u003e\u003cp\u003e范围分区和列表分区需要 B-树操作符类，hash 分区则需要 hash 操作符类。如果未显式指定操作符类，将使用相应类型的默认操作符类；若不存在默认操作符类，则会报错。使用 hash 分区时，所用操作符类必须实现支持函数 2（详见\u003ca href=\"/docs/18/xindex.html#XINDEX-SUPPORT\" rel=\"nofollow\"\u003e第 36.16.3 节\u003c/a\u003e）。\u003c/p\u003e\u003cp\u003e分区表被划分为多个子表（称为分区），它们使用单独的 \u003ccode\u003eCREATE TABLE\u003c/code\u003e 命令创建。分区表本身为空。插入到该表的数据行会根据分区键中列或表达式的值被路由到相应分区。如果没有现有分区与新行中的值匹配，就会报错。\u003c/p\u003e\u003cp\u003e有关表分区的更多讨论，请参阅\u003ca href=\"/docs/18/ddl-partitioning.html\" rel=\"nofollow\"\u003e第 5.12 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePARTITION OF \u003cem\u003e\u003ccode\u003eparent_table\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将该表创建为指定父表的\u003cem\u003e分区\u003c/em\u003e。可以使用 \u003ccode\u003eFOR VALUES\u003c/code\u003e 将其创建为特定值的分区，也可以使用 \u003ccode\u003eDEFAULT\u003c/code\u003e 创建为默认分区。父表上已有的任何索引、约束和用户定义的行级触发器都会克隆到新分区上。\u003c/p\u003e\u003cp\u003e\u003cem\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e 必须对应于父表的分区方法和分区键，并且不能与该父表的任何现有分区重叠。带 \u003ccode\u003eIN\u003c/code\u003e 的形式用于列表分区，带 \u003ccode\u003eFROM\u003c/code\u003e 和 \u003ccode\u003eTO\u003c/code\u003e 的形式用于范围分区，带 \u003ccode\u003eWITH\u003c/code\u003e 的形式用于 hash 分区。\u003c/p\u003e\u003cp\u003e\u003cem\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e 是任何无变量表达式（不允许子查询、窗口函数、聚合函数和集合返回函数）。它的数据类型必须与相应分区键列的数据类型相匹配。表达式在表创建时只计算一次，因此它甚至可以包含易失性表达式，例如 \u003ccode\u003e\u003ccode\u003eCURRENT_TIMESTAMP\u003c/code\u003e\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e在创建列表分区时，可以指定 \u003ccode\u003eNULL\u003c/code\u003e，表示该分区允许分区键列为 NULL。但是，对于给定的父表，这样的列表分区不能多于一个。\u003ccode\u003eNULL\u003c/code\u003e 不能用于范围分区。\u003c/p\u003e\u003cp\u003e创建范围分区时，由 \u003ccode\u003eFROM\u003c/code\u003e 指定的下界是包含边界，而由 \u003ccode\u003eTO\u003c/code\u003e 指定的上界是不包含边界。也就是说，\u003ccode\u003eFROM\u003c/code\u003e 列表中指定的值是该分区相应分区键列的有效值，而 \u003ccode\u003eTO\u003c/code\u003e 列表中的值不是。请注意，必须根据按行比较的规则来理解这一点（\u003ca href=\"/docs/18/functions-comparisons.html#ROW-WISE-COMPARISON\" rel=\"nofollow\"\u003e第 9.25.5 节\u003c/a\u003e）。例如，给定 \u003ccode\u003ePARTITION BY RANGE (x,y)\u003c/code\u003e，分区边界 \u003ccode\u003eFROM (1, 2) TO (3, 4)\u003c/code\u003e 允许 \u003ccode\u003ex=1\u003c/code\u003e 且任意 \u003ccode\u003ey\u0026gt;=2\u003c/code\u003e，\u003ccode\u003ex=2\u003c/code\u003e 且任意非 NULL 的 \u003ccode\u003ey\u003c/code\u003e，以及 \u003ccode\u003ex=3\u003c/code\u003e 且任意 \u003ccode\u003ey\u0026lt;4\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e在创建范围分区时，可以使用特殊值 \u003ccode\u003eMINVALUE\u003c/code\u003e 和 \u003ccode\u003eMAXVALUE\u003c/code\u003e 表示该列值没有下界或上界。例如，使用 \u003ccode\u003eFROM (MINVALUE) TO (10)\u003c/code\u003e 定义的分区允许任何小于 10 的值，而使用 \u003ccode\u003eFROM (10) TO (MAXVALUE)\u003c/code\u003e 定义的分区允许任何大于或等于 10 的值。\u003c/p\u003e\u003cp\u003e在创建涉及多列的范围分区时，将 \u003ccode\u003eMAXVALUE\u003c/code\u003e 用作下界的一部分、将 \u003ccode\u003eMINVALUE\u003c/code\u003e 用作上界的一部分也可能是有意义的。例如，使用 \u003ccode\u003eFROM (0, MAXVALUE) TO (10, MAXVALUE)\u003c/code\u003e 定义的分区允许第一个分区键列大于 0 且小于或等于 10 的所有行。类似地，使用 \u003ccode\u003eFROM (\u0026#39;a\u0026#39;, MINVALUE) TO (\u0026#39;b\u0026#39;, MINVALUE)\u003c/code\u003e 定义的分区允许第一个分区键列以“a”开头的所有行。\u003c/p\u003e\u003cp\u003e请注意，如果 \u003ccode\u003eMINVALUE\u003c/code\u003e 或 \u003ccode\u003eMAXVALUE\u003c/code\u003e 用于分区边界中的某一列，则后续所有列都必须使用相同的值。例如，\u003ccode\u003e(10, MINVALUE, 0)\u003c/code\u003e 不是有效边界；应写成 \u003ccode\u003e(10, MINVALUE, MINVALUE)\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e还要注意，某些元素类型（如 \u003ccode\u003etimestamp\u003c/code\u003e）具有“无穷大”的概念，那只是另一种可存储的值。这不同于 \u003ccode\u003eMINVALUE\u003c/code\u003e 和 \u003ccode\u003eMAXVALUE\u003c/code\u003e，后两者并非可存储的实际值，而只是表示值无界的方式。\u003ccode\u003eMAXVALUE\u003c/code\u003e 可以视为大于任何其他值，包括“无穷大”；\u003ccode\u003eMINVALUE\u003c/code\u003e 可以视为小于任何其他值，包括“负无穷大”。因此，范围 \u003ccode\u003eFROM (\u0026#39;infinity\u0026#39;) TO (MAXVALUE)\u003c/code\u003e 并不是空范围；它只允许存储一个值 — “无穷大”。\u003c/p\u003e\u003cp\u003e如果指定了 \u003ccode\u003eDEFAULT\u003c/code\u003e，则该表将创建为父表的默认分区。此选项不适用于 hash 分区表。任何不适合该父表其他分区的分区键值都会被路由到默认分区。\u003c/p\u003e\u003cp\u003e当一个表已有 \u003ccode\u003eDEFAULT\u003c/code\u003e 分区并且要向其添加新分区时，必须扫描默认分区，以验证其中不包含应属于新分区的任何行。如果默认分区包含大量行，这可能会很慢。如果默认分区是外部表，或者它具有可证明不可能包含应放入新分区之行的约束，则会跳过扫描。\u003c/p\u003e\u003cp\u003e创建 hash 分区时，必须指定模数和余数。模数必须是正整数，余数必须是小于模数的非负整数。通常，在最初设置 hash 分区表时，应选择一个等于分区数的模数，并为每个分区指定相同的模数和不同的余数（见下文示例）。不过，并不要求每个分区都有相同的模数；只要求 hash 分区表中出现的每个模数都是下一个更大模数的因数。这样就可以按增量方式增加分区数量，而不必一次移动全部数据。例如，假设你有一个包含 8 个分区的 hash 分区表，每个分区的模数都是 8，但发现有必要将分区数增加到 16。你可以分离其中一个模数为 8 的分区，再创建两个新的模数为 16 的分区来覆盖同一部分键空间（其中一个的余数等于被分离分区的余数，另一个的余数等于该值加 8），然后重新向它们填充数据。之后可以对每个模数为 8 的分区重复这一过程，也许是在之后某个时间点，直到一个不剩。虽然每一步仍可能涉及大量数据移动，但仍优于创建一张全新的表并一次性移动所有数据。\u003c/p\u003e\u003cp\u003e分区必须具有与其所属分区表相同的列名和类型。对分区表列名或类型的修改会自动传播到所有分区。\u003ccode\u003eCHECK\u003c/code\u003e 约束会自动被每个分区继承，但单个分区也可以指定额外的 \u003ccode\u003eCHECK\u003c/code\u003e 约束；与父表中名称和条件相同的额外约束将与父表约束合并。可以为每个分区分别指定默认值。但是请注意，在通过分区表插入元组时不会应用分区的默认值。\u003c/p\u003e\u003cp\u003e插入分区表中的行将自动路由到正确的分区。如果不存在合适的分区，则会发生错误。\u003c/p\u003e\u003cp\u003e像 \u003ccode\u003eTRUNCATE\u003c/code\u003e 这样通常会影响表及其所有继承子表的操作，会级联到所有分区，但也可以在单个分区上执行。\u003c/p\u003e\u003cp\u003e请注意，使用 \u003ccode\u003ePARTITION OF\u003c/code\u003e 创建分区需要对父分区表获取 \u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e 锁。类似地，使用 \u003ccode\u003eDROP TABLE\u003c/code\u003e 删除分区也需要对父表获取 \u003ccode\u003eACCESS EXCLUSIVE\u003c/code\u003e 锁。可以使用 \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eALTER TABLE ATTACH/DETACH PARTITION\u003c/code\u003e\u003c/a\u003e 以更弱的锁执行这些操作，从而减少对分区表并发操作的干扰。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eLIKE \u003cem\u003e\u003ccode\u003esource_table\u003c/code\u003e\u003c/em\u003e [ \u003cem\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e ... ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eLIKE\u003c/code\u003e 子句指定一个表，新表会自动从中复制所有列名、数据类型及其非空约束。\u003c/p\u003e\u003cp\u003e与 \u003ccode\u003eINHERITS\u003c/code\u003e 不同，新表和原表在创建完成后就完全脱钩了。对原表的修改不会应用到新表，也不可能在扫描原表时包含新表的数据。\u003c/p\u003e\u003cp\u003e同样与 \u003ccode\u003eINHERITS\u003c/code\u003e 不同，由 \u003ccode\u003eLIKE\u003c/code\u003e 复制的列和约束不会与同名的列和约束合并。如果同一名称被显式指定，或在另一个 \u003ccode\u003eLIKE\u003c/code\u003e 子句中指定，则会报错。\u003c/p\u003e\u003cp\u003e可选的 \u003cem\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e 子句指定还要复制原表的哪些附加属性。指定 \u003ccode\u003eINCLUDING\u003c/code\u003e 表示复制该属性，指定 \u003ccode\u003eEXCLUDING\u003c/code\u003e 表示省略该属性。默认是 \u003ccode\u003eEXCLUDING\u003c/code\u003e。如果对同一类对象给出了多个说明，则采用最后一个。可用选项如下：\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING COMMENTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e复制的列、检查约束、非空约束、索引和扩展统计信息的注释也会被复制。默认行为是不复制注释，因此新表中对应的对象没有注释。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING COMPRESSION\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e列的压缩方法也会被复制。默认行为是不复制压缩方法，因此列会使用默认压缩方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING CONSTRAINTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制 \u003ccode\u003eCHECK\u003c/code\u003e 约束。列约束与表约束不作区分。非空约束始终会复制到新表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING DEFAULTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义的默认表达式。否则默认表达式不会复制，因此新表中复制出的列默认值为 null。注意，复制那些调用数据库修改函数（如 \u003ccode\u003enextval\u003c/code\u003e）的默认值，可能在原表与新表之间建立功能性关联。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING GENERATED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义中的生成表达式以及 STORED/VIRTUAL 的选择。默认情况下，新列将是普通基础列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING IDENTITY\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义中的任何标识规范。新表的每个标识列都会创建一个新序列，它与旧表关联的序列相互独立。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING INDEXES\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e原表上的索引、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e、\u003ccode\u003eUNIQUE\u003c/code\u003e 和 \u003ccode\u003eEXCLUDE\u003c/code\u003e 约束都会在新表上创建。新索引和约束的名称会按默认规则选择，而不考虑原始名称。（这种行为可避免新索引因名称重复而失败。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING STATISTICS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e扩展统计信息将复制到新表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINCLUDING STORAGE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义的 \u003ccode\u003eSTORAGE\u003c/code\u003e 设置。默认行为是不复制 \u003ccode\u003eSTORAGE\u003c/code\u003e 设置，因此新表中复制出的列将使用类型特定的默认设置。关于 \u003ccode\u003eSTORAGE\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\u003eINCLUDING ALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eINCLUDING ALL\u003c/code\u003e 是选择所有可用单项选项的缩写形式。（可以在 \u003ccode\u003eINCLUDING ALL\u003c/code\u003e 之后再写单独的 \u003ccode\u003eEXCLUDING\u003c/code\u003e 子句，以选中除某些特定选项之外的全部选项。）\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\u003ccode\u003eLIKE\u003c/code\u003e 子句也可用于从视图、外部表或复合类型复制列定义。不适用的选项（例如从视图复制 \u003ccode\u003eINCLUDING INDEXES\u003c/code\u003e）会被忽略。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCONSTRAINT \u003cem\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e列约束或表约束的可选名称。如果约束被违反，错误消息中会包含该约束名，因此诸如\u003ccode\u003ecol must be positive\u003c/code\u003e这样的约束名可以向客户端应用传达有用的约束信息。（若约束名中包含空格，则需要用双引号指定。）如果未指定约束名，系统会生成一个。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNOT NULL [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列不允许包含空值。\u003c/p\u003e\u003cp\u003e标记为\u003ccode\u003eNO INHERIT\u003c/code\u003e的约束不会传播到子表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列允许包含空值。这是默认情况。\u003c/p\u003e\u003cp\u003e该子句仅为兼容非标准 SQL 数据库而提供，不建议在新应用中使用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCHECK ( \u003cem\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eCHECK\u003c/code\u003e 子句指定一个产生布尔结果的表达式。要使插入或更新成功，新行或更新后的行必须满足该表达式。计算结果为 TRUE 或 UNKNOWN 的表达式视为成功。如果插入或更新操作中的任何一行得到 FALSE 结果，就会抛出错误异常，并且插入或更新不会修改数据库。作为列约束指定的检查约束只应引用该列的值，而出现在表约束中的表达式可以引用多个列。\u003c/p\u003e\u003cp\u003e当前，\u003ccode\u003eCHECK\u003c/code\u003e 表达式不能包含子查询，也不能引用当前行的列之外的变量（参见\u003ca href=\"/docs/18/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS\" rel=\"nofollow\"\u003e第 5.5.1 节\u003c/a\u003e）。可以引用系统列 \u003ccode\u003etableoid\u003c/code\u003e，但不能引用其他系统列。\u003c/p\u003e\u003cp\u003e标记为 \u003ccode\u003eNO INHERIT\u003c/code\u003e 的约束不会传播到子表。\u003c/p\u003e\u003cp\u003e当一个表有多个 \u003ccode\u003eCHECK\u003c/code\u003e 约束时，在检查完 \u003ccode\u003eNOT NULL\u003c/code\u003e 约束之后，会按名称的字母顺序对每一行进行检查。（9.5 之前的 \u003cspan\u003ePostgreSQL\u003c/span\u003e 版本并不保证 \u003ccode\u003eCHECK\u003c/code\u003e 约束的特定触发顺序。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDEFAULT \u003cem\u003e\u003ccode\u003edefault_expr\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eDEFAULT\u003c/code\u003e 子句为其所在列指定默认数据值。该值是一个不含变量的表达式（特别是，不允许引用当前表中的其他列）。子查询也不允许。默认值表达式的数据类型必须匹配列的数据类型。\u003c/p\u003e\u003cp\u003e默认值表达式会用于任何未为该列指定值的插入操作。如果一列没有默认值，则默认值为 null。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eGENERATED ALWAYS AS ( \u003cem\u003e\u003ccode\u003egeneration_expr\u003c/code\u003e\u003c/em\u003e ) [ STORED | VIRTUAL ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e此子句将列创建为\u003cem\u003e生成列\u003c/em\u003e。列不可写入，读取时会返回指定表达式的结果。\u003c/p\u003e\u003cp\u003e指定 \u003ccode\u003eVIRTUAL\u003c/code\u003e 时，列会在读取时计算且不占用存储；指定 \u003ccode\u003eSTORED\u003c/code\u003e 时，列会在写入时计算并存储在磁盘上。默认是 \u003ccode\u003eVIRTUAL\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e生成表达式可以引用表中的其他列，但不能引用其他生成列。使用的任何函数和操作符都必须是不可变的。不允许引用其他表。\u003c/p\u003e\u003cp\u003e虚拟生成列不能使用用户定义类型，且其生成表达式不得引用用户定义函数或类型，也就是只能使用内置函数或类型。此限制也包含间接使用（例如操作符或类型转换背后的函数或类型）。该限制不适用于存储型生成列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句将列创建为\u003cem\u003e标识列\u003c/em\u003e。它会隐式附带一个序列，并且在新插入的行中，该列会自动取得分配给它的序列值。这种列会隐式带有 \u003ccode\u003eNOT NULL\u003c/code\u003e 约束。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eALWAYS\u003c/code\u003e 和 \u003ccode\u003eBY DEFAULT\u003c/code\u003e 子句决定在 \u003ccode\u003eINSERT\u003c/code\u003e 和 \u003ccode\u003eUPDATE\u003c/code\u003e 命令中如何处理用户显式指定的值。\u003c/p\u003e\u003cp\u003e在 \u003ccode\u003eINSERT\u003c/code\u003e 命令中，如果选择了 \u003ccode\u003eALWAYS\u003c/code\u003e，则仅当 \u003ccode\u003eINSERT\u003c/code\u003e 语句指定 \u003ccode\u003eOVERRIDING SYSTEM VALUE\u003c/code\u003e 时才接受用户指定的值。如果选择了 \u003ccode\u003eBY DEFAULT\u003c/code\u003e，则用户指定的值优先。有关详细信息，请参阅\u003ca href=\"/docs/18/sql-insert.html\" title=\"INSERT\" rel=\"nofollow\"\u003e\u003cspan\u003eINSERT\u003c/span\u003e\u003c/a\u003e。（在 \u003ccode\u003eCOPY\u003c/code\u003e 命令中，无论此设置如何，始终使用用户指定的值。）\u003c/p\u003e\u003cp\u003e在 \u003ccode\u003eUPDATE\u003c/code\u003e 命令中，如果选择了 \u003ccode\u003eALWAYS\u003c/code\u003e，则把该列更新为除 \u003ccode\u003eDEFAULT\u003c/code\u003e 之外的任何值都会被拒绝。如果选择了 \u003ccode\u003eBY DEFAULT\u003c/code\u003e，则该列可以正常更新。（\u003ccode\u003eUPDATE\u003c/code\u003e 命令没有 \u003ccode\u003eOVERRIDING\u003c/code\u003e 子句。）\u003c/p\u003e\u003cp\u003e可选的 \u003cem\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e 子句可用于覆盖序列参数。可用选项包括\u003ca href=\"/docs/18/sql-createsequence.html\" title=\"CREATE SEQUENCE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE SEQUENCE\u003c/span\u003e\u003c/a\u003e中展示的选项，另外还包括 \u003ccode\u003eSEQUENCE NAME \u003cem\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e、\u003ccode\u003eLOGGED\u003c/code\u003e 与 \u003ccode\u003eUNLOGGED\u003c/code\u003e，用于选择序列名称及持久化级别。不指定 \u003ccode\u003eSEQUENCE NAME\u003c/code\u003e 时，系统会为该序列选择一个未使用名称。不指定 \u003ccode\u003eLOGGED\u003c/code\u003e 或 \u003ccode\u003eUNLOGGED\u003c/code\u003e 时，序列将与表具有相同的持久化级别。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUNIQUE [ NULLS [ NOT ] DISTINCT ]\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e \u003cspan\u003e\u003ccode\u003eUNIQUE [ NULLS [ NOT ] DISTINCT ] ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] )\u003c/code\u003e [ \u003cspan\u003e\u003ccode\u003eINCLUDE ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ]（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eUNIQUE\u003c/code\u003e 约束指定表中一列或多列组成的一组只能包含唯一值。表级唯一约束的行为与列级唯一约束相同，只是它还能跨越多列。因此，该约束要求任意两行在这些列中至少有一列不同。\u003c/p\u003e\u003cp\u003e如果为最后一列指定了 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 选项，则该列检查的是是否重叠，而不是是否相等。在这种情况下，只要重复值在 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 列上不发生重叠，约束中的其他列就允许重复。（如果该列是日期或时间戳范围，这有时被称为时态键；但 \u003cspan\u003ePostgreSQL\u003c/span\u003e 允许在任意基础类型上定义范围。）实际上，这种约束是通过 \u003ccode\u003eEXCLUDE\u003c/code\u003e 约束而不是 \u003ccode\u003eUNIQUE\u003c/code\u003e 约束来强制的。例如，\u003ccode\u003eUNIQUE (id, valid_at WITHOUT OVERLAPS)\u003c/code\u003e 的行为类似于 \u003ccode\u003eEXCLUDE USING GIST (id WITH =, valid_at WITH \u0026amp;\u0026amp;)\u003c/code\u003e。\u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 列必须是范围类型或多范围类型，且不允许为空范围或空多范围。该约束中不带 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 的列可以是任何可在 GiST 索引中按相等性比较的类型。默认仅支持范围类型，但可通过安装\u003ca href=\"/docs/18/btree-gist.html\" rel=\"nofollow\"\u003ebtree_gist\u003c/a\u003e扩展来使用其他类型（这也是该特性的预期用法）。\u003c/p\u003e\u003cp\u003e就唯一约束而言，空值不被视为相等，除非指定了 \u003ccode\u003eNULLS NOT DISTINCT\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e每个唯一约束都应引用一组列，这组列应不同于该表上任何其他唯一约束或主键约束所引用的列集合。（否则，冗余的唯一约束将被丢弃。）\u003c/p\u003e\u003cp\u003e在为多级分区层次结构建立唯一约束时，目标分区表分区键中的所有列，以及其所有后代分区表分区键中的所有列，都必须包含在约束定义中。\u003c/p\u003e\u003cp\u003e添加唯一约束会自动在约束所用的列或列组上创建唯一 B-树索引；但如果该约束包含 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 子句，则会使用 GiST 索引。所创建的索引与该唯一约束同名。\u003c/p\u003e\u003cp\u003e可选的 \u003ccode\u003eINCLUDE\u003c/code\u003e 子句会向该索引添加一个或多个仅作为\u003cspan\u003e“\u003cspan\u003e载荷\u003c/span\u003e”\u003c/span\u003e的列：对这些列不强制唯一性，而且该索引也不能据此进行搜索。不过，它们可以通过仅索引扫描取回。注意，尽管约束不在内含列上强制执行，它仍然依赖于这些列。因此，对这类列执行某些操作（例如 \u003ccode\u003eDROP COLUMN\u003c/code\u003e）可能导致约束和索引被级联删除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePRIMARY KEY\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003ePRIMARY KEY ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e WITHOUT OVERLAPS ] )\u003c/code\u003e [ \u003cspan\u003e\u003ccode\u003eINCLUDE ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ]（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 约束指定表的一列或多列只能包含唯一（不重复）且非空的值。无论作为列约束还是表约束，一个表都只能指定一个主键。\u003c/p\u003e\u003cp\u003e主键约束所引用的列集合应不同于同一表上定义的任何唯一约束所引用的列集合。（否则，该唯一约束是冗余的，会被丢弃。）\u003c/p\u003e\u003cp\u003e\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 强制的数据约束与 \u003ccode\u003eUNIQUE\u003c/code\u003e 和 \u003ccode\u003eNOT NULL\u003c/code\u003e 的组合相同。不过，将一组列标识为主键还会为模式设计提供元数据，因为主键意味着其他表可以将这组列作为行的唯一标识符来依赖。\u003c/p\u003e\u003cp\u003e放在分区表上时，\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 约束与前面描述的 \u003ccode\u003eUNIQUE\u003c/code\u003e 约束有相同的限制。\u003c/p\u003e\u003cp\u003e添加 \u003ccode\u003ePRIMARY KEY\u003c/code\u003e 约束会自动在约束所用的列或列组上创建唯一 B-树索引；如果指定了 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e，则会创建 GiST 索引。\u003c/p\u003e\u003cp\u003e可选的 \u003ccode\u003eINCLUDE\u003c/code\u003e 子句会向该索引添加一个或多个仅作为\u003cspan\u003e“\u003cspan\u003e载荷\u003c/span\u003e”\u003c/span\u003e的列：对这些列不强制唯一性，而且该索引也不能据此进行搜索。不过，它们可以通过仅索引扫描取回。注意，尽管约束不在内含列上强制执行，它仍然依赖于这些列。因此，对这类列执行某些操作（例如 \u003ccode\u003eDROP COLUMN\u003c/code\u003e）可能导致约束和索引被级联删除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eEXCLUDE [ USING \u003cem\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e ] ( \u003cem\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e WITH \u003cem\u003e\u003ccode\u003eoperator\u003c/code\u003e\u003c/em\u003e [, ... ] ) \u003cem\u003e\u003ccode\u003eindex_parameters\u003c/code\u003e\u003c/em\u003e [ WHERE ( \u003cem\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode\u003eEXCLUDE\u003c/code\u003e 子句定义一个排他约束。它保证如果任意两行在指定列或表达式上使用指定操作符进行比较，这些比较不会全部返回 \u003ccode\u003eTRUE\u003c/code\u003e。如果所有指定操作符都测试相等，这就等价于 \u003ccode\u003eUNIQUE\u003c/code\u003e 约束，尽管普通唯一约束会更快。不过，排他约束可以指定比简单相等更一般的约束。例如，你可以通过使用 \u003ccode\u003e\u0026amp;\u0026amp;\u003c/code\u003e 操作符来指定一个约束，使表中不存在两个包含重叠圆的行（见\u003ca href=\"/docs/18/datatype-geometric.html\" rel=\"nofollow\"\u003e第 8.8 节\u003c/a\u003e）。这些操作符必须是可交换的。\u003c/p\u003e\u003cp\u003e排他约束通过一个与约束同名的索引来实现，因此每个指定操作符都必须与索引访问方法 \u003cem\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e 的适当操作符类关联（见\u003ca href=\"/docs/18/indexes-opclass.html\" rel=\"nofollow\"\u003e第 11.10 节\u003c/a\u003e）。每个 \u003cem\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e 都定义了索引的一列，因此它可以可选地指定排序规则、操作符类、操作符类参数和/或排序选项；这些在\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e中有完整说明。\u003c/p\u003e\u003cp\u003e访问方法必须支持 \u003ccode\u003eamgettuple\u003c/code\u003e（见\u003ca href=\"/docs/18/indexam.html\" rel=\"nofollow\"\u003e第 63 章\u003c/a\u003e）；目前这意味着不能使用 \u003cacronym\u003eGIN\u003c/acronym\u003e。虽然允许，但在排他约束上使用 B-树或 hash 索引意义不大，因为它们做不到比普通唯一约束更好的事情。因此，实践中访问方法总是 \u003cacronym\u003eGiST\u003c/acronym\u003e 或 \u003cacronym\u003eSP-GiST\u003c/acronym\u003e。\u003c/p\u003e\u003cp\u003e\u003cem\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e 允许你只在表的一个子集上指定排他约束；在内部，这会创建一个部分索引。注意，谓词周围的圆括号是必需的。\u003c/p\u003e\u003cp\u003e在为多级分区层次结构建立排他约束时，目标分区表分区键中的所有列，以及其所有后代分区表分区键中的所有列，都必须包含在约束定义中。此外，这些列必须使用等值操作符进行比较。这些限制保证潜在冲突的行会落在同一分区中。约束也可以引用不属于任何分区键的其他列，这些列可以使用任何合适的操作符来比较。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eREFERENCES \u003cem\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ) ] [ MATCH \u003cem\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ ON DELETE \u003cem\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eFOREIGN KEY ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] [, PERIOD \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e ] ) REFERENCES \u003cem\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e [ ( \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e [, ... ] [, PERIOD \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e ] ) ] [ MATCH \u003cem\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ ON DELETE \u003cem\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ] [ ON UPDATE \u003cem\u003e\u003ccode\u003ereferential_action\u003c/code\u003e\u003c/em\u003e ]\u003c/code\u003e（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些子句指定外键约束，要求新表的一列或多列组成的一组只能包含与被引用表某一行的被引用列中值相匹配的值。若省略 \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 列表，则使用 \u003cem\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e 的主键。否则，\u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 列表必须引用某个不可延迟的唯一约束或主键约束的列，或者引用某个非部分唯一索引的列。\u003c/p\u003e\u003cp\u003e如果最后一列标记为 \u003ccode\u003ePERIOD\u003c/code\u003e，则会按特殊方式处理。非 \u003ccode\u003ePERIOD\u003c/code\u003e 列按相等性比较（并且至少需要一列），而 \u003ccode\u003ePERIOD\u003c/code\u003e 列则不会。取而代之的是，只有当被引用表中基于键的非 \u003ccode\u003ePERIOD\u003c/code\u003e 部分相匹配的记录，其合并后的 \u003ccode\u003ePERIOD\u003c/code\u003e 值能够完整覆盖引用记录的 PERIOD 值时，约束才算满足。换言之，该引用在其整个持续区间内都必须有对应的被引用对象。该列必须是范围类型或多范围类型。此外，被引用表必须具有用 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e 声明的主键或唯一约束。最后，如果外键包含 PERIOD \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e 说明，则对应的 \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e（如果存在）也必须标记为 \u003ccode\u003ePERIOD\u003c/code\u003e。如果省略 \u003cem\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 子句，从而选择 reftable 的主键约束，则该主键的最后一列必须标记为 \u003ccode\u003eWITHOUT OVERLAPS\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e对于每一对引用列和被引用列，如果它们属于支持排序规则的数据类型，则两者的排序规则要么都必须是确定性的，要么必须完全相同。这样可以确保两列对“相等”具有一致的定义。\u003c/p\u003e\u003cp\u003e用户必须对被引用表拥有 \u003ccode\u003eREFERENCES\u003c/code\u003e 权限（对整张表，或对特定的被引用列）。添加外键约束需要在被引用表上获取 \u003ccode\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e 锁。注意，不能在临时表和永久表之间定义外键约束。\u003c/p\u003e\u003cp\u003e插入到引用列中的值会按照给定的匹配类型，与被引用表及其被引用列中的值进行匹配。共有三种匹配类型：\u003ccode\u003eMATCH FULL\u003c/code\u003e、\u003ccode\u003eMATCH PARTIAL\u003c/code\u003e 和 \u003ccode\u003eMATCH SIMPLE\u003c/code\u003e（默认值）。\u003ccode\u003eMATCH FULL\u003c/code\u003e 不允许多列外键中的某一列为空，除非所有外键列都为空；如果它们都为空，则不要求该行在被引用表中有匹配行。\u003ccode\u003eMATCH SIMPLE\u003c/code\u003e 允许任意外键列为空；如果其中任何一列为空，则不要求该行在被引用表中有匹配行。\u003ccode\u003eMATCH PARTIAL\u003c/code\u003e 目前尚未实现。（当然，可以对引用列应用 \u003ccode\u003eNOT NULL\u003c/code\u003e 约束，以防止出现这些情况。）\u003c/p\u003e\u003cp\u003e此外，当被引用列中的数据发生变化时，会对本表列中的数据执行某些操作。\u003ccode\u003eON DELETE\u003c/code\u003e 子句指定当被引用表中的被引用行被删除时要执行的操作。同样，\u003ccode\u003eON UPDATE\u003c/code\u003e 子句指定当被引用表中的被引用列被更新为新值时要执行的操作。如果更新了行，但被引用列实际上并未改变，则不执行任何操作。引用动作作为数据更改命令的一部分执行，即使该约束是延迟的也一样。每个子句可用的操作如下：\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eNO ACTION\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\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如果待删除或更新的行在引用表中有匹配行，则产生错误。即使操作完成后的状态并不违反外键约束，也会阻止该操作。特别地，它会阻止将被引用行更新为“值不同但比较相等”的情况。（但不会阻止把列更新为相同值的\u003cspan\u003e“\u003cspan\u003e无操作\u003c/span\u003e”\u003c/span\u003e更新。）\u003c/p\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分别删除任何引用已删除行的行，或将引用列的值更新为被引用列的新值。\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET NULL [ ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将全部引用列，或指定的引用列子集，设置为空值。只有 \u003ccode\u003eON DELETE\u003c/code\u003e 动作才能指定列子集。\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eSET DEFAULT [ ( \u003cem\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将全部引用列，或指定的引用列子集，设置为其默认值。只有 \u003ccode\u003eON DELETE\u003c/code\u003e 动作才能指定列子集。（如果默认值不是空值，则被引用表中必须有一行与这些默认值匹配，否则该操作会失败。）\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e如果被引用列经常变化，可以考虑在引用列上添加索引，使与外键约束关联的引用操作能够更高效地执行。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDEFERRABLE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eNOT DEFERRABLE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句控制约束是否可延迟。不可延迟的约束会在每条命令之后立即检查。可延迟约束的检查可以推迟到事务结束时（使用 \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\" rel=\"nofollow\"\u003e\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e 命令）。\u003ccode\u003eNOT DEFERRABLE\u003c/code\u003e 是默认值。当前，只有 \u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e、\u003ccode\u003eEXCLUDE\u003c/code\u003e 和 \u003ccode\u003eREFERENCES\u003c/code\u003e（外键）约束接受该子句。\u003ccode\u003eNOT NULL\u003c/code\u003e 和 \u003ccode\u003eCHECK\u003c/code\u003e 约束不可延迟。注意在包含 \u003ccode\u003eON CONFLICT\u003c/code\u003e 子句的 \u003ccode\u003eINSERT\u003c/code\u003e 语句中，可延迟约束不能用作冲突仲裁器。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eINITIALLY IMMEDIATE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eINITIALLY DEFERRED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果约束可延迟，该子句指定默认的约束检查时间。如果约束是 \u003ccode\u003eINITIALLY IMMEDIATE\u003c/code\u003e，则会在每条语句之后检查。这是默认值。如果约束是 \u003ccode\u003eINITIALLY DEFERRED\u003c/code\u003e，则只会在事务结束时检查。约束检查时间可以用 \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\" rel=\"nofollow\"\u003e\u003ccode\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e 命令修改。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eENFORCED\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan\u003e\u003ccode\u003eNOT ENFORCED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e当约束为 \u003ccode\u003eENFORCED\u003c/code\u003e 时，数据库系统会在适当时机检查该约束（按需要在每条语句后或事务结束时），以确保约束成立。这是默认行为。若约束为 \u003ccode\u003eNOT ENFORCED\u003c/code\u003e，数据库系统将不再检查该约束，改由应用代码保证约束成立。对于不影响结果正确性的优化决策，数据库系统仍可能假定数据满足该约束。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eNOT ENFORCED\u003c/code\u003e 约束可作为文档用途，适用于运行时检查约束代价过高的场景。\u003c/p\u003e\u003cp\u003e当前该能力仅支持外键约束与 \u003ccode\u003eCHECK\u003c/code\u003e 约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUSING \u003cem\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该可选子句指定存储新表内容所用的表访问方法；该方法必须是类型为 \u003ccode\u003eTABLE\u003c/code\u003e 的访问方法。详见\u003ca href=\"/docs/18/tableam.html\" rel=\"nofollow\"\u003e第 62 章\u003c/a\u003e。如果未指定此选项，则会为新表选择默认表访问方法。详见\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\" rel=\"nofollow\"\u003edefault_table_access_method\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e创建分区时，若其分区表已设置表访问方法，则该分区会使用分区表的访问方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eWITH ( \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#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"存储参数\" rel=\"nofollow\"\u003eStorage Parameters\u003c/a\u003e。为了向后兼容，表的 \u003ccode\u003eWITH\u003c/code\u003e 子句还可以包含 \u003ccode\u003eOIDS=FALSE\u003c/code\u003e，以指定新表的行不应包含 OID（对象标识符）；\u003ccode\u003eOIDS=TRUE\u003c/code\u003e 已不再受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eWITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这是声明表 \u003ccode\u003eWITHOUT OIDS\u003c/code\u003e 的向后兼容语法；创建 \u003ccode\u003eWITH OIDS\u003c/code\u003e 表已不再受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eON COMMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可以使用 \u003ccode\u003eON COMMIT\u003c/code\u003e 控制临时表在事务块结束时的行为。三种选项如下：\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003ePRESERVE ROWS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在事务结束时不执行任何特殊操作。这是默认行为。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDELETE ROWS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在每个事务块结束时删除临时表中的所有行。本质上，相当于在每次提交时自动执行一次 \u003ca href=\"/docs/18/sql-truncate.html\" title=\"TRUNCATE\" rel=\"nofollow\"\u003e\u003ccode\u003eTRUNCATE\u003c/code\u003e\u003c/a\u003e。用于分区表时，此操作不会级联到其分区。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eDROP\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\u003eTABLESPACE \u003cem\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003cem\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e 是新表要创建于其中的表空间名称。若未指定，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\" rel=\"nofollow\"\u003edefault_tablespace\u003c/a\u003e；如果该表是临时表，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\" rel=\"nofollow\"\u003etemp_tablespaces\u003c/a\u003e。对于分区表，由于表本身不需要存储，所指定的表空间会覆盖 \u003ccode\u003edefault_tablespace\u003c/code\u003e，成为在未显式指定其他表空间时新建分区所使用的默认表空间。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUSING INDEX TABLESPACE \u003cem\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句允许选择与 \u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 或 \u003ccode\u003eEXCLUDE\u003c/code\u003e 约束相关联的索引要创建在哪个表空间中。若未指定，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\" rel=\"nofollow\"\u003edefault_tablespace\u003c/a\u003e；如果该表是临时表，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\" rel=\"nofollow\"\u003etemp_tablespaces\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e存储参数\u003c/h3\u003e\u003cp\u003e\u003ccode\u003eWITH\u003c/code\u003e 子句可以为表以及与 \u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 或 \u003ccode\u003eEXCLUDE\u003c/code\u003e 约束关联的索引指定\u003cem\u003e存储参数\u003c/em\u003e。索引的存储参数已在\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e中介绍。当前可用于表的存储参数列在下文。对于其中许多参数，如下所示，还存在一个同名且带 \u003ccode\u003etoast.\u003c/code\u003e 前缀的附加参数，用于控制该表的二级 \u003cacronym\u003eTOAST\u003c/acronym\u003e 表（如果有）的行为（更多有关 TOAST 的信息见\u003ca href=\"/docs/18/storage-toast.html\" rel=\"nofollow\"\u003e第 66.2 节\u003c/a\u003e）。如果设置了表参数值，而对应的 \u003ccode\u003etoast.\u003c/code\u003e 参数未设置，则 TOAST 表将使用该表的参数值。不支持为分区表指定这些参数，但可以为单独的叶子分区指定。\u003c/p\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003efillfactor\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e表的填充因子是介于 10 和 100 之间的百分比。100（完全填充）是默认值。当指定较小的填充因子时，\u003ccode\u003eINSERT\u003c/code\u003e 操作只会把表页填充到指定百分比；每页剩余的空间则预留给更新该页上的行。这使得 \u003ccode\u003eUPDATE\u003c/code\u003e 有机会将更新后的行副本放在与原始行相同的页上，这比放在不同页上更高效，也更有可能发生\u003ca href=\"/docs/18/storage-hot.html\" rel=\"nofollow\"\u003e堆内元组更新\u003c/a\u003e。对于条目永远不会更新的表，完全填充是最佳选择；但对频繁更新的表来说，较小的填充因子更合适。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003etoast_tuple_target\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003etoast_tuple_target 指定在尝试压缩和/或将长列值移入 TOAST 表之前所需的最小元组长度，同时也是一旦开始 TOAST 化后试图将长度缩减到其以下的目标长度。这会影响标记为 External（用于移动）、Main（用于压缩）或 Extended（用于两者）的列，并且只适用于新元组。对现有行没有影响。默认情况下，该参数被设置为至少允许每个块容纳 4 个元组，在默认块大小下即为 2040 字节。有效值介于 128 字节与（块大小减去页头）之间，默认上限为 8160 字节。对非常短或非常长的行，修改该值可能没有帮助。注意，默认设置通常已经接近最佳，某些情况下设置该参数可能产生负面影响。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eparallel_workers\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该参数设置在对该表执行并行扫描时用于辅助扫描的工作进程数量。若未设置，系统会根据关系大小决定一个值。规划器或使用并行扫描的实用命令实际选择的工作进程数量可能更少，例如可能受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES\" rel=\"nofollow\"\u003emax_worker_processes\u003c/a\u003e设置所限。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_enabled\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_enabled\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e为特定表启用或禁用自动清理守护进程。如果为真，自动清理守护进程将按照\u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" rel=\"nofollow\"\u003e第 24.1.6 节\u003c/a\u003e中讨论的规则，在该表上执行自动 \u003ccode\u003eVACUUM\u003c/code\u003e 和/或 \u003ccode\u003eANALYZE\u003c/code\u003e 操作。如果为假，则该表不会被自动清理，但为了防止事务 ID 回卷，仍可能对其执行自动清理。有关回卷防护的更多信息，见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" rel=\"nofollow\"\u003e第 24.1.5 节\u003c/a\u003e。注意，如果\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM\" rel=\"nofollow\"\u003eautovacuum\u003c/a\u003e参数为假，则自动清理守护进程根本不会运行（防止事务 ID 回卷的情况除外）；为单独表设置存储参数也不会覆盖这一点。因此，显式将此存储参数设为 \u003ccode\u003etrue\u003c/code\u003e 往往意义不大，设为 \u003ccode\u003efalse\u003c/code\u003e 才更有用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003evacuum_index_cleanup\u003c/code\u003e, \u003ccode\u003etoast.vacuum_index_cleanup\u003c/code\u003e (\u003ccode\u003eenum\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e强制或禁用在该表上执行 \u003ccode\u003eVACUUM\u003c/code\u003e 时的索引清理。默认值为 \u003ccode\u003eAUTO\u003c/code\u003e。设为 \u003ccode\u003eOFF\u003c/code\u003e 时禁用索引清理，设为 \u003ccode\u003eON\u003c/code\u003e 时启用，设为 \u003ccode\u003eAUTO\u003c/code\u003e 时，则每次运行 \u003ccode\u003eVACUUM\u003c/code\u003e 都会动态决定。动态行为允许 \u003ccode\u003eVACUUM\u003c/code\u003e 避免为了移除很少的死元组而无谓地扫描索引。强制禁用全部索引清理可以显著加快 \u003ccode\u003eVACUUM\u003c/code\u003e，但如果表修改频繁，也可能导致索引严重膨胀。若在 \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\" rel=\"nofollow\"\u003e\u003ccode\u003eVACUUM\u003c/code\u003e\u003c/a\u003e 中指定了 \u003ccode\u003eINDEX_CLEANUP\u003c/code\u003e 参数，则会覆盖此选项的值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003evacuum_truncate\u003c/code\u003e, \u003ccode\u003etoast.vacuum_truncate\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\" rel=\"nofollow\"\u003evacuum_truncate\u003c/a\u003e参数的每表取值。若 \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\" rel=\"nofollow\"\u003e\u003ccode\u003eVACUUM\u003c/code\u003e\u003c/a\u003e 指定了 \u003ccode\u003eTRUNCATE\u003c/code\u003e 参数，将覆盖此选项的值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_threshold\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_threshold\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD\" rel=\"nofollow\"\u003eautovacuum_vacuum_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_max_threshold\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_max_threshold\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-MAX-THRESHOLD\" rel=\"nofollow\"\u003eautovacuum_vacuum_max_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_scale_factor\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_scale_factor\u003c/code\u003e (\u003ccode\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR\" rel=\"nofollow\"\u003eautovacuum_vacuum_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_insert_threshold\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_insert_threshold\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-THRESHOLD\" rel=\"nofollow\"\u003eautovacuum_vacuum_insert_threshold\u003c/a\u003e参数的每表取值。特殊值 -1 可用于禁用该表上的由插入触发的清理。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_insert_scale_factor\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_insert_scale_factor\u003c/code\u003e (\u003ccode\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-SCALE-FACTOR\" rel=\"nofollow\"\u003eautovacuum_vacuum_insert_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_analyze_threshold\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-THRESHOLD\" rel=\"nofollow\"\u003eautovacuum_analyze_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_analyze_scale_factor\u003c/code\u003e (\u003ccode\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-SCALE-FACTOR\" rel=\"nofollow\"\u003eautovacuum_analyze_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_cost_delay\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_cost_delay\u003c/code\u003e (\u003ccode\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-DELAY\" rel=\"nofollow\"\u003eautovacuum_vacuum_cost_delay\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_vacuum_cost_limit\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_vacuum_cost_limit\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-LIMIT\" rel=\"nofollow\"\u003eautovacuum_vacuum_cost_limit\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_freeze_min_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_freeze_min_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\" rel=\"nofollow\"\u003evacuum_freeze_min_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\" rel=\"nofollow\"\u003eautovacuum_freeze_max_age\u003c/a\u003e设置一半的每表 \u003ccode\u003eautovacuum_freeze_min_age\u003c/code\u003e 参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_freeze_max_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_freeze_max_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\" rel=\"nofollow\"\u003eautovacuum_freeze_max_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围设置的每表 \u003ccode\u003eautovacuum_freeze_max_age\u003c/code\u003e 参数（它只能设置得更小）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_freeze_table_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_freeze_table_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\" rel=\"nofollow\"\u003evacuum_freeze_table_age\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_multixact_freeze_min_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-MIN-AGE\" rel=\"nofollow\"\u003evacuum_multixact_freeze_min_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\" rel=\"nofollow\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e设置一半的每表 \u003ccode\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e 参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_multixact_freeze_max_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\" rel=\"nofollow\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围设置的每表 \u003ccode\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e 参数（它只能设置得更小）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eautovacuum_multixact_freeze_table_age\u003c/code\u003e, \u003ccode\u003etoast.autovacuum_multixact_freeze_table_age\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-TABLE-AGE\" rel=\"nofollow\"\u003evacuum_multixact_freeze_table_age\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003elog_autovacuum_min_duration\u003c/code\u003e, \u003ccode\u003etoast.log_autovacuum_min_duration\u003c/code\u003e (\u003ccode\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-logging.html#GUC-LOG-AUTOVACUUM-MIN-DURATION\" rel=\"nofollow\"\u003elog_autovacuum_min_duration\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003evacuum_max_eager_freeze_failure_rate\u003c/code\u003e, \u003ccode\u003etoast.vacuum_max_eager_freeze_failure_rate\u003c/code\u003e (\u003ccode\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MAX-EAGER-FREEZE-FAILURE-RATE\" rel=\"nofollow\"\u003evacuum_max_eager_freeze_failure_rate\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003euser_catalog_table\u003c/code\u003e (\u003ccode\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将该表声明为逻辑复制用途的附加目录表。详见\u003ca href=\"/docs/18/logicaldecoding-output-plugin.html#LOGICALDECODING-CAPABILITIES\" rel=\"nofollow\"\u003e第 47.6.2 节\u003c/a\u003e。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\u003cspan\u003ePostgreSQL\u003c/span\u003e为每一个唯一约束和主键约束自动创建一个索引来强制唯一性。因此，没有必要显式地为主键列创建一个索引（详见\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e）。\u003c/p\u003e\u003cp\u003e在当前的实现中，唯一约束和主键不会被继承。这使得继承与唯一约束的组合相当不实用。\u003c/p\u003e\u003cp\u003e一个表不能有超过 1600 列（实际上，由于元组长度限制，有效的限制通常更低）。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e创建表\u003ccode\u003efilms\u003c/code\u003e和表\u003ccode\u003edistributors\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE TABLE films (\n    code        char(5) CONSTRAINT firstkey PRIMARY KEY,\n    title       varchar(40) NOT NULL,\n    did         integer NOT NULL,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute\n);\n\nCREATE TABLE distributors (\n     did    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,\n     name   varchar(40) NOT NULL CHECK (name \u0026lt;\u0026gt; \u0026#39;\u0026#39;)\n);\n\u003c/pre\u003e\u003cp\u003e创建一个带二维数组列的表：\u003c/p\u003e\u003cpre\u003eCREATE TABLE array_int (\n    vector  int[][]\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode\u003efilms\u003c/code\u003e定义一个唯一表约束。唯一表约束可以定义在表的一列或多列上：\u003c/p\u003e\u003cpre\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT production UNIQUE(date_prod)\n);\n\u003c/pre\u003e\u003cp\u003e定义一个列检查约束：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer CHECK (did \u0026gt; 100),\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003e定义一个表检查约束：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    CONSTRAINT con1 CHECK (did \u0026gt; 100 AND name \u0026lt;\u0026gt; \u0026#39;\u0026#39;)\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode\u003efilms\u003c/code\u003e定义一个主键表约束：\u003c/p\u003e\u003cpre\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT code_title PRIMARY KEY(code,title)\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode\u003edistributors\u003c/code\u003e定义一个主键约束。下面的两个示例是等价的，第一个使用表约束语法，第二个使用列约束语法：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    PRIMARY KEY(did)\n);\n\nCREATE TABLE distributors (\n    did     integer PRIMARY KEY,\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003e为列\u003ccode\u003ename\u003c/code\u003e指定一个字面常量默认值，将列\u003ccode\u003edid\u003c/code\u003e的默认值设为从某个序列对象中取下一个值，并让\u003ccode\u003emodtime\u003c/code\u003e的默认值为插入该行的时间：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    name      varchar(40) DEFAULT \u0026#39;Luso Films\u0026#39;,\n    did       integer DEFAULT nextval(\u0026#39;distributors_serial\u0026#39;),\n    modtime   timestamp DEFAULT current_timestamp\n);\n\u003c/pre\u003e\u003cp\u003e在表\u003ccode\u003edistributors\u003c/code\u003e上定义两个\u003ccode\u003eNOT NULL\u003c/code\u003e列约束，其中一个显式指定了名称：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer CONSTRAINT no_null NOT NULL,\n    name    varchar(40) NOT NULL\n);\n\u003c/pre\u003e\u003cp\u003e为\u003ccode\u003ename\u003c/code\u003e列定义一个唯一约束：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40) UNIQUE\n);\n\u003c/pre\u003e\u003cp\u003e同样的唯一约束用表约束指定：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name)\n);\n\u003c/pre\u003e\u003cp\u003e创建同样的表，并为该表及其唯一索引都指定 70% 的填充因子：\u003c/p\u003e\u003cpre\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name) WITH (fillfactor=70)\n)\nWITH (fillfactor=70);\n\u003c/pre\u003e\u003cp\u003e创建表\u003ccode\u003ecircles\u003c/code\u003e，并添加一个排他约束以防任意两个圆重叠：\u003c/p\u003e\u003cpre\u003eCREATE TABLE circles (\n    c circle,\n    EXCLUDE USING gist (c WITH \u0026amp;\u0026amp;)\n);\n\u003c/pre\u003e\u003cp\u003e在表空间\u003ccode\u003ediskvol1\u003c/code\u003e中创建表\u003ccode\u003ecinemas\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE TABLE cinemas (\n        id serial,\n        name text,\n        location text\n) TABLESPACE diskvol1;\n\u003c/pre\u003e\u003cp\u003e创建一个复合类型和一个类型化表：\u003c/p\u003e\u003cpre\u003eCREATE TYPE employee_type AS (name text, salary numeric);\n\nCREATE TABLE employees OF employee_type (\n    PRIMARY KEY (name),\n    salary WITH OPTIONS DEFAULT 1000\n);\n\u003c/pre\u003e\u003cp\u003e创建一个范围分区表：\u003c/p\u003e\u003cpre\u003eCREATE TABLE measurement (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (logdate);\n\u003c/pre\u003e\u003cp\u003e创建一个在分区键中包含多个列的范围分区表：\u003c/p\u003e\u003cpre\u003eCREATE TABLE measurement_year_month (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (EXTRACT(YEAR FROM logdate), EXTRACT(MONTH FROM logdate));\n\u003c/pre\u003e\u003cp\u003e创建列表分区表：\u003c/p\u003e\u003cpre\u003eCREATE TABLE cities (\n    city_id      bigserial not null,\n    name         text not null,\n    population   bigint\n) PARTITION BY LIST (left(lower(name), 1));\n\u003c/pre\u003e\u003cp\u003e创建 hash 分区表：\u003c/p\u003e\u003cpre\u003eCREATE TABLE orders (\n    order_id     bigint not null,\n    cust_id      bigint not null,\n    status       text\n) PARTITION BY HASH (order_id);\n\u003c/pre\u003e\u003cp\u003e创建范围分区表的分区：\u003c/p\u003e\u003cpre\u003eCREATE TABLE measurement_y2016m07\n    PARTITION OF measurement (\n    unitsales DEFAULT 0\n) 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\u003eCREATE TABLE measurement_ym_older\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (MINVALUE, MINVALUE) TO (2016, 11);\n\nCREATE TABLE measurement_ym_y2016m11\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 11) TO (2016, 12);\n\nCREATE TABLE measurement_ym_y2016m12\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 12) TO (2017, 01);\n\nCREATE TABLE measurement_ym_y2017m01\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2017, 01) TO (2017, 02);\n\u003c/pre\u003e\u003cp\u003e创建列表分区表的分区：\u003c/p\u003e\u003cpre\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN (\u0026#39;a\u0026#39;, \u0026#39;b\u0026#39;);\n\u003c/pre\u003e\u003cp\u003e创建一个本身还要进一步分区的列表分区表分区，然后再向其添加一个分区：\u003c/p\u003e\u003cpre\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN (\u0026#39;a\u0026#39;, \u0026#39;b\u0026#39;) PARTITION BY RANGE (population);\n\nCREATE TABLE cities_ab_10000_to_100000\n    PARTITION OF cities_ab FOR VALUES FROM (10000) TO (100000);\n\u003c/pre\u003e\u003cp\u003e创建 hash 分区表的分区：\u003c/p\u003e\u003cpre\u003eCREATE TABLE orders_p1 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 0);\nCREATE TABLE orders_p2 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 1);\nCREATE TABLE orders_p3 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 2);\nCREATE TABLE orders_p4 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003e创建默认分区：\u003c/p\u003e\u003cpre\u003eCREATE TABLE cities_partdef\n    PARTITION OF cities DEFAULT;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e 命令符合 \u003cacronym\u003eSQL\u003c/acronym\u003e 标准，但有下列例外。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e临时表\u003c/h3\u003e\u003cp\u003e尽管 \u003ccode\u003eCREATE TEMPORARY TABLE\u003c/code\u003e 的语法看起来类似于 SQL 标准，但其效果并不相同。按标准，临时表只需定义一次，并会自动存在于每个需要它的会话中（内容初始为空）。而 \u003cspan\u003ePostgreSQL\u003c/span\u003e 要求每个会话都为每个要使用的临时表发出自己的 \u003ccode\u003eCREATE TEMPORARY TABLE\u003c/code\u003e 命令。这使不同会话可以出于不同目的使用相同的临时表名；而标准做法则要求给定临时表名的所有实例都必须具有相同的表结构。\u003c/p\u003e\u003cp\u003e标准对临时表行为的定义在实践中被广泛忽略。\u003cspan\u003ePostgreSQL\u003c/span\u003e 在这一点上的行为与多种其他 SQL 数据库相似。\u003c/p\u003e\u003cp\u003eSQL 标准还区分全局和局部临时表，其中局部临时表在每个会话内的每个 SQL 模块中都有独立的内容集合，但其定义仍在多个会话之间共享。由于 \u003cspan\u003ePostgreSQL\u003c/span\u003e 不支持 SQL 模块，这一区别在 \u003cspan\u003ePostgreSQL\u003c/span\u003e 中没有意义。\u003c/p\u003e\u003cp\u003e出于兼容性考虑，\u003cspan\u003ePostgreSQL\u003c/span\u003e 接受在临时表声明中使用 \u003ccode\u003eGLOBAL\u003c/code\u003e 和 \u003ccode\u003eLOCAL\u003c/code\u003e 关键字，但它们目前没有效果。不鼓励使用这些关键字，因为未来版本的 \u003cspan\u003ePostgreSQL\u003c/span\u003e 可能会采用更符合标准的解释。\u003c/p\u003e\u003cp\u003e临时表的 \u003ccode\u003eON COMMIT\u003c/code\u003e 子句也与 SQL 标准相似，但存在一些差异。如果省略 \u003ccode\u003eON COMMIT\u003c/code\u003e 子句，SQL 规定默认行为是 \u003ccode\u003eON COMMIT DELETE ROWS\u003c/code\u003e。然而，\u003cspan\u003ePostgreSQL\u003c/span\u003e 中的默认行为是 \u003ccode\u003eON COMMIT PRESERVE ROWS\u003c/code\u003e。SQL 中不存在 \u003ccode\u003eON COMMIT DROP\u003c/code\u003e 选项。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e非延迟唯一性约束\u003c/h3\u003e\u003cp\u003e当 \u003ccode\u003eUNIQUE\u003c/code\u003e 或 \u003ccode\u003ePRIMARY KEY\u003c/code\u003e 约束不可延迟时，只要有行被插入或修改，\u003cspan\u003ePostgreSQL\u003c/span\u003e 就会立刻检查唯一性。SQL 标准规定应只在语句结束时强制唯一性；例如，当单个命令会更新多个键值时，这两者就会产生差异。若要获得符合标准的行为，应将约束声明为 \u003ccode\u003eDEFERRABLE\u003c/code\u003e 但不延迟（即 \u003ccode\u003eINITIALLY IMMEDIATE\u003c/code\u003e）。注意，这可能明显慢于立即检查唯一性。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e列检查约束\u003c/h3\u003e\u003cp\u003eSQL 标准规定，\u003ccode\u003eCHECK\u003c/code\u003e 列约束只能引用其所作用的列；只有 \u003ccode\u003eCHECK\u003c/code\u003e 表约束才能引用多列。\u003cspan\u003ePostgreSQL\u003c/span\u003e 并不强制这一限制；它对列检查约束和表检查约束一视同仁。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003eEXCLUDE\u003c/code\u003e 约束\u003c/h3\u003e\u003cp\u003e\u003ccode\u003eEXCLUDE\u003c/code\u003e 约束类型是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e外键约束\u003c/h3\u003e\u003cp\u003e在外键动作 \u003ccode\u003eSET DEFAULT\u003c/code\u003e 和 \u003ccode\u003eSET NULL\u003c/code\u003e 中指定列列表的能力，是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003cp\u003e外键约束可以引用唯一索引的列，而不仅限于主键或唯一约束的列，这也是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003eNULL\u003c/code\u003e \u003cspan\u003e“\u003cspan\u003e约束\u003c/span\u003e”\u003c/span\u003e\u003c/h3\u003e\u003cp\u003e\u003ccode\u003eNULL\u003c/code\u003e \u003cspan\u003e“\u003cspan\u003e约束\u003c/span\u003e”\u003c/span\u003e（实际上并不是约束）是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 对 SQL 标准的扩展；提供它是为了与其他一些数据库系统兼容（以及与 \u003ccode\u003eNOT NULL\u003c/code\u003e 约束保持对称）。由于它本来就是任意列的默认情况，所以它的存在只是噪声。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e约束命名\u003c/h3\u003e\u003cp\u003eSQL 标准规定，在包含表或域的模式范围内，表约束和域约束都必须具有唯一名称。\u003cspan\u003ePostgreSQL\u003c/span\u003e 较为宽松：它只要求约束名称在附加到特定表或域的约束之间唯一。不过，对于基于索引的约束（\u003ccode\u003eUNIQUE\u003c/code\u003e、\u003ccode\u003ePRIMARY KEY\u003c/code\u003e 和 \u003ccode\u003eEXCLUDE\u003c/code\u003e 约束）就没有这种额外自由，因为关联索引与约束同名，而索引名称在同一模式内的所有关系之间必须唯一。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e继承\u003c/h3\u003e\u003cp\u003e通过 \u003ccode\u003eINHERITS\u003c/code\u003e 子句实现的多重继承是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的语言扩展。SQL:1999 及后续标准使用不同的语法和语义定义了单继承。\u003cspan\u003ePostgreSQL\u003c/span\u003e 尚不支持 SQL:1999 风格的继承。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e零列的表\u003c/h3\u003e\u003cp\u003e\u003cspan\u003ePostgreSQL\u003c/span\u003e 允许创建没有列的表（例如 \u003ccode\u003eCREATE TABLE foo();\u003c/code\u003e）。这是对 SQL 标准的扩展，标准不允许零列的表。零列的表本身并不十分有用，但若禁止它们，就会让 \u003ccode\u003eALTER TABLE DROP COLUMN\u003c/code\u003e 出现奇怪的特殊情况，因此忽略这一规范限制看起来更整洁。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e多个标识列\u003c/h3\u003e\u003cp\u003e\u003cspan\u003ePostgreSQL\u003c/span\u003e 允许一个表拥有多个标识列。该标准指定一个表最多只能有一个标识列。放宽这一限制主要是为了给模式更改或迁移提供更大的灵活性。请注意，\u003ccode\u003eINSERT\u003c/code\u003e 命令仅支持一个适用于整个语句的覆盖子句，因此对行为不同的多个标识列支持并不好。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e生成列\u003c/h3\u003e\u003cp\u003e\u003ccode\u003eSTORED\u003c/code\u003e 和 \u003ccode\u003eVIRTUAL\u003c/code\u003e 选项都不是标准的一部分，但其他 SQL 实现也会使用。SQL 标准并不规定生成列的存储方式。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003eLIKE\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e虽然 SQL 标准中存在 \u003ccode\u003eLIKE\u003c/code\u003e 子句，但 \u003cspan\u003ePostgreSQL\u003c/span\u003e 接受的许多 LIKE 选项并不在标准中，而标准中的某些选项又没有被 \u003cspan\u003ePostgreSQL\u003c/span\u003e 实现。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003eWITH\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode\u003eWITH\u003c/code\u003e 子句是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展；存储参数不属于标准内容。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e表空间\u003c/h3\u003e\u003cp\u003e\u003cspan\u003ePostgreSQL\u003c/span\u003e 的表空间概念不是标准的一部分。因此，\u003ccode\u003eTABLESPACE\u003c/code\u003e 和 \u003ccode\u003eUSING INDEX TABLESPACE\u003c/code\u003e 子句都是扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e类型化表\u003c/h3\u003e\u003cp\u003e类型化表实现了 SQL 标准的一个子集。按照标准，类型化表除了具有与底层复合类型相对应的列之外，还应有一个额外的\u003cspan\u003e“\u003cspan\u003e自引用列\u003c/span\u003e”\u003c/span\u003e。\u003cspan\u003ePostgreSQL\u003c/span\u003e 不显式支持自引用列。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003ePARTITION BY\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode\u003ePARTITION BY\u003c/code\u003e 子句是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv\u003e\u003ch3\u003e\u003ccode\u003ePARTITION OF\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode\u003ePARTITION OF\u003c/code\u003e 子句是 \u003cspan\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/alter-table/?v=18\" title=\"ALTER TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-table/?v=18\" title=\"DROP TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-tablespace/?v=18\" title=\"CREATE TABLESPACE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"974c2dda3c08ee3f72bcbdb6246af5cb68e7cc78dec4c6e154a3c9960b902ae8","Payload":{"purpose_zh":"定义一个新表","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e 将在当前数据库中创建一个新的、初始为空的表。该表归发出该命令的用户所有。\u003c/p\u003e\u003cp\u003e如果给出了模式名（例如 \u003ccode class=\"literal\"\u003eCREATE TABLE myschema.mytable ...\u003c/code\u003e），则表将在指定模式中创建。否则，它将在当前模式中创建。临时表存在于一个特殊模式中，因此创建临时表时不能给出模式名。表名必须与同一模式中任何其他关系（表、序列、索引、视图、物化视图或外部表）的名称不同。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e 还会自动创建一种数据类型，用以表示与该表一行对应的复合类型。因此，表名不能与同一模式中任何已有数据类型同名。\u003c/p\u003e\u003cp\u003e可选的约束子句指定插入或更新要成功时，新行或更新后的行必须满足的约束（测试）。约束是一种 SQL 对象，可用多种方式帮助定义表中允许的值集合。\u003c/p\u003e\u003cp\u003e定义约束有两种方式：表约束和列约束。列约束作为列定义的一部分定义。表约束则不绑定到特定列，并且可以涵盖多个列。每个列约束也都可以写成表约束；当约束只影响一列时，列约束只是一种书写上的方便。\u003c/p\u003e\u003cp\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\"\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定该选项，表将创建为临时表。临时表会在会话结束时自动删除，或者也可在当前事务结束时删除（见下文 \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e）。默认的 search_path 会首先包含临时模式，因此在临时表存在期间，除非使用带模式限定的名称引用，否则不会在新计划中选中同名的现有永久表。在临时表上创建的任何索引也都会自动成为临时索引。\u003c/p\u003e\u003cp\u003e\u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. 自动清理守护进程\"\u003e自动清理守护进程\u003c/a\u003e不能访问并且因此也不能清理或分析临时表。由于这个原因，应该通过会话的 SQL 命令执行合适的清理和分析操作。例如，如果一个临时表将要被用于复杂的查询，最好在把它填充完毕后在其上运行\u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e可以在 \u003ccode class=\"literal\"\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e 前写 \u003ccode class=\"literal\"\u003eGLOBAL\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e。这在当前的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 中没有区别，而且已弃用；见下文\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-COMPATIBILITY\" title=\"兼容性\"\u003eCompatibility\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定该选项，表将创建为不记录 WAL 的表。写入不记录 WAL 的表的数据不会写入预写式日志（见\u003ca href=\"/docs/18/wal.html\" title=\"第 28 章 可靠性与预写式日志\"\u003e第 28 章\u003c/a\u003e），因此它们比普通表快得多。不过，它们不具备崩溃安全性：在崩溃或非正常关闭后，不记录 WAL 的表会被自动截断。不记录 WAL 的表的内容也不会复制到备库。在不记录 WAL 的表上创建的任何索引也都会自动成为不记录 WAL 的。\u003c/p\u003e\u003cp\u003e如果指定该选项，与不记录 WAL 的表一起创建的任何序列（用于标识列或 serial 列）也会创建为不记录 WAL 的。\u003c/p\u003e\u003cp\u003e这种形式不支持分区表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF NOT 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\u003etable_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\"\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创建一个\u003cem class=\"firstterm\"\u003e类型化表\u003c/em\u003e，其结构取自指定的独立复合类型（即通过\u003ca href=\"/docs/18/sql-createtype.html\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e创建的类型），同时该表自身也会生成一个新的复合类型。该表会依赖于所引用的类型，这意味着对该类型执行级联的 ALTER 或 DROP 操作会传播到该表。\u003c/p\u003e\u003cp\u003e类型化表的列名和数据类型始终与其所基于的类型相同，因此不能再指定额外的列。但 \u003ccode class=\"literal\"\u003eCREATE TABLE\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\u003edata_type\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列的数据类型，可以包含数组说明符。有关\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e支持的数据类型的更多信息，参见\u003ca href=\"/docs/18/datatype.html\" title=\"第 8 章 数据类型\"\u003e第 8 章\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCOLLATE \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecollation\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCOLLATE\u003c/code\u003e子句为列指定排序规则（该列必须属于支持排序规则的数据类型）。如果未指定，则使用列数据类型的默认排序规则。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSTORAGE { 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 值上的子串操作速度，但代价是占用更多存储空间。详见\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\"\u003eCOMPRESSION \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\u003ccode class=\"literal\"\u003eCOMPRESSION\u003c/code\u003e 子句设置列的压缩方法。压缩仅支持变宽数据类型，并且只会在列的存储模式为 \u003ccode class=\"literal\"\u003emain\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eextended\u003c/code\u003e 时使用。（有关列存储模式的信息，见\u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e。）为分区表设置该属性没有直接影响，因为此类表自身不存储数据，但新创建的分区会继承该配置值。支持的压缩方法是 \u003ccode class=\"literal\"\u003epglz\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003elz4\u003c/code\u003e。（只有在构建 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 时使用了 \u003ccode class=\"option\"\u003e--with-lz4\u003c/code\u003e，\u003ccode class=\"literal\"\u003elz4\u003c/code\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\"\u003eINHERITS ( \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可选的 \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e 子句指定一组表，新表将自动从中继承所有列。父表可以是普通表或外部表。\u003c/p\u003e\u003cp\u003e使用 \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e 会在新子表与其父表之间建立持久关系。对父表的模式修改通常也会传播到子表，且默认情况下，对父表的扫描会包含子表的数据。\u003c/p\u003e\u003cp\u003e如果同一列名出现在多个父表中，除非这些父表中该列的数据类型全部匹配，否则会报错。如果没有冲突，这些重复列会合并为新表中的单个列。如果新表的列名列表中包含一个同样来自继承的列名，其数据类型也必须与继承列匹配，并且列定义会合并为一个。如果新表显式为该列指定了默认值，该默认值会覆盖继承声明中的任何默认值。否则，任何为该列指定默认值的父表都必须指定相同的默认值，否则会报错。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束基本上也按与列相同的方式合并：如果多个父表和/或新表定义中包含同名的 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束，则这些约束必须拥有相同的检查表达式，否则会报错。同名且表达式相同的约束将合并为一份。父表中标记为 \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e 的约束不会被考虑。注意，新表中未命名的 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束永远不会被合并，因为系统总会为它选择一个唯一名称。\u003c/p\u003e\u003cp\u003e列的 \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e 设置也会从父表复制过来。\u003c/p\u003e\u003cp\u003e如果父表中的列是标识列，则该属性不会被继承。若需要，可将子表中的列声明为标识列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARTITION BY { RANGE | LIST | HASH } ( { \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 ) } [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eopclass\u003c/code\u003e\u003c/em\u003e ] [, ...] )\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可选的 \u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e 子句指定表的分区策略。这样创建的表称为\u003cem class=\"firstterm\"\u003e分区表\u003c/em\u003e。括号中的列或表达式列表构成该表的\u003cem class=\"firstterm\"\u003e分区键\u003c/em\u003e。使用范围分区或 hash 分区时，分区键可以包含多个列或表达式（最多 32 个，但该限制可在构建 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 时调整）；而列表分区的分区键必须由单个列或表达式组成。\u003c/p\u003e\u003cp\u003e范围分区和列表分区需要 B-树操作符类，hash 分区则需要 hash 操作符类。如果未显式指定操作符类，将使用相应类型的默认操作符类；若不存在默认操作符类，则会报错。使用 hash 分区时，所用操作符类必须实现支持函数 2（详见\u003ca href=\"/docs/18/xindex.html#XINDEX-SUPPORT\" title=\"36.16.3. 索引方法支持例程\"\u003e第 36.16.3 节\u003c/a\u003e）。\u003c/p\u003e\u003cp\u003e分区表被划分为多个子表（称为分区），它们使用单独的 \u003ccode class=\"literal\"\u003eCREATE TABLE\u003c/code\u003e 命令创建。分区表本身为空。插入到该表的数据行会根据分区键中列或表达式的值被路由到相应分区。如果没有现有分区与新行中的值匹配，就会报错。\u003c/p\u003e\u003cp\u003e有关表分区的更多讨论，请参阅\u003ca href=\"/docs/18/ddl-partitioning.html\" title=\"5.12. 表分区\"\u003e第 5.12 节\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePARTITION OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\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将该表创建为指定父表的\u003cem class=\"firstterm\"\u003e分区\u003c/em\u003e。可以使用 \u003ccode class=\"literal\"\u003eFOR VALUES\u003c/code\u003e 将其创建为特定值的分区，也可以使用 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 创建为默认分区。父表上已有的任何索引、约束和用户定义的行级触发器都会克隆到新分区上。\u003c/p\u003e\u003cp\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e 必须对应于父表的分区方法和分区键，并且不能与该父表的任何现有分区重叠。带 \u003ccode class=\"literal\"\u003eIN\u003c/code\u003e 的形式用于列表分区，带 \u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e 的形式用于范围分区，带 \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e 的形式用于 hash 分区。\u003c/p\u003e\u003cp\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_expr\u003c/code\u003e\u003c/em\u003e 是任何无变量表达式（不允许子查询、窗口函数、聚合函数和集合返回函数）。它的数据类型必须与相应分区键列的数据类型相匹配。表达式在表创建时只计算一次，因此它甚至可以包含易失性表达式，例如 \u003ccode class=\"literal\"\u003e\u003ccode class=\"function\"\u003eCURRENT_TIMESTAMP\u003c/code\u003e\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e在创建列表分区时，可以指定 \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e，表示该分区允许分区键列为 NULL。但是，对于给定的父表，这样的列表分区不能多于一个。\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e 不能用于范围分区。\u003c/p\u003e\u003cp\u003e创建范围分区时，由 \u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e 指定的下界是包含边界，而由 \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e 指定的上界是不包含边界。也就是说，\u003ccode class=\"literal\"\u003eFROM\u003c/code\u003e 列表中指定的值是该分区相应分区键列的有效值，而 \u003ccode class=\"literal\"\u003eTO\u003c/code\u003e 列表中的值不是。请注意，必须根据按行比较的规则来理解这一点（\u003ca href=\"/docs/18/functions-comparisons.html#ROW-WISE-COMPARISON\" title=\"9.25.5. 行构造器比较\"\u003e第 9.25.5 节\u003c/a\u003e）。例如，给定 \u003ccode class=\"literal\"\u003ePARTITION BY RANGE (x,y)\u003c/code\u003e，分区边界 \u003ccode class=\"literal\"\u003eFROM (1, 2) TO (3, 4)\u003c/code\u003e 允许 \u003ccode class=\"literal\"\u003ex=1\u003c/code\u003e 且任意 \u003ccode class=\"literal\"\u003ey\u0026gt;=2\u003c/code\u003e，\u003ccode class=\"literal\"\u003ex=2\u003c/code\u003e 且任意非 NULL 的 \u003ccode class=\"literal\"\u003ey\u003c/code\u003e，以及 \u003ccode class=\"literal\"\u003ex=3\u003c/code\u003e 且任意 \u003ccode class=\"literal\"\u003ey\u0026lt;4\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e在创建范围分区时，可以使用特殊值 \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e 表示该列值没有下界或上界。例如，使用 \u003ccode class=\"literal\"\u003eFROM (MINVALUE) TO (10)\u003c/code\u003e 定义的分区允许任何小于 10 的值，而使用 \u003ccode class=\"literal\"\u003eFROM (10) TO (MAXVALUE)\u003c/code\u003e 定义的分区允许任何大于或等于 10 的值。\u003c/p\u003e\u003cp\u003e在创建涉及多列的范围分区时，将 \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e 用作下界的一部分、将 \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e 用作上界的一部分也可能是有意义的。例如，使用 \u003ccode class=\"literal\"\u003eFROM (0, MAXVALUE) TO (10, MAXVALUE)\u003c/code\u003e 定义的分区允许第一个分区键列大于 0 且小于或等于 10 的所有行。类似地，使用 \u003ccode class=\"literal\"\u003eFROM ('a', MINVALUE) TO ('b', MINVALUE)\u003c/code\u003e 定义的分区允许第一个分区键列以“a”开头的所有行。\u003c/p\u003e\u003cp\u003e请注意，如果 \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e 用于分区边界中的某一列，则后续所有列都必须使用相同的值。例如，\u003ccode class=\"literal\"\u003e(10, MINVALUE, 0)\u003c/code\u003e 不是有效边界；应写成 \u003ccode class=\"literal\"\u003e(10, MINVALUE, MINVALUE)\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e还要注意，某些元素类型（如 \u003ccode class=\"literal\"\u003etimestamp\u003c/code\u003e）具有“无穷大”的概念，那只是另一种可存储的值。这不同于 \u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e，后两者并非可存储的实际值，而只是表示值无界的方式。\u003ccode class=\"literal\"\u003eMAXVALUE\u003c/code\u003e 可以视为大于任何其他值，包括“无穷大”；\u003ccode class=\"literal\"\u003eMINVALUE\u003c/code\u003e 可以视为小于任何其他值，包括“负无穷大”。因此，范围 \u003ccode class=\"literal\"\u003eFROM ('infinity') TO (MAXVALUE)\u003c/code\u003e 并不是空范围；它只允许存储一个值 — “无穷大”。\u003c/p\u003e\u003cp\u003e如果指定了 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e，则该表将创建为父表的默认分区。此选项不适用于 hash 分区表。任何不适合该父表其他分区的分区键值都会被路由到默认分区。\u003c/p\u003e\u003cp\u003e当一个表已有 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 分区并且要向其添加新分区时，必须扫描默认分区，以验证其中不包含应属于新分区的任何行。如果默认分区包含大量行，这可能会很慢。如果默认分区是外部表，或者它具有可证明不可能包含应放入新分区之行的约束，则会跳过扫描。\u003c/p\u003e\u003cp\u003e创建 hash 分区时，必须指定模数和余数。模数必须是正整数，余数必须是小于模数的非负整数。通常，在最初设置 hash 分区表时，应选择一个等于分区数的模数，并为每个分区指定相同的模数和不同的余数（见下文示例）。不过，并不要求每个分区都有相同的模数；只要求 hash 分区表中出现的每个模数都是下一个更大模数的因数。这样就可以按增量方式增加分区数量，而不必一次移动全部数据。例如，假设你有一个包含 8 个分区的 hash 分区表，每个分区的模数都是 8，但发现有必要将分区数增加到 16。你可以分离其中一个模数为 8 的分区，再创建两个新的模数为 16 的分区来覆盖同一部分键空间（其中一个的余数等于被分离分区的余数，另一个的余数等于该值加 8），然后重新向它们填充数据。之后可以对每个模数为 8 的分区重复这一过程，也许是在之后某个时间点，直到一个不剩。虽然每一步仍可能涉及大量数据移动，但仍优于创建一张全新的表并一次性移动所有数据。\u003c/p\u003e\u003cp\u003e分区必须具有与其所属分区表相同的列名和类型。对分区表列名或类型的修改会自动传播到所有分区。\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束会自动被每个分区继承，但单个分区也可以指定额外的 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束；与父表中名称和条件相同的额外约束将与父表约束合并。可以为每个分区分别指定默认值。但是请注意，在通过分区表插入元组时不会应用分区的默认值。\u003c/p\u003e\u003cp\u003e插入分区表中的行将自动路由到正确的分区。如果不存在合适的分区，则会发生错误。\u003c/p\u003e\u003cp\u003e像 \u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e 这样通常会影响表及其所有继承子表的操作，会级联到所有分区，但也可以在单个分区上执行。\u003c/p\u003e\u003cp\u003e请注意，使用 \u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e 创建分区需要对父分区表获取 \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e 锁。类似地，使用 \u003ccode class=\"command\"\u003eDROP TABLE\u003c/code\u003e 删除分区也需要对父表获取 \u003ccode class=\"literal\"\u003eACCESS EXCLUSIVE\u003c/code\u003e 锁。可以使用 \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003ccode class=\"command\"\u003eALTER TABLE ATTACH/DETACH PARTITION\u003c/code\u003e\u003c/a\u003e 以更弱的锁执行这些操作，从而减少对分区表并发操作的干扰。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eLIKE \u003cem class=\"replaceable\"\u003e\u003ccode\u003esource_table\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e ... ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 子句指定一个表，新表会自动从中复制所有列名、数据类型及其非空约束。\u003c/p\u003e\u003cp\u003e与 \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e 不同，新表和原表在创建完成后就完全脱钩了。对原表的修改不会应用到新表，也不可能在扫描原表时包含新表的数据。\u003c/p\u003e\u003cp\u003e同样与 \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e 不同，由 \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 复制的列和约束不会与同名的列和约束合并。如果同一名称被显式指定，或在另一个 \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 子句中指定，则会报错。\u003c/p\u003e\u003cp\u003e可选的 \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e 子句指定还要复制原表的哪些附加属性。指定 \u003ccode class=\"literal\"\u003eINCLUDING\u003c/code\u003e 表示复制该属性，指定 \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e 表示省略该属性。默认是 \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e。如果对同一类对象给出了多个说明，则采用最后一个。可用选项如下：\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING COMMENTS\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\"\u003eINCLUDING COMPRESSION\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\"\u003eINCLUDING CONSTRAINTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束。列约束与表约束不作区分。非空约束始终会复制到新表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING DEFAULTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义的默认表达式。否则默认表达式不会复制，因此新表中复制出的列默认值为 null。注意，复制那些调用数据库修改函数（如 \u003ccode class=\"function\"\u003enextval\u003c/code\u003e）的默认值，可能在原表与新表之间建立功能性关联。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING GENERATED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义中的生成表达式以及 STORED/VIRTUAL 的选择。默认情况下，新列将是普通基础列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING IDENTITY\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\"\u003eINCLUDING INDEXES\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 和 \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束都会在新表上创建。新索引和约束的名称会按默认规则选择，而不考虑原始名称。（这种行为可避免新索引因名称重复而失败。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINCLUDING STATISTICS\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\"\u003eINCLUDING STORAGE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e会复制被复制列定义的 \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e 设置。默认行为是不复制 \u003ccode class=\"literal\"\u003eSTORAGE\u003c/code\u003e 设置，因此新表中复制出的列将使用类型特定的默认设置。关于 \u003ccode class=\"literal\"\u003eSTORAGE\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\"\u003eINCLUDING ALL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eINCLUDING ALL\u003c/code\u003e 是选择所有可用单项选项的缩写形式。（可以在 \u003ccode class=\"literal\"\u003eINCLUDING ALL\u003c/code\u003e 之后再写单独的 \u003ccode class=\"literal\"\u003eEXCLUDING\u003c/code\u003e 子句，以选中除某些特定选项之外的全部选项。）\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 子句也可用于从视图、外部表或复合类型复制列定义。不适用的选项（例如从视图复制 \u003ccode class=\"literal\"\u003eINCLUDING INDEXES\u003c/code\u003e）会被忽略。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCONSTRAINT \u003cem class=\"replaceable\"\u003e\u003ccode\u003econstraint_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e列约束或表约束的可选名称。如果约束被违反，错误消息中会包含该约束名，因此诸如\u003ccode class=\"literal\"\u003ecol must be positive\u003c/code\u003e这样的约束名可以向客户端应用传达有用的约束信息。（若约束名中包含空格，则需要用双引号指定。）如果未指定约束名，系统会生成一个。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT NULL [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列不允许包含空值。\u003c/p\u003e\u003cp\u003e标记为\u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e的约束不会传播到子表。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该列允许包含空值。这是默认情况。\u003c/p\u003e\u003cp\u003e该子句仅为兼容非标准 SQL 数据库而提供，不建议在新应用中使用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 子句指定一个产生布尔结果的表达式。要使插入或更新成功，新行或更新后的行必须满足该表达式。计算结果为 TRUE 或 UNKNOWN 的表达式视为成功。如果插入或更新操作中的任何一行得到 FALSE 结果，就会抛出错误异常，并且插入或更新不会修改数据库。作为列约束指定的检查约束只应引用该列的值，而出现在表约束中的表达式可以引用多个列。\u003c/p\u003e\u003cp\u003e当前，\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 表达式不能包含子查询，也不能引用当前行的列之外的变量（参见\u003ca href=\"/docs/18/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS\" title=\"5.5.1. 检查约束\"\u003e第 5.5.1 节\u003c/a\u003e）。可以引用系统列 \u003ccode class=\"literal\"\u003etableoid\u003c/code\u003e，但不能引用其他系统列。\u003c/p\u003e\u003cp\u003e标记为 \u003ccode class=\"literal\"\u003eNO INHERIT\u003c/code\u003e 的约束不会传播到子表。\u003c/p\u003e\u003cp\u003e当一个表有多个 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束时，在检查完 \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 约束之后，会按名称的字母顺序对每一行进行检查。（9.5 之前的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 版本并不保证 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束的特定触发顺序。）\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFAULT \u003cem class=\"replaceable\"\u003e\u003ccode\u003edefault_expr\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 子句为其所在列指定默认数据值。该值是一个不含变量的表达式（特别是，不允许引用当前表中的其他列）。子查询也不允许。默认值表达式的数据类型必须匹配列的数据类型。\u003c/p\u003e\u003cp\u003e默认值表达式会用于任何未为该列指定值的插入操作。如果一列没有默认值，则默认值为 null。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eGENERATED ALWAYS AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003egeneration_expr\u003c/code\u003e\u003c/em\u003e ) [ STORED | VIRTUAL ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e此子句将列创建为\u003cem class=\"firstterm\"\u003e生成列\u003c/em\u003e。列不可写入，读取时会返回指定表达式的结果。\u003c/p\u003e\u003cp\u003e指定 \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e 时，列会在读取时计算且不占用存储；指定 \u003ccode class=\"literal\"\u003eSTORED\u003c/code\u003e 时，列会在写入时计算并存储在磁盘上。默认是 \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e生成表达式可以引用表中的其他列，但不能引用其他生成列。使用的任何函数和操作符都必须是不可变的。不允许引用其他表。\u003c/p\u003e\u003cp\u003e虚拟生成列不能使用用户定义类型，且其生成表达式不得引用用户定义函数或类型，也就是只能使用内置函数或类型。此限制也包含间接使用（例如操作符或类型转换背后的函数或类型）。该限制不适用于存储型生成列。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eGENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句将列创建为\u003cem class=\"firstterm\"\u003e标识列\u003c/em\u003e。它会隐式附带一个序列，并且在新插入的行中，该列会自动取得分配给它的序列值。这种列会隐式带有 \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 约束。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eBY 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=\"command\"\u003eINSERT\u003c/code\u003e 命令中，如果选择了 \u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e，则仅当 \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e 语句指定 \u003ccode class=\"literal\"\u003eOVERRIDING SYSTEM VALUE\u003c/code\u003e 时才接受用户指定的值。如果选择了 \u003ccode class=\"literal\"\u003eBY DEFAULT\u003c/code\u003e，则用户指定的值优先。有关详细信息，请参阅\u003ca href=\"/docs/18/sql-insert.html\" title=\"INSERT\"\u003e\u003cspan class=\"refentrytitle\"\u003eINSERT\u003c/span\u003e\u003c/a\u003e。（在 \u003ccode class=\"command\"\u003eCOPY\u003c/code\u003e 命令中，无论此设置如何，始终使用用户指定的值。）\u003c/p\u003e\u003cp\u003e在 \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e 命令中，如果选择了 \u003ccode class=\"literal\"\u003eALWAYS\u003c/code\u003e，则把该列更新为除 \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e 之外的任何值都会被拒绝。如果选择了 \u003ccode class=\"literal\"\u003eBY DEFAULT\u003c/code\u003e，则该列可以正常更新。（\u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e 命令没有 \u003ccode class=\"literal\"\u003eOVERRIDING\u003c/code\u003e 子句。）\u003c/p\u003e\u003cp\u003e可选的 \u003cem class=\"replaceable\"\u003e\u003ccode\u003esequence_options\u003c/code\u003e\u003c/em\u003e 子句可用于覆盖序列参数。可用选项包括\u003ca href=\"/docs/18/sql-createsequence.html\" title=\"CREATE SEQUENCE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SEQUENCE\u003c/span\u003e\u003c/a\u003e中展示的选项，另外还包括 \u003ccode class=\"literal\"\u003eSEQUENCE NAME \u003cem class=\"replaceable\"\u003e\u003ccode\u003ename\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e、\u003ccode class=\"literal\"\u003eLOGGED\u003c/code\u003e 与 \u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e，用于选择序列名称及持久化级别。不指定 \u003ccode class=\"literal\"\u003eSEQUENCE NAME\u003c/code\u003e 时，系统会为该序列选择一个未使用名称。不指定 \u003ccode class=\"literal\"\u003eLOGGED\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e 时，序列将与表具有相同的持久化级别。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNIQUE [ NULLS [ NOT ] DISTINCT ]\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e \u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNIQUE [ 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 ] )\u003c/code\u003e [ \u003cspan class=\"optional\"\u003e\u003ccode class=\"literal\"\u003eINCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ]（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 约束指定表中一列或多列组成的一组只能包含唯一值。表级唯一约束的行为与列级唯一约束相同，只是它还能跨越多列。因此，该约束要求任意两行在这些列中至少有一列不同。\u003c/p\u003e\u003cp\u003e如果为最后一列指定了 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 选项，则该列检查的是是否重叠，而不是是否相等。在这种情况下，只要重复值在 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 列上不发生重叠，约束中的其他列就允许重复。（如果该列是日期或时间戳范围，这有时被称为时态键；但 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 允许在任意基础类型上定义范围。）实际上，这种约束是通过 \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束而不是 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 约束来强制的。例如，\u003ccode class=\"literal\"\u003eUNIQUE (id, valid_at WITHOUT OVERLAPS)\u003c/code\u003e 的行为类似于 \u003ccode class=\"literal\"\u003eEXCLUDE USING GIST (id WITH =, valid_at WITH \u0026amp;\u0026amp;)\u003c/code\u003e。\u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 列必须是范围类型或多范围类型，且不允许为空范围或空多范围。该约束中不带 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 的列可以是任何可在 GiST 索引中按相等性比较的类型。默认仅支持范围类型，但可通过安装\u003ca href=\"/docs/18/btree-gist.html\" title=\"F.8. btree_gist — 具有 B-树行为的 GiST 操作符类\"\u003ebtree_gist\u003c/a\u003e扩展来使用其他类型（这也是该特性的预期用法）。\u003c/p\u003e\u003cp\u003e就唯一约束而言，空值不被视为相等，除非指定了 \u003ccode class=\"literal\"\u003eNULLS NOT DISTINCT\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e每个唯一约束都应引用一组列，这组列应不同于该表上任何其他唯一约束或主键约束所引用的列集合。（否则，冗余的唯一约束将被丢弃。）\u003c/p\u003e\u003cp\u003e在为多级分区层次结构建立唯一约束时，目标分区表分区键中的所有列，以及其所有后代分区表分区键中的所有列，都必须包含在约束定义中。\u003c/p\u003e\u003cp\u003e添加唯一约束会自动在约束所用的列或列组上创建唯一 B-树索引；但如果该约束包含 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 子句，则会使用 GiST 索引。所创建的索引与该唯一约束同名。\u003c/p\u003e\u003cp\u003e可选的 \u003ccode class=\"literal\"\u003eINCLUDE\u003c/code\u003e 子句会向该索引添加一个或多个仅作为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e载荷\u003c/span\u003e”\u003c/span\u003e的列：对这些列不强制唯一性，而且该索引也不能据此进行搜索。不过，它们可以通过仅索引扫描取回。注意，尽管约束不在内含列上强制执行，它仍然依赖于这些列。因此，对这类列执行某些操作（例如 \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e）可能导致约束和索引被级联删除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRIMARY 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 ] )\u003c/code\u003e [ \u003cspan class=\"optional\"\u003e\u003ccode class=\"literal\"\u003eINCLUDE ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ...])\u003c/code\u003e\u003c/span\u003e ]（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 约束指定表的一列或多列只能包含唯一（不重复）且非空的值。无论作为列约束还是表约束，一个表都只能指定一个主键。\u003c/p\u003e\u003cp\u003e主键约束所引用的列集合应不同于同一表上定义的任何唯一约束所引用的列集合。（否则，该唯一约束是冗余的，会被丢弃。）\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 强制的数据约束与 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 的组合相同。不过，将一组列标识为主键还会为模式设计提供元数据，因为主键意味着其他表可以将这组列作为行的唯一标识符来依赖。\u003c/p\u003e\u003cp\u003e放在分区表上时，\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 约束与前面描述的 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 约束有相同的限制。\u003c/p\u003e\u003cp\u003e添加 \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 约束会自动在约束所用的列或列组上创建唯一 B-树索引；如果指定了 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e，则会创建 GiST 索引。\u003c/p\u003e\u003cp\u003e可选的 \u003ccode class=\"literal\"\u003eINCLUDE\u003c/code\u003e 子句会向该索引添加一个或多个仅作为\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e载荷\u003c/span\u003e”\u003c/span\u003e的列：对这些列不强制唯一性，而且该索引也不能据此进行搜索。不过，它们可以通过仅索引扫描取回。注意，尽管约束不在内含列上强制执行，它仍然依赖于这些列。因此，对这类列执行某些操作（例如 \u003ccode class=\"literal\"\u003eDROP COLUMN\u003c/code\u003e）可能导致约束和索引被级联删除。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eEXCLUDE [ 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 ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 子句定义一个排他约束。它保证如果任意两行在指定列或表达式上使用指定操作符进行比较，这些比较不会全部返回 \u003ccode class=\"literal\"\u003eTRUE\u003c/code\u003e。如果所有指定操作符都测试相等，这就等价于 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 约束，尽管普通唯一约束会更快。不过，排他约束可以指定比简单相等更一般的约束。例如，你可以通过使用 \u003ccode class=\"literal\"\u003e\u0026amp;\u0026amp;\u003c/code\u003e 操作符来指定一个约束，使表中不存在两个包含重叠圆的行（见\u003ca href=\"/docs/18/datatype-geometric.html\" title=\"8.8. 几何类型\"\u003e第 8.8 节\u003c/a\u003e）。这些操作符必须是可交换的。\u003c/p\u003e\u003cp\u003e排他约束通过一个与约束同名的索引来实现，因此每个指定操作符都必须与索引访问方法 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eindex_method\u003c/code\u003e\u003c/em\u003e 的适当操作符类关联（见\u003ca href=\"/docs/18/indexes-opclass.html\" title=\"11.10. 操作符类和操作符族\"\u003e第 11.10 节\u003c/a\u003e）。每个 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexclude_element\u003c/code\u003e\u003c/em\u003e 都定义了索引的一列，因此它可以可选地指定排序规则、操作符类、操作符类参数和/或排序选项；这些在\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e中有完整说明。\u003c/p\u003e\u003cp\u003e访问方法必须支持 \u003ccode class=\"literal\"\u003eamgettuple\u003c/code\u003e（见\u003ca href=\"/docs/18/indexam.html\" title=\"第 63 章 索引访问方法接口定义\"\u003e第 63 章\u003c/a\u003e）；目前这意味着不能使用 \u003cacronym\u003eGIN\u003c/acronym\u003e。虽然允许，但在排他约束上使用 B-树或 hash 索引意义不大，因为它们做不到比普通唯一约束更好的事情。因此，实践中访问方法总是 \u003cacronym\u003eGiST\u003c/acronym\u003e 或 \u003cacronym\u003eSP-GiST\u003c/acronym\u003e。\u003c/p\u003e\u003cp\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003epredicate\u003c/code\u003e\u003c/em\u003e 允许你只在表的一个子集上指定排他约束；在内部，这会创建一个部分索引。注意，谓词周围的圆括号是必需的。\u003c/p\u003e\u003cp\u003e在为多级分区层次结构建立排他约束时，目标分区表分区键中的所有列，以及其所有后代分区表分区键中的所有列，都必须包含在约束定义中。此外，这些列必须使用等值操作符进行比较。这些限制保证潜在冲突的行会落在同一分区中。约束也可以引用不属于任何分区键的其他列，这些列可以使用任何合适的操作符来比较。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eREFERENCES \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 \u003cem class=\"replaceable\"\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ 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 ]\u003c/code\u003e（列约束）\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eFOREIGN 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 ] ) ] [ MATCH \u003cem class=\"replaceable\"\u003e\u003ccode\u003ematchtype\u003c/code\u003e\u003c/em\u003e ] [ 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 ]\u003c/code\u003e（表约束）\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这些子句指定外键约束，要求新表的一列或多列组成的一组只能包含与被引用表某一行的被引用列中值相匹配的值。若省略 \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 列表，则使用 \u003cem class=\"replaceable\"\u003e\u003ccode\u003ereftable\u003c/code\u003e\u003c/em\u003e 的主键。否则，\u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 列表必须引用某个不可延迟的唯一约束或主键约束的列，或者引用某个非部分唯一索引的列。\u003c/p\u003e\u003cp\u003e如果最后一列标记为 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e，则会按特殊方式处理。非 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e 列按相等性比较（并且至少需要一列），而 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e 列则不会。取而代之的是，只有当被引用表中基于键的非 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e 部分相匹配的记录，其合并后的 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e 值能够完整覆盖引用记录的 PERIOD 值时，约束才算满足。换言之，该引用在其整个持续区间内都必须有对应的被引用对象。该列必须是范围类型或多范围类型。此外，被引用表必须具有用 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e 声明的主键或唯一约束。最后，如果外键包含 PERIOD \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e 说明，则对应的 \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e（如果存在）也必须标记为 \u003ccode class=\"literal\"\u003ePERIOD\u003c/code\u003e。如果省略 \u003cem class=\"replaceable\"\u003e\u003ccode\u003erefcolumn\u003c/code\u003e\u003c/em\u003e 子句，从而选择 reftable 的主键约束，则该主键的最后一列必须标记为 \u003ccode class=\"literal\"\u003eWITHOUT OVERLAPS\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e对于每一对引用列和被引用列，如果它们属于支持排序规则的数据类型，则两者的排序规则要么都必须是确定性的，要么必须完全相同。这样可以确保两列对“相等”具有一致的定义。\u003c/p\u003e\u003cp\u003e用户必须对被引用表拥有 \u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e 权限（对整张表，或对特定的被引用列）。添加外键约束需要在被引用表上获取 \u003ccode class=\"literal\"\u003eSHARE ROW EXCLUSIVE\u003c/code\u003e 锁。注意，不能在临时表和永久表之间定义外键约束。\u003c/p\u003e\u003cp\u003e插入到引用列中的值会按照给定的匹配类型，与被引用表及其被引用列中的值进行匹配。共有三种匹配类型：\u003ccode class=\"literal\"\u003eMATCH FULL\u003c/code\u003e、\u003ccode class=\"literal\"\u003eMATCH PARTIAL\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eMATCH SIMPLE\u003c/code\u003e（默认值）。\u003ccode class=\"literal\"\u003eMATCH FULL\u003c/code\u003e 不允许多列外键中的某一列为空，除非所有外键列都为空；如果它们都为空，则不要求该行在被引用表中有匹配行。\u003ccode class=\"literal\"\u003eMATCH SIMPLE\u003c/code\u003e 允许任意外键列为空；如果其中任何一列为空，则不要求该行在被引用表中有匹配行。\u003ccode class=\"literal\"\u003eMATCH PARTIAL\u003c/code\u003e 目前尚未实现。（当然，可以对引用列应用 \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 约束，以防止出现这些情况。）\u003c/p\u003e\u003cp\u003e此外，当被引用列中的数据发生变化时，会对本表列中的数据执行某些操作。\u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e 子句指定当被引用表中的被引用行被删除时要执行的操作。同样，\u003ccode class=\"literal\"\u003eON UPDATE\u003c/code\u003e 子句指定当被引用表中的被引用列被更新为新值时要执行的操作。如果更新了行，但被引用列实际上并未改变，则不执行任何操作。引用动作作为数据更改命令的一部分执行，即使该约束是延迟的也一样。每个子句可用的操作如下：\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNO ACTION\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\"\u003eRESTRICT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果待删除或更新的行在引用表中有匹配行，则产生错误。即使操作完成后的状态并不违反外键约束，也会阻止该操作。特别地，它会阻止将被引用行更新为“值不同但比较相等”的情况。（但不会阻止把列更新为相同值的\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e无操作\u003c/span\u003e”\u003c/span\u003e更新。）\u003c/p\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分别删除任何引用已删除行的行，或将引用列的值更新为被引用列的新值。\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET NULL [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将全部引用列，或指定的引用列子集，设置为空值。只有 \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e 动作才能指定列子集。\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eSET DEFAULT [ ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将全部引用列，或指定的引用列子集，设置为其默认值。只有 \u003ccode class=\"literal\"\u003eON DELETE\u003c/code\u003e 动作才能指定列子集。（如果默认值不是空值，则被引用表中必须有一行与这些默认值匹配，否则该操作会失败。）\u003c/p\u003e\u003cp\u003e对于时态外键，该选项不受支持。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e如果被引用列经常变化，可以考虑在引用列上添加索引，使与外键约束关联的引用操作能够更高效地执行。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDEFERRABLE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT DEFERRABLE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句控制约束是否可延迟。不可延迟的约束会在每条命令之后立即检查。可延迟约束的检查可以推迟到事务结束时（使用 \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\"\u003e\u003ccode class=\"command\"\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e 命令）。\u003ccode class=\"literal\"\u003eNOT DEFERRABLE\u003c/code\u003e 是默认值。当前，只有 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e、\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eREFERENCES\u003c/code\u003e（外键）约束接受该子句。\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束不可延迟。注意在包含 \u003ccode class=\"literal\"\u003eON CONFLICT\u003c/code\u003e 子句的 \u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e 语句中，可延迟约束不能用作冲突仲裁器。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eINITIALLY DEFERRED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果约束可延迟，该子句指定默认的约束检查时间。如果约束是 \u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e，则会在每条语句之后检查。这是默认值。如果约束是 \u003ccode class=\"literal\"\u003eINITIALLY DEFERRED\u003c/code\u003e，则只会在事务结束时检查。约束检查时间可以用 \u003ca href=\"/docs/18/sql-set-constraints.html\" title=\"SET CONSTRAINTS\"\u003e\u003ccode class=\"command\"\u003eSET CONSTRAINTS\u003c/code\u003e\u003c/a\u003e 命令修改。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eENFORCED\u003c/code\u003e\u003cbr\u003e\u003c/span\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e当约束为 \u003ccode class=\"literal\"\u003eENFORCED\u003c/code\u003e 时，数据库系统会在适当时机检查该约束（按需要在每条语句后或事务结束时），以确保约束成立。这是默认行为。若约束为 \u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e，数据库系统将不再检查该约束，改由应用代码保证约束成立。对于不影响结果正确性的优化决策，数据库系统仍可能假定数据满足该约束。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eNOT ENFORCED\u003c/code\u003e 约束可作为文档用途，适用于运行时检查约束代价过高的场景。\u003c/p\u003e\u003cp\u003e当前该能力仅支持外键约束与 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 约束。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该可选子句指定存储新表内容所用的表访问方法；该方法必须是类型为 \u003ccode class=\"literal\"\u003eTABLE\u003c/code\u003e 的访问方法。详见\u003ca href=\"/docs/18/tableam.html\" title=\"第 62 章 表访问方法接口定义\"\u003e第 62 章\u003c/a\u003e。如果未指定此选项，则会为新表选择默认表访问方法。详见\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e创建分区时，若其分区表已设置表访问方法，则该分区会使用分区表的访问方法。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITH ( \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#SQL-CREATETABLE-STORAGE-PARAMETERS\" title=\"存储参数\"\u003eStorage Parameters\u003c/a\u003e。为了向后兼容，表的 \u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e 子句还可以包含 \u003ccode class=\"literal\"\u003eOIDS=FALSE\u003c/code\u003e，以指定新表的行不应包含 OID（对象标识符）；\u003ccode class=\"literal\"\u003eOIDS=TRUE\u003c/code\u003e 已不再受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eWITHOUT OIDS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e这是声明表 \u003ccode class=\"literal\"\u003eWITHOUT OIDS\u003c/code\u003e 的向后兼容语法；创建 \u003ccode class=\"literal\"\u003eWITH OIDS\u003c/code\u003e 表已不再受支持。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e可以使用 \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e 控制临时表在事务块结束时的行为。三种选项如下：\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003ePRESERVE ROWS\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\"\u003eDELETE ROWS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e在每个事务块结束时删除临时表中的所有行。本质上，相当于在每次提交时自动执行一次 \u003ca href=\"/docs/18/sql-truncate.html\" title=\"TRUNCATE\"\u003e\u003ccode class=\"command\"\u003eTRUNCATE\u003c/code\u003e\u003c/a\u003e。用于分区表时，此操作不会级联到其分区。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eDROP\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\"\u003eTABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e 是新表要创建于其中的表空间名称。若未指定，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\"\u003edefault_tablespace\u003c/a\u003e；如果该表是临时表，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\"\u003etemp_tablespaces\u003c/a\u003e。对于分区表，由于表本身不需要存储，所指定的表空间会覆盖 \u003ccode class=\"literal\"\u003edefault_tablespace\u003c/code\u003e，成为在未显式指定其他表空间时新建分区所使用的默认表空间。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUSING INDEX TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该子句允许选择与 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束相关联的索引要创建在哪个表空间中。若未指定，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLESPACE\"\u003edefault_tablespace\u003c/a\u003e；如果该表是临时表，则参考\u003ca href=\"/docs/18/runtime-config-client.html#GUC-TEMP-TABLESPACES\"\u003etemp_tablespaces\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e存储参数\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eWITH\u003c/code\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=\"firstterm\"\u003e存储参数\u003c/em\u003e。索引的存储参数已在\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e中介绍。当前可用于表的存储参数列在下文。对于其中许多参数，如下所示，还存在一个同名且带 \u003ccode class=\"literal\"\u003etoast.\u003c/code\u003e 前缀的附加参数，用于控制该表的二级 \u003cacronym\u003eTOAST\u003c/acronym\u003e 表（如果有）的行为（更多有关 TOAST 的信息见\u003ca href=\"/docs/18/storage-toast.html\" title=\"66.2. TOAST\"\u003e第 66.2 节\u003c/a\u003e）。如果设置了表参数值，而对应的 \u003ccode class=\"literal\"\u003etoast.\u003c/code\u003e 参数未设置，则 TOAST 表将使用该表的参数值。不支持为分区表指定这些参数，但可以为单独的叶子分区指定。\u003c/p\u003e\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"varname\"\u003efillfactor\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e表的填充因子是介于 10 和 100 之间的百分比。100（完全填充）是默认值。当指定较小的填充因子时，\u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e 操作只会把表页填充到指定百分比；每页剩余的空间则预留给更新该页上的行。这使得 \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e 有机会将更新后的行副本放在与原始行相同的页上，这比放在不同页上更高效，也更有可能发生\u003ca href=\"/docs/18/storage-hot.html\" title=\"66.7. 堆内元组（HOT）\"\u003e堆内元组更新\u003c/a\u003e。对于条目永远不会更新的表，完全填充是最佳选择；但对频繁更新的表来说，较小的填充因子更合适。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003etoast_tuple_target\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003etoast_tuple_target 指定在尝试压缩和/或将长列值移入 TOAST 表之前所需的最小元组长度，同时也是一旦开始 TOAST 化后试图将长度缩减到其以下的目标长度。这会影响标记为 External（用于移动）、Main（用于压缩）或 Extended（用于两者）的列，并且只适用于新元组。对现有行没有影响。默认情况下，该参数被设置为至少允许每个块容纳 4 个元组，在默认块大小下即为 2040 字节。有效值介于 128 字节与（块大小减去页头）之间，默认上限为 8160 字节。对非常短或非常长的行，修改该值可能没有帮助。注意，默认设置通常已经接近最佳，某些情况下设置该参数可能产生负面影响。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eparallel_workers\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e该参数设置在对该表执行并行扫描时用于辅助扫描的工作进程数量。若未设置，系统会根据关系大小决定一个值。规划器或使用并行扫描的实用命令实际选择的工作进程数量可能更少，例如可能受\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES\"\u003emax_worker_processes\u003c/a\u003e设置所限。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_enabled\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_enabled\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e为特定表启用或禁用自动清理守护进程。如果为真，自动清理守护进程将按照\u003ca href=\"/docs/18/routine-vacuuming.html#AUTOVACUUM\" title=\"24.1.6. 自动清理守护进程\"\u003e第 24.1.6 节\u003c/a\u003e中讨论的规则，在该表上执行自动 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 和/或 \u003ccode class=\"command\"\u003eANALYZE\u003c/code\u003e 操作。如果为假，则该表不会被自动清理，但为了防止事务 ID 回卷，仍可能对其执行自动清理。有关回卷防护的更多信息，见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. 防止事务 ID 回卷失败\"\u003e第 24.1.5 节\u003c/a\u003e。注意，如果\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM\"\u003eautovacuum\u003c/a\u003e参数为假，则自动清理守护进程根本不会运行（防止事务 ID 回卷的情况除外）；为单独表设置存储参数也不会覆盖这一点。因此，显式将此存储参数设为 \u003ccode class=\"literal\"\u003etrue\u003c/code\u003e 往往意义不大，设为 \u003ccode class=\"literal\"\u003efalse\u003c/code\u003e 才更有用。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_index_cleanup\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_index_cleanup\u003c/code\u003e (\u003ccode class=\"type\"\u003eenum\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e强制或禁用在该表上执行 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 时的索引清理。默认值为 \u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e。设为 \u003ccode class=\"literal\"\u003eOFF\u003c/code\u003e 时禁用索引清理，设为 \u003ccode class=\"literal\"\u003eON\u003c/code\u003e 时启用，设为 \u003ccode class=\"literal\"\u003eAUTO\u003c/code\u003e 时，则每次运行 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 都会动态决定。动态行为允许 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e 避免为了移除很少的死元组而无谓地扫描索引。强制禁用全部索引清理可以显著加快 \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e，但如果表修改频繁，也可能导致索引严重膨胀。若在 \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e\u003c/a\u003e 中指定了 \u003ccode class=\"literal\"\u003eINDEX_CLEANUP\u003c/code\u003e 参数，则会覆盖此选项的值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_truncate\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_truncate\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-TRUNCATE\"\u003evacuum_truncate\u003c/a\u003e参数的每表取值。若 \u003ca href=\"/docs/18/sql-vacuum.html\" title=\"VACUUM\"\u003e\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e\u003c/a\u003e 指定了 \u003ccode class=\"literal\"\u003eTRUNCATE\u003c/code\u003e 参数，将覆盖此选项的值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-THRESHOLD\"\u003eautovacuum_vacuum_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_max_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_max_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-MAX-THRESHOLD\"\u003eautovacuum_vacuum_max_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_scale_factor\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-SCALE-FACTOR\"\u003eautovacuum_vacuum_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_insert_threshold\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_insert_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-THRESHOLD\"\u003eautovacuum_vacuum_insert_threshold\u003c/a\u003e参数的每表取值。特殊值 -1 可用于禁用该表上的由插入触发的清理。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_insert_scale_factor\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_insert_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-INSERT-SCALE-FACTOR\"\u003eautovacuum_vacuum_insert_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_analyze_threshold\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-THRESHOLD\"\u003eautovacuum_analyze_threshold\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_analyze_scale_factor\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-ANALYZE-SCALE-FACTOR\"\u003eautovacuum_analyze_scale_factor\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_cost_delay\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_cost_delay\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-DELAY\"\u003eautovacuum_vacuum_cost_delay\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_vacuum_cost_limit\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_vacuum_cost_limit\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-LIMIT\"\u003eautovacuum_vacuum_cost_limit\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_min_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_min_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-MIN-AGE\"\u003evacuum_freeze_min_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\"\u003eautovacuum_freeze_max_age\u003c/a\u003e设置一半的每表 \u003ccode class=\"literal\"\u003eautovacuum_freeze_min_age\u003c/code\u003e 参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_max_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_max_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE\"\u003eautovacuum_freeze_max_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围设置的每表 \u003ccode class=\"literal\"\u003eautovacuum_freeze_max_age\u003c/code\u003e 参数（它只能设置得更小）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_freeze_table_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_freeze_table_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-FREEZE-TABLE-AGE\"\u003evacuum_freeze_table_age\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_min_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-MIN-AGE\"\u003evacuum_multixact_freeze_min_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e设置一半的每表 \u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_min_age\u003c/code\u003e 参数。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_max_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-AUTOVACUUM-MULTIXACT-FREEZE-MAX-AGE\"\u003eautovacuum_multixact_freeze_max_age\u003c/a\u003e参数的每表取值。注意，自动清理会忽略大于系统范围设置的每表 \u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_max_age\u003c/code\u003e 参数（它只能设置得更小）。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eautovacuum_multixact_freeze_table_age\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.autovacuum_multixact_freeze_table_age\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MULTIXACT-FREEZE-TABLE-AGE\"\u003evacuum_multixact_freeze_table_age\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003elog_autovacuum_min_duration\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.log_autovacuum_min_duration\u003c/code\u003e (\u003ccode class=\"type\"\u003einteger\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-logging.html#GUC-LOG-AUTOVACUUM-MIN-DURATION\"\u003elog_autovacuum_min_duration\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003evacuum_max_eager_freeze_failure_rate\u003c/code\u003e, \u003ccode class=\"literal\"\u003etoast.vacuum_max_eager_freeze_failure_rate\u003c/code\u003e (\u003ccode class=\"type\"\u003efloating point\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e\u003ca href=\"/docs/18/runtime-config-vacuum.html#GUC-VACUUM-MAX-EAGER-FREEZE-FAILURE-RATE\"\u003evacuum_max_eager_freeze_failure_rate\u003c/a\u003e参数的每表取值。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003euser_catalog_table\u003c/code\u003e (\u003ccode class=\"type\"\u003eboolean\u003c/code\u003e) \u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将该表声明为逻辑复制用途的附加目录表。详见\u003ca href=\"/docs/18/logicaldecoding-output-plugin.html#LOGICALDECODING-CAPABILITIES\" title=\"47.6.2. 能力\"\u003e第 47.6.2 节\u003c/a\u003e。不能为 TOAST 表设置此参数。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e为每一个唯一约束和主键约束自动创建一个索引来强制唯一性。因此，没有必要显式地为主键列创建一个索引（详见\u003ca href=\"/docs/18/sql-createindex.html\" title=\"CREATE INDEX\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE INDEX\u003c/span\u003e\u003c/a\u003e）。\u003c/p\u003e\u003cp\u003e在当前的实现中，唯一约束和主键不会被继承。这使得继承与唯一约束的组合相当不实用。\u003c/p\u003e\u003cp\u003e一个表不能有超过 1600 列（实际上，由于元组长度限制，有效的限制通常更低）。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e创建表\u003ccode class=\"structname\"\u003efilms\u003c/code\u003e和表\u003ccode class=\"structname\"\u003edistributors\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5) CONSTRAINT firstkey PRIMARY KEY,\n    title       varchar(40) NOT NULL,\n    did         integer NOT NULL,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute\n);\n\nCREATE TABLE distributors (\n     did    integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,\n     name   varchar(40) NOT NULL CHECK (name \u0026lt;\u0026gt; '')\n);\n\u003c/pre\u003e\u003cp\u003e创建一个带二维数组列的表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE array_int (\n    vector  int[][]\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode class=\"literal\"\u003efilms\u003c/code\u003e定义一个唯一表约束。唯一表约束可以定义在表的一列或多列上：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT production UNIQUE(date_prod)\n);\n\u003c/pre\u003e\u003cp\u003e定义一个列检查约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer CHECK (did \u0026gt; 100),\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003e定义一个表检查约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    CONSTRAINT con1 CHECK (did \u0026gt; 100 AND name \u0026lt;\u0026gt; '')\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode class=\"structname\"\u003efilms\u003c/code\u003e定义一个主键表约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE films (\n    code        char(5),\n    title       varchar(40),\n    did         integer,\n    date_prod   date,\n    kind        varchar(10),\n    len         interval hour to minute,\n    CONSTRAINT code_title PRIMARY KEY(code,title)\n);\n\u003c/pre\u003e\u003cp\u003e为表\u003ccode class=\"structname\"\u003edistributors\u003c/code\u003e定义一个主键约束。下面的两个示例是等价的，第一个使用表约束语法，第二个使用列约束语法：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    PRIMARY KEY(did)\n);\n\nCREATE TABLE distributors (\n    did     integer PRIMARY KEY,\n    name    varchar(40)\n);\n\u003c/pre\u003e\u003cp\u003e为列\u003ccode class=\"literal\"\u003ename\u003c/code\u003e指定一个字面常量默认值，将列\u003ccode class=\"literal\"\u003edid\u003c/code\u003e的默认值设为从某个序列对象中取下一个值，并让\u003ccode class=\"literal\"\u003emodtime\u003c/code\u003e的默认值为插入该行的时间：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    name      varchar(40) DEFAULT 'Luso Films',\n    did       integer DEFAULT nextval('distributors_serial'),\n    modtime   timestamp DEFAULT current_timestamp\n);\n\u003c/pre\u003e\u003cp\u003e在表\u003ccode class=\"classname\"\u003edistributors\u003c/code\u003e上定义两个\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e列约束，其中一个显式指定了名称：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer CONSTRAINT no_null NOT NULL,\n    name    varchar(40) NOT NULL\n);\n\u003c/pre\u003e\u003cp\u003e为\u003ccode class=\"literal\"\u003ename\u003c/code\u003e列定义一个唯一约束：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40) UNIQUE\n);\n\u003c/pre\u003e\u003cp\u003e同样的唯一约束用表约束指定：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name)\n);\n\u003c/pre\u003e\u003cp\u003e创建同样的表，并为该表及其唯一索引都指定 70% 的填充因子：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE distributors (\n    did     integer,\n    name    varchar(40),\n    UNIQUE(name) WITH (fillfactor=70)\n)\nWITH (fillfactor=70);\n\u003c/pre\u003e\u003cp\u003e创建表\u003ccode class=\"structname\"\u003ecircles\u003c/code\u003e，并添加一个排他约束以防任意两个圆重叠：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE circles (\n    c circle,\n    EXCLUDE USING gist (c WITH \u0026amp;\u0026amp;)\n);\n\u003c/pre\u003e\u003cp\u003e在表空间\u003ccode class=\"structname\"\u003ediskvol1\u003c/code\u003e中创建表\u003ccode class=\"structname\"\u003ecinemas\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cinemas (\n        id serial,\n        name text,\n        location text\n) TABLESPACE diskvol1;\n\u003c/pre\u003e\u003cp\u003e创建一个复合类型和一个类型化表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TYPE employee_type AS (name text, salary numeric);\n\nCREATE TABLE employees OF employee_type (\n    PRIMARY KEY (name),\n    salary WITH OPTIONS DEFAULT 1000\n);\n\u003c/pre\u003e\u003cp\u003e创建一个范围分区表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (logdate);\n\u003c/pre\u003e\u003cp\u003e创建一个在分区键中包含多个列的范围分区表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_year_month (\n    logdate         date not null,\n    peaktemp        int,\n    unitsales       int\n) PARTITION BY RANGE (EXTRACT(YEAR FROM logdate), EXTRACT(MONTH FROM logdate));\n\u003c/pre\u003e\u003cp\u003e创建列表分区表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities (\n    city_id      bigserial not null,\n    name         text not null,\n    population   bigint\n) PARTITION BY LIST (left(lower(name), 1));\n\u003c/pre\u003e\u003cp\u003e创建 hash 分区表：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE orders (\n    order_id     bigint not null,\n    cust_id      bigint not null,\n    status       text\n) PARTITION BY HASH (order_id);\n\u003c/pre\u003e\u003cp\u003e创建范围分区表的分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_y2016m07\n    PARTITION OF measurement (\n    unitsales DEFAULT 0\n) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01');\n\u003c/pre\u003e\u003cp\u003e使用分区键中的多个列创建范围分区表的几个分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE measurement_ym_older\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (MINVALUE, MINVALUE) TO (2016, 11);\n\nCREATE TABLE measurement_ym_y2016m11\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 11) TO (2016, 12);\n\nCREATE TABLE measurement_ym_y2016m12\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2016, 12) TO (2017, 01);\n\nCREATE TABLE measurement_ym_y2017m01\n    PARTITION OF measurement_year_month\n    FOR VALUES FROM (2017, 01) TO (2017, 02);\n\u003c/pre\u003e\u003cp\u003e创建列表分区表的分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN ('a', 'b');\n\u003c/pre\u003e\u003cp\u003e创建一个本身还要进一步分区的列表分区表分区，然后再向其添加一个分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_ab\n    PARTITION OF cities (\n    CONSTRAINT city_id_nonzero CHECK (city_id != 0)\n) FOR VALUES IN ('a', 'b') PARTITION BY RANGE (population);\n\nCREATE TABLE cities_ab_10000_to_100000\n    PARTITION OF cities_ab FOR VALUES FROM (10000) TO (100000);\n\u003c/pre\u003e\u003cp\u003e创建 hash 分区表的分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE orders_p1 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 0);\nCREATE TABLE orders_p2 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 1);\nCREATE TABLE orders_p3 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 2);\nCREATE TABLE orders_p4 PARTITION OF orders\n    FOR VALUES WITH (MODULUS 4, REMAINDER 3);\n\u003c/pre\u003e\u003cp\u003e创建默认分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE TABLE cities_partdef\n    PARTITION OF cities DEFAULT;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e 命令符合 \u003cacronym\u003eSQL\u003c/acronym\u003e 标准，但有下列例外。\u003c/p\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e临时表\u003c/h3\u003e\u003cp\u003e尽管 \u003ccode class=\"literal\"\u003eCREATE TEMPORARY TABLE\u003c/code\u003e 的语法看起来类似于 SQL 标准，但其效果并不相同。按标准，临时表只需定义一次，并会自动存在于每个需要它的会话中（内容初始为空）。而 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 要求每个会话都为每个要使用的临时表发出自己的 \u003ccode class=\"literal\"\u003eCREATE TEMPORARY TABLE\u003c/code\u003e 命令。这使不同会话可以出于不同目的使用相同的临时表名；而标准做法则要求给定临时表名的所有实例都必须具有相同的表结构。\u003c/p\u003e\u003cp\u003e标准对临时表行为的定义在实践中被广泛忽略。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 在这一点上的行为与多种其他 SQL 数据库相似。\u003c/p\u003e\u003cp\u003eSQL 标准还区分全局和局部临时表，其中局部临时表在每个会话内的每个 SQL 模块中都有独立的内容集合，但其定义仍在多个会话之间共享。由于 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 不支持 SQL 模块，这一区别在 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 中没有意义。\u003c/p\u003e\u003cp\u003e出于兼容性考虑，\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 接受在临时表声明中使用 \u003ccode class=\"literal\"\u003eGLOBAL\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e 关键字，但它们目前没有效果。不鼓励使用这些关键字，因为未来版本的 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 可能会采用更符合标准的解释。\u003c/p\u003e\u003cp\u003e临时表的 \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e 子句也与 SQL 标准相似，但存在一些差异。如果省略 \u003ccode class=\"literal\"\u003eON COMMIT\u003c/code\u003e 子句，SQL 规定默认行为是 \u003ccode class=\"literal\"\u003eON COMMIT DELETE ROWS\u003c/code\u003e。然而，\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 中的默认行为是 \u003ccode class=\"literal\"\u003eON COMMIT PRESERVE ROWS\u003c/code\u003e。SQL 中不存在 \u003ccode class=\"literal\"\u003eON COMMIT DROP\u003c/code\u003e 选项。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e非延迟唯一性约束\u003c/h3\u003e\u003cp\u003e当 \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 约束不可延迟时，只要有行被插入或修改，\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 就会立刻检查唯一性。SQL 标准规定应只在语句结束时强制唯一性；例如，当单个命令会更新多个键值时，这两者就会产生差异。若要获得符合标准的行为，应将约束声明为 \u003ccode class=\"literal\"\u003eDEFERRABLE\u003c/code\u003e 但不延迟（即 \u003ccode class=\"literal\"\u003eINITIALLY IMMEDIATE\u003c/code\u003e）。注意，这可能明显慢于立即检查唯一性。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e列检查约束\u003c/h3\u003e\u003cp\u003eSQL 标准规定，\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 列约束只能引用其所作用的列；只有 \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e 表约束才能引用多列。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 并不强制这一限制；它对列检查约束和表检查约束一视同仁。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束类型是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e外键约束\u003c/h3\u003e\u003cp\u003e在外键动作 \u003ccode class=\"literal\"\u003eSET DEFAULT\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eSET NULL\u003c/code\u003e 中指定列列表的能力，是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003cp\u003e外键约束可以引用唯一索引的列，而不仅限于主键或唯一约束的列，这也是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e约束\u003c/span\u003e”\u003c/span\u003e\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e约束\u003c/span\u003e”\u003c/span\u003e（实际上并不是约束）是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 对 SQL 标准的扩展；提供它是为了与其他一些数据库系统兼容（以及与 \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e 约束保持对称）。由于它本来就是任意列的默认情况，所以它的存在只是噪声。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e约束命名\u003c/h3\u003e\u003cp\u003eSQL 标准规定，在包含表或域的模式范围内，表约束和域约束都必须具有唯一名称。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 较为宽松：它只要求约束名称在附加到特定表或域的约束之间唯一。不过，对于基于索引的约束（\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e、\u003ccode class=\"literal\"\u003ePRIMARY KEY\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eEXCLUDE\u003c/code\u003e 约束）就没有这种额外自由，因为关联索引与约束同名，而索引名称在同一模式内的所有关系之间必须唯一。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e继承\u003c/h3\u003e\u003cp\u003e通过 \u003ccode class=\"literal\"\u003eINHERITS\u003c/code\u003e 子句实现的多重继承是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的语言扩展。SQL:1999 及后续标准使用不同的语法和语义定义了单继承。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 尚不支持 SQL:1999 风格的继承。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e零列的表\u003c/h3\u003e\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 允许创建没有列的表（例如 \u003ccode class=\"literal\"\u003eCREATE TABLE foo();\u003c/code\u003e）。这是对 SQL 标准的扩展，标准不允许零列的表。零列的表本身并不十分有用，但若禁止它们，就会让 \u003ccode class=\"command\"\u003eALTER TABLE DROP COLUMN\u003c/code\u003e 出现奇怪的特殊情况，因此忽略这一规范限制看起来更整洁。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e多个标识列\u003c/h3\u003e\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 允许一个表拥有多个标识列。该标准指定一个表最多只能有一个标识列。放宽这一限制主要是为了给模式更改或迁移提供更大的灵活性。请注意，\u003ccode class=\"command\"\u003eINSERT\u003c/code\u003e 命令仅支持一个适用于整个语句的覆盖子句，因此对行为不同的多个标识列支持并不好。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e生成列\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSTORED\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eVIRTUAL\u003c/code\u003e 选项都不是标准的一部分，但其他 SQL 实现也会使用。SQL 标准并不规定生成列的存储方式。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e虽然 SQL 标准中存在 \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e 子句，但 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 接受的许多 LIKE 选项并不在标准中，而标准中的某些选项又没有被 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 实现。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eWITH\u003c/code\u003e 子句是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展；存储参数不属于标准内容。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e表空间\u003c/h3\u003e\u003cp\u003e\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的表空间概念不是标准的一部分。因此，\u003ccode class=\"literal\"\u003eTABLESPACE\u003c/code\u003e 和 \u003ccode class=\"literal\"\u003eUSING INDEX TABLESPACE\u003c/code\u003e 子句都是扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e类型化表\u003c/h3\u003e\u003cp\u003e类型化表实现了 SQL 标准的一个子集。按照标准，类型化表除了具有与底层复合类型相对应的列之外，还应有一个额外的\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e自引用列\u003c/span\u003e”\u003c/span\u003e。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 不显式支持自引用列。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ePARTITION BY\u003c/code\u003e 子句是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e\u003cdiv class=\"refsect2\"\u003e\u003ch3\u003e\u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e 子句\u003c/h3\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e 子句是 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 的扩展。\u003c/p\u003e\u003c/div\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-table/?v=18\" title=\"ALTER TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-table/?v=18\" title=\"DROP TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-tablespace/?v=18\" title=\"CREATE TABLESPACE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLESPACE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-type/?v=18\" title=\"CREATE TYPE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TYPE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e ( [\n  { \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 [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecompression_method\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    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e\n    | LIKE \u003cem class=\"replaceable\"\u003e\u003ccode\u003esource_table\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003elike_option\u003c/code\u003e\u003c/em\u003e ... ] }\n    [, ... ]\n] )\n[ INHERITS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\n    OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etype_name\u003c/code\u003e\u003c/em\u003e [ (\n  { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ WITH OPTIONS ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e }\n    [, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e\n    PARTITION OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003eparent_table\u003c/code\u003e\u003c/em\u003e [ (\n  { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ WITH OPTIONS ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_constraint\u003c/code\u003e\u003c/em\u003e [ ... ] ]\n    | \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_constraint\u003c/code\u003e\u003c/em\u003e }\n    [, ... ]\n) ] { FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { \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 ] [, ... ] ) ]\n[ USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\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] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ 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\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\u003elike_option\u003c/code\u003e\u003c/em\u003e为：\u003c/span\u003e\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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\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":"CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nOF type_name [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\n\nCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ] { FOR VALUES partition_bound_spec | DEFAULT }\n[ PARTITION BY { RANGE | LIST | HASH } ( { column_name | ( expression ) } [ COLLATE collation ] [ opclass ] [, ... ] ) ]\n[ USING method ]\n[ WITH ( storage_parameter [= value] [, ... ] ) | WITHOUT OIDS ]\n[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]\n[ TABLESPACE tablespace_name ]\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而like_option为：\n\n{ INCLUDING | EXCLUDING } { COMMENTS | COMPRESSION | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }\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\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}
