{"Entry":{"collection":"sql","key":"create-foreign-table","name":"CREATE FOREIGN TABLE","aliases":["createforeigntable"],"metadata":{"aliases":["createforeigntable"],"changed_in":["9.2","9.3","9.5","10","11","12","18"],"changes":[{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"9.1"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters"],"removed":[]},"status":"changed","synopsis":{"added":["{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ NULL | NOT NULL ] }"],"removed":[]},"to":"9.2"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]","[ CONSTRAINT constraint_name ]","{ NOT NULL |","NULL |","DEFAULT default_expr }"],"removed":["{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ NULL | NOT NULL ] }"]},"to":"9.3"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":["notes"],"changed":["parameters","compatibility","see_also"],"removed":[]},"status":"changed","synopsis":{"added":["{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]","| table_constraint }","[ INHERITS ( parent_table [, ... ] ) ]","CHECK ( expression ) [ NO INHERIT ] |","[ CONSTRAINT constraint_name ]","CHECK ( expression ) [ NO INHERIT ]"],"removed":[]},"to":"9.5"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","examples"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name","PARTITION OF parent_table [ (","{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]","| table_constraint }","[, ... ]",") ]","FOR VALUES partition_bound_spec","SERVER server_name","[ OPTIONS ( option 'value' [, ... ] ) ]","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":[]},"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["{ FOR VALUES partition_bound_spec | DEFAULT }","TO ( { numeric_literal | string_literal | TRUE | FALSE | MINVALUE | MAXVALUE } [, ...] ) |","WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )"],"removed":[]},"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["DEFAULT default_expr |","GENERATED ALWAYS AS ( generation_expr ) STORED }","IN ( partition_bound_expr [, ...] ) |","FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )","TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |"],"removed":["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":"14","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"15"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["| LIKE source_table [ like_option ... ] }","{ NOT NULL [ NO INHERIT ] |","GENERATED ALWAYS AS ( generation_expr ) [ STORED | VIRTUAL ] }","[ ENFORCED | NOT ENFORCED ]","{  NOT NULL column_name [ NO INHERIT ] |","CHECK ( expression ) [ NO INHERIT ] }","[ ENFORCED | NOT ENFORCED ]","{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | ALL }"],"removed":[]},"to":"18"}],"content_hash":"3af86cab133f41ece47ae7377e5d1dd09994bf327cd1a736ec14dce64dbbfeb1","editorial":{},"first_version":"9.1","group":"foreign","imported_at":"2026-09-30T17:43:38.751541+08:00","last_version":"20","name":"CREATE FOREIGN TABLE","object":"FOREIGN TABLE","position":9004,"present_in":["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 foreign table","purpose_zh":"","related":["alter-foreign-table","drop-foreign-table","create-table","create-server","import-foreign-schema"],"slug":"create-foreign-table","source_rev":"a709ab85","synopsis":"CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n{ FOR VALUES partition_bound_spec | DEFAULT }\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\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 ] }\n[ ENFORCED | NOT ENFORCED ]\n\nand table_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL column_name [ NO INHERIT ] |\nCHECK ( expression ) [ NO INHERIT ] }\n[ ENFORCED | NOT ENFORCED ]\n\nand like_option is:\n\n{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-foreign-table","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-foreign-table","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATEFOREIGNTABLE","file":"sql-createforeigntable.html","lang":"en","name":"CREATE FOREIGN TABLE","purpose":"define a new foreign table","purpose_zh":"","related":["alter-foreign-table","drop-foreign-table","create-table","create-server","import-foreign-schema"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE FOREIGN TABLE\u003c/code\u003e creates a new foreign 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 FOREIGN TABLE myschema.mytable ...\u003c/code\u003e) then the table is created in the specified schema. Otherwise it is created in the current schema. The name of the foreign 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 FOREIGN TABLE\u003c/code\u003e also automatically creates a data type that represents the composite type corresponding to one row of the foreign table. Therefore, foreign tables cannot have the same name as any existing data type in the same schema.\u003c/p\u003e\u003cp\u003eIf \u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e clause is specified then the table is created as a partition of \u003ccode class=\"literal\"\u003eparent_table\u003c/code\u003e with specified bounds.\u003c/p\u003e\u003cp\u003eTo be able to create a foreign table, you must have \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on the foreign server, as well as \u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e privilege on all column types used in the table.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF 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\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\"\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 foreign table automatically inherits all columns. Parent tables can be plain tables or foreign tables. See the similar form of \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e for more details.\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\u003eThis form can be used to create the foreign table as partition of the given parent table with specified partition bound values. See the similar form of \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e for more details. Note that it is currently not allowed to create the foreign table as a partition of the parent table if there are \u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e indexes on the parent table. (See also \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003ccode class=\"command\"\u003eALTER TABLE ATTACH 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\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, constraints, 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 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 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 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 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\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\u003c/code\u003e [ NO INHERIT ]\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 each row in the foreign table is expected to satisfy; that is, the expression should produce TRUE or UNKNOWN, never FALSE, for all rows in the foreign table. 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. 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\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 (subqueries and cross-references to other columns in the current table are not allowed). 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. (The foreign-data wrapper will see it as a null value in new rows and may choose to store it as a null value or ignore it altogether.) When \u003ccode class=\"literal\"\u003eSTORED\u003c/code\u003e is specified, the column will be computed on write. (The computed value will be presented to the foreign-data wrapper for storage and must be returned on reading.) \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\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of an existing foreign server to use for the foreign table. For details on defining a server, see \u003ca href=\"/docs/18/sql-createserver.html\" title=\"CREATE SERVER\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\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\u003eOptions to be associated with the new foreign table or one of its columns. The allowed option names and values are specific to each foreign data wrapper and are validated using the foreign-data wrapper's validator function. Duplicate option names are not allowed (although it's OK for a table option and a column option to have the same name).\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eConstraints on foreign tables (such as \u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e or \u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e clauses) are not enforced by the core \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e system, and most foreign data wrappers do not attempt to enforce them either; that is, the constraint is simply assumed to hold true. There would be little point in such enforcement since it would only apply to rows inserted or updated via the foreign table, and not to rows modified by other means, such as directly on the remote server. Instead, a constraint attached to a foreign table should represent a constraint that is being enforced by the remote server.\u003c/p\u003e\u003cp\u003eSome special-purpose foreign data wrappers might be the only access mechanism for the data they access, and in that case it might be appropriate for the foreign data wrapper itself to perform constraint enforcement. But you should not assume that a wrapper does that unless its documentation says so.\u003c/p\u003e\u003cp\u003eAlthough \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e does not attempt to enforce constraints on foreign tables, it does assume that they are correct for purposes of query optimization. If there are rows visible in the foreign table that do not satisfy a declared constraint, queries on the table might produce errors or incorrect answers. It is the user's responsibility to ensure that the constraint definition matches reality.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eWhen a foreign table is used as a partition of a partitioned table, there is an implicit constraint that its contents must satisfy the partitioning rule. Again, it is the user's responsibility to ensure that that is true, which is best done by installing a matching constraint on the remote server.\u003c/p\u003e\u003c/div\u003e\u003cp\u003eWithin a partitioned table containing foreign-table partitions, an \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e that changes the partition key value can cause a row to be moved from a local partition to a foreign-table partition, provided the foreign data wrapper supports tuple routing. However, it is not currently possible to move a row from a foreign-table partition to another partition. An \u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e that would require doing that will fail due to the partitioning constraint, assuming that that is properly enforced by the remote server.\u003c/p\u003e\u003cp\u003eSimilar considerations apply to generated columns. Stored generated columns are computed on insert or update on the local \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e server and handed to the foreign-data wrapper for writing out to the foreign data store, but it is not enforced that a query of the foreign table returns values for stored generated columns that are consistent with the generation expression. Again, this might result in incorrect query results.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eCreate foreign table \u003ccode class=\"structname\"\u003efilms\u003c/code\u003e, which will be accessed through the server \u003ccode class=\"structname\"\u003efilm_server\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FOREIGN TABLE films (\n    code        char(5) NOT NULL,\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)\nSERVER film_server;\n\u003c/pre\u003e\u003cp\u003eCreate foreign table \u003ccode class=\"structname\"\u003emeasurement_y2016m07\u003c/code\u003e, which will be accessed through the server \u003ccode class=\"structname\"\u003eserver_07\u003c/code\u003e, as a partition of the range partitioned table \u003ccode class=\"structname\"\u003emeasurement\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FOREIGN TABLE measurement_y2016m07\n    PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')\n    SERVER server_07;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe \u003ccode class=\"command\"\u003eCREATE FOREIGN TABLE\u003c/code\u003e command largely conforms to the \u003cacronym\u003eSQL\u003c/acronym\u003e standard; however, much as with \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e, \u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e constraints and zero-column foreign tables are permitted. The ability to specify column default values is also a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension. Table inheritance, in the form defined by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e, is nonstandard. The \u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e clause, as supported in this command, is nonstandard.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-foreign-table/?v=18\" title=\"ALTER FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-foreign-table/?v=18\" title=\"DROP FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-server/?v=18\" title=\"CREATE SERVER\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/import-foreign-schema/?v=18\" title=\"IMPORT FOREIGN SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eIMPORT FOREIGN SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE FOREIGN 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 [ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\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  SERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\n[ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e' [, ... ] ) ]\n\nCREATE FOREIGN 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) ]\n{ FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n  SERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\n[ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\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[ 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{  NOT NULL \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ NO INHERIT ] |\n   CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] }\n[ 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 | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )","synopsis_text":"CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n{ FOR VALUES partition_bound_spec | DEFAULT }\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\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 ] }\n[ ENFORCED | NOT ENFORCED ]\n\nand table_constraint is:\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL column_name [ NO INHERIT ] |\nCHECK ( expression ) [ NO INHERIT ] }\n[ ENFORCED | NOT ENFORCED ]\n\nand like_option is:\n\n{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-foreign-table","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE FOREIGN TABLE","Summary":"定义一个新外部表","BodyHTML":"\u003cpre\u003eCREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ OPTIONS ( option \u0026#39;value\u0026#39; [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\nSERVER server_name\n[ OPTIONS ( option \u0026#39;value\u0026#39; [, ... ] ) ]\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n{ FOR VALUES partition_bound_spec | DEFAULT }\nSERVER server_name\n[ OPTIONS ( option \u0026#39;value\u0026#39; [, ... ] ) ]\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 ] }\n[ ENFORCED | NOT ENFORCED ]\n\n而table_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL column_name [ NO INHERIT ] |\nCHECK ( expression ) [ NO INHERIT ] }\n[ ENFORCED | NOT ENFORCED ]\n\n而like_option为：\n\n{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE FOREIGN TABLE\u003c/code\u003e将在当前数据库中创建一个新的外部表。该表归发出该命令的用户所有。\u003c/p\u003e\u003cp\u003e如果给出了模式名（例如 \u003ccode\u003eCREATE FOREIGN TABLE myschema.mytable ...\u003c/code\u003e），则表将在指定模式中创建。否则，它将在当前模式中创建。外部表名必须与同一模式中任何其他关系（表、序列、索引、视图、物化视图或外部表）的名称不同。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eCREATE FOREIGN TABLE\u003c/code\u003e还会自动创建一种数据类型，用以表示与该外部表一行对应的复合类型。因此，外部表名不能与同一模式中任何已有数据类型同名。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode\u003ePARTITION OF\u003c/code\u003e子句，则该表会按指定边界创建为\u003ccode\u003eparent_table\u003c/code\u003e的一个分区。\u003c/p\u003e\u003cp\u003e要创建外部表，必须拥有该外部服务器上的\u003ccode\u003eUSAGE\u003c/code\u003e权限，以及表中所用所有列类型上的\u003ccode\u003eUSAGE\u003c/code\u003e权限。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eIF 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\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\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子句指定一个表列表，新外部表会自动继承这些表中的所有列。父表可以是普通表，也可以是外部表。详见 \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e的类似形式。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\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这种形式可用于将外部表创建为给定父表的一个分区，并为其指定分区边界值。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e的类似形式。注意，如果父表上存在\u003ccode\u003eUNIQUE\u003c/code\u003e索引，则目前不允许将外部表创建为该父表的分区。（另见 \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eALTER TABLE ATTACH 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 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会复制被复制列定义中的任何生成表达式。默认情况下，新列将是普通基础列。\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 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\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\u003c/code\u003e [ NO INHERIT ]\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表达式不能包含子查询，也不能引用当前行的列之外的变量。可以引用系统列\u003ccode\u003etableoid\u003c/code\u003e，但不能引用其他系统列。\u003c/p\u003e\u003cp\u003e标记为\u003ccode\u003eNO INHERIT\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\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于该外部表的现有外部服务器的名称。有关定义服务器的细节，参见\u003ca href=\"/docs/18/sql-createserver.html\" title=\"CREATE SERVER\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eOPTIONS ( \u003cem\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e \u0026#39;\u003cem\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e\u0026#39; [, ...] )\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/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e核心\u003cspan\u003ePostgreSQL\u003c/span\u003e系统不会强制执行外部表上的约束（例如\u003ccode\u003eCHECK\u003c/code\u003e或\u003ccode\u003eNOT NULL\u003c/code\u003e子句），而且大多数外部数据包装器也不会尝试强制执行它们；也就是说，这些约束只是被假定为真。因为这种强制执行只会适用于通过外部表插入或更新的行，而不会适用于通过其他方式修改的行，例如直接在远程服务器上修改的行，所以这样做意义不大。相反，附加到外部表上的约束应当表示由远程服务器强制执行的约束。\u003c/p\u003e\u003cp\u003e某些专用的外部数据包装器可能是其所访问数据的唯一访问机制，在这种情况下，由外部数据包装器自身执行约束检查也许是合适的。但除非其文档明确说明，否则不应假定某个包装器会这样做。\u003c/p\u003e\u003cp\u003e尽管\u003cspan\u003ePostgreSQL\u003c/span\u003e不会尝试强制执行外部表上的约束，但出于查询优化的目的，它会假定这些约束是正确的。如果外部表中存在不满足已声明约束的可见行，那么对该表的查询可能会产生错误或不正确的结果。确保约束定义符合实际情况是用户的责任。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e当外部表被用作分区表的一个分区时，会有一个隐含约束，即其内容必须满足分区规则。同样，确保这一点是用户的责任，最好的做法是在远程服务器上安装匹配的约束。\u003c/p\u003e\u003c/div\u003e\u003cp\u003e在包含外部表分区的分区表中，如果外部数据包装器支持元组路由，那么更改分区键值的\u003ccode\u003eUPDATE\u003c/code\u003e可能导致某一行从本地分区移动到外部表分区。然而，目前还不能将一行从外部表分区移动到另一个分区。需要这样做的\u003ccode\u003eUPDATE\u003c/code\u003e会因为分区约束而失败，前提是假定远程服务器已正确强制执行该约束。\u003c/p\u003e\u003cp\u003e对生成列也有类似的考虑。存储型生成列会在本地\u003cspan\u003ePostgreSQL\u003c/span\u003e服务器上于插入或更新时计算，并交给外部数据包装器写入外部数据存储，但并不会强制要求查询外部表时返回的存储型生成列值与生成表达式保持一致。这同样可能导致不正确的查询结果。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e创建通过服务器\u003ccode\u003efilm_server\u003c/code\u003e访问的外部表\u003ccode\u003efilms\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE FOREIGN TABLE films (\n    code        char(5) NOT NULL,\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)\nSERVER film_server;\n\u003c/pre\u003e\u003cp\u003e创建通过服务器\u003ccode\u003eserver_07\u003c/code\u003e访问的外部表\u003ccode\u003emeasurement_y2016m07\u003c/code\u003e，并将其作为范围分区表\u003ccode\u003emeasurement\u003c/code\u003e的一个分区：\u003c/p\u003e\u003cpre\u003eCREATE FOREIGN TABLE measurement_y2016m07\n    PARTITION OF measurement FOR VALUES FROM (\u0026#39;2016-07-01\u0026#39;) TO (\u0026#39;2016-08-01\u0026#39;)\n    SERVER server_07;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE FOREIGN TABLE\u003c/code\u003e命令基本符合\u003cacronym\u003eSQL\u003c/acronym\u003e标准；但是，与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e一样，它允许使用\u003ccode\u003eNULL\u003c/code\u003e约束，也允许零列外部表。指定列默认值的能力也是\u003cspan\u003ePostgreSQL\u003c/span\u003e的扩展。按\u003cspan\u003ePostgreSQL\u003c/span\u003e定义的形式，表继承是非标准的。该命令支持的\u003ccode\u003eLIKE\u003c/code\u003e子句也是非标准的。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/alter-foreign-table/?v=18\" title=\"ALTER FOREIGN TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-foreign-table/?v=18\" title=\"DROP FOREIGN TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-server/?v=18\" title=\"CREATE SERVER\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/import-foreign-schema/?v=18\" title=\"IMPORT FOREIGN SCHEMA\" rel=\"nofollow\"\u003e\u003cspan\u003eIMPORT FOREIGN SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"5f41c3f20fb4e2e65d2dd1d421e5f0c2442f689e6987473e3f2633d25b16a97b","Payload":{"purpose_zh":"定义一个新外部表","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE FOREIGN TABLE\u003c/code\u003e将在当前数据库中创建一个新的外部表。该表归发出该命令的用户所有。\u003c/p\u003e\u003cp\u003e如果给出了模式名（例如 \u003ccode class=\"literal\"\u003eCREATE FOREIGN TABLE myschema.mytable ...\u003c/code\u003e），则表将在指定模式中创建。否则，它将在当前模式中创建。外部表名必须与同一模式中任何其他关系（表、序列、索引、视图、物化视图或外部表）的名称不同。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE FOREIGN TABLE\u003c/code\u003e还会自动创建一种数据类型，用以表示与该外部表一行对应的复合类型。因此，外部表名不能与同一模式中任何已有数据类型同名。\u003c/p\u003e\u003cp\u003e如果指定了\u003ccode class=\"literal\"\u003ePARTITION OF\u003c/code\u003e子句，则该表会按指定边界创建为\u003ccode class=\"literal\"\u003eparent_table\u003c/code\u003e的一个分区。\u003c/p\u003e\u003cp\u003e要创建外部表，必须拥有该外部服务器上的\u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e权限，以及表中所用所有列类型上的\u003ccode class=\"literal\"\u003eUSAGE\u003c/code\u003e权限。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF 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\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\"\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子句指定一个表列表，新外部表会自动继承这些表中的所有列。父表可以是普通表，也可以是外部表。详见 \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e的类似形式。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\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这种形式可用于将外部表创建为给定父表的一个分区，并为其指定分区边界值。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e的类似形式。注意，如果父表上存在\u003ccode class=\"literal\"\u003eUNIQUE\u003c/code\u003e索引，则目前不允许将外部表创建为该父表的分区。（另见 \u003ca href=\"/docs/18/sql-altertable.html\" title=\"ALTER TABLE\"\u003e\u003ccode class=\"command\"\u003eALTER TABLE ATTACH 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 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会复制被复制列定义中的任何生成表达式。默认情况下，新列将是普通基础列。\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 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\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\u003c/code\u003e [ NO INHERIT ]\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表达式不能包含子查询，也不能引用当前行的列之外的变量。可以引用系统列\u003ccode class=\"literal\"\u003etableoid\u003c/code\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\"\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\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e用于该外部表的现有外部服务器的名称。有关定义服务器的细节，参见\u003ca href=\"/docs/18/sql-createserver.html\" title=\"CREATE SERVER\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eOPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\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要与新外部表或其某一列关联的选项。允许的选项名和值都取决于具体的外部数据包装器，并使用该外部数据包装器的验证器函数进行验证。不允许重复的选项名（不过表选项和列选项同名是允许的）。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e核心\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e系统不会强制执行外部表上的约束（例如\u003ccode class=\"literal\"\u003eCHECK\u003c/code\u003e或\u003ccode class=\"literal\"\u003eNOT NULL\u003c/code\u003e子句），而且大多数外部数据包装器也不会尝试强制执行它们；也就是说，这些约束只是被假定为真。因为这种强制执行只会适用于通过外部表插入或更新的行，而不会适用于通过其他方式修改的行，例如直接在远程服务器上修改的行，所以这样做意义不大。相反，附加到外部表上的约束应当表示由远程服务器强制执行的约束。\u003c/p\u003e\u003cp\u003e某些专用的外部数据包装器可能是其所访问数据的唯一访问机制，在这种情况下，由外部数据包装器自身执行约束检查也许是合适的。但除非其文档明确说明，否则不应假定某个包装器会这样做。\u003c/p\u003e\u003cp\u003e尽管\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e不会尝试强制执行外部表上的约束，但出于查询优化的目的，它会假定这些约束是正确的。如果外部表中存在不满足已声明约束的可见行，那么对该表的查询可能会产生错误或不正确的结果。确保约束定义符合实际情况是用户的责任。\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e当外部表被用作分区表的一个分区时，会有一个隐含约束，即其内容必须满足分区规则。同样，确保这一点是用户的责任，最好的做法是在远程服务器上安装匹配的约束。\u003c/p\u003e\u003c/div\u003e\u003cp\u003e在包含外部表分区的分区表中，如果外部数据包装器支持元组路由，那么更改分区键值的\u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e可能导致某一行从本地分区移动到外部表分区。然而，目前还不能将一行从外部表分区移动到另一个分区。需要这样做的\u003ccode class=\"command\"\u003eUPDATE\u003c/code\u003e会因为分区约束而失败，前提是假定远程服务器已正确强制执行该约束。\u003c/p\u003e\u003cp\u003e对生成列也有类似的考虑。存储型生成列会在本地\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e服务器上于插入或更新时计算，并交给外部数据包装器写入外部数据存储，但并不会强制要求查询外部表时返回的存储型生成列值与生成表达式保持一致。这同样可能导致不正确的查询结果。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e创建通过服务器\u003ccode class=\"structname\"\u003efilm_server\u003c/code\u003e访问的外部表\u003ccode class=\"structname\"\u003efilms\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FOREIGN TABLE films (\n    code        char(5) NOT NULL,\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)\nSERVER film_server;\n\u003c/pre\u003e\u003cp\u003e创建通过服务器\u003ccode class=\"structname\"\u003eserver_07\u003c/code\u003e访问的外部表\u003ccode class=\"structname\"\u003emeasurement_y2016m07\u003c/code\u003e，并将其作为范围分区表\u003ccode class=\"structname\"\u003emeasurement\u003c/code\u003e的一个分区：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE FOREIGN TABLE measurement_y2016m07\n    PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')\n    SERVER server_07;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE FOREIGN TABLE\u003c/code\u003e命令基本符合\u003cacronym\u003eSQL\u003c/acronym\u003e标准；但是，与\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e\u003c/a\u003e一样，它允许使用\u003ccode class=\"literal\"\u003eNULL\u003c/code\u003e约束，也允许零列外部表。指定列默认值的能力也是\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e的扩展。按\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e定义的形式，表继承是非标准的。该命令支持的\u003ccode class=\"literal\"\u003eLIKE\u003c/code\u003e子句也是非标准的。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-foreign-table/?v=18\" title=\"ALTER FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-foreign-table/?v=18\" title=\"DROP FOREIGN TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP FOREIGN TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-table/?v=18\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/create-server/?v=18\" title=\"CREATE SERVER\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE SERVER\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/import-foreign-schema/?v=18\" title=\"IMPORT FOREIGN SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eIMPORT FOREIGN SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE FOREIGN 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 [ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\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  SERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\n[ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\u003c/code\u003e\u003c/em\u003e' [, ... ] ) ]\n\nCREATE FOREIGN 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) ]\n{ FOR VALUES \u003cem class=\"replaceable\"\u003e\u003ccode\u003epartition_bound_spec\u003c/code\u003e\u003c/em\u003e | DEFAULT }\n  SERVER \u003cem class=\"replaceable\"\u003e\u003ccode\u003eserver_name\u003c/code\u003e\u003c/em\u003e\n[ OPTIONS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoption\u003c/code\u003e\u003c/em\u003e '\u003cem class=\"replaceable\"\u003e\u003ccode\u003evalue\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[ 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{  NOT NULL \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecolumn_name\u003c/code\u003e\u003c/em\u003e [ NO INHERIT ] |\n   CHECK ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e ) [ NO INHERIT ] }\n[ 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 | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )","synopsis_text":"CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [\n{ column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]\n| table_constraint\n| LIKE source_table [ like_option ... ] }\n[, ... ]\n] )\n[ INHERITS ( parent_table [, ... ] ) ]\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\n\nCREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name\nPARTITION OF parent_table [ (\n{ column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ]\n| table_constraint }\n[, ... ]\n) ]\n{ FOR VALUES partition_bound_spec | DEFAULT }\nSERVER server_name\n[ OPTIONS ( option 'value' [, ... ] ) ]\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 ] }\n[ ENFORCED | NOT ENFORCED ]\n\n而table_constraint为：\n\n[ CONSTRAINT constraint_name ]\n{ NOT NULL column_name [ NO INHERIT ] |\nCHECK ( expression ) [ NO INHERIT ] }\n[ ENFORCED | NOT ENFORCED ]\n\n而like_option为：\n\n{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | STATISTICS | 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 )"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
