create_table.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 719 行 · 第 1/2 页

7
719
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE TABLE" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE TABLE \- define a new table.SH SYNOPSIS.sp.nfCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE \fItable_name\fR ( [  { \fIcolumn_name\fR \fIdata_type\fR [ DEFAULT \fIdefault_expr\fR ] [ \fIcolumn_constraint\fR [ ... ] ]    | \fItable_constraint\fR    | LIKE \fIparent_table\fR [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS } ] ... }    [, ... ]] )[ INHERITS ( \fIparent_table\fR [, ... ] ) ][ WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, ... ] ) | WITH OIDS | WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ][ TABLESPACE \fItablespace\fR ]where \fIcolumn_constraint\fR is:[ CONSTRAINT \fIconstraint_name\fR ]{ NOT NULL |   NULL |   UNIQUE \fIindex_parameters\fR |  PRIMARY KEY \fIindex_parameters\fR |  CHECK ( \fIexpression\fR ) |  REFERENCES \fIreftable\fR [ ( \fIrefcolumn\fR ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]    [ ON DELETE \fIaction\fR ] [ ON UPDATE \fIaction\fR ] }[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]and \fItable_constraint\fR is:[ CONSTRAINT \fIconstraint_name\fR ]{ UNIQUE ( \fIcolumn_name\fR [, ... ] ) \fIindex_parameters\fR |  PRIMARY KEY ( \fIcolumn_name\fR [, ... ] ) \fIindex_parameters\fR |  CHECK ( \fIexpression\fR ) |  FOREIGN KEY ( \fIcolumn_name\fR [, ... ] ) REFERENCES \fIreftable\fR [ ( \fIrefcolumn\fR [, ... ] ) ]    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE \fIaction\fR ] [ ON UPDATE \fIaction\fR ] }[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]\fIindex_parameters\fR in UNIQUE and PRIMARY KEY constraints are:[ WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, ... ] ) ][ USING INDEX TABLESPACE \fItablespace\fR ].sp.fi.SH "DESCRIPTION".PP\fBCREATE TABLE\fR will create a new, initially empty tablein the current database. The table will be owned by the user issuing thecommand..PPIf a schema name is given (for example, CREATE TABLEmyschema.mytable ...) then the table is created in the specifiedschema. Otherwise it is created in the current schema. Temporarytables exist in a special schema, so a schema name may not be givenwhen creating a temporary table. The name of the table must bedistinct from the name of any other table, sequence, index, or viewin the same schema..PP\fBCREATE TABLE\fR also automatically creates a datatype that represents the composite type correspondingto one row of the table. Therefore, tables cannot have the samename as any existing data type in the same schema..PPThe optional constraint clauses specify constraints (tests) thatnew or updated rows must satisfy for an insert or update operationto succeed. A constraint is an SQL object that helps define theset of valid values in the table in various ways..PPThere are two ways to define constraints: table constraints andcolumn constraints. A column constraint is defined as part of acolumn definition. A table constraint definition is not tied to aparticular 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 theconstraint only affects one column..SH "PARAMETERS".TP\fBTEMPORARY or TEMP\fRIf specified, the table is created as a temporary table.Temporary tables are automatically dropped at the end of asession, or optionally at the end of the current transaction(see ON COMMIT below). Existing permanenttables with the same name are not visible to the current sessionwhile the temporary table exists, unless they are referencedwith schema-qualified names. Any indexes created on a temporarytable are automatically temporary as well.Optionally, GLOBAL or LOCALcan be written before TEMPORARY or TEMP.This makes no difference in PostgreSQL, but seeCompatibility [\fBcreate_table\fR(7)]..TP\fB\fItable_name\fB\fRThe name (optionally schema-qualified) of the table to be created..TP\fB\fIcolumn_name\fB\fRThe name of a column to be created in the new table..TP\fB\fIdata_type\fB\fRThe data type of the column. This may include arrayspecifiers. For more information on the data types supported byPostgreSQL, refer to in the documentation..TP\fBDEFAULT\fRThe DEFAULT clause assigns a default data value forthe column whose column definition it appears within. The valueis any variable-free expression (subqueries and cross-referencesto other columns in the current table are not allowed). Thedata type of the default expression must match the data type of thecolumn.The default expression will be used in any insert operation thatdoes not specify a value for the column. If there is no defaultfor a column, then the default is null..TP\fBINHERITS ( \fIparent_table\fB [, ... ] )\fRThe optional INHERITS clause specifies a list oftables from which the new table automatically inherits allcolumns.Use of INHERITS creates a persistent relationshipbetween the new child table and its parent table(s). Schemamodifications to the parent(s) normally propagate to childrenas well, and by default the data of the child table is included inscans of the parent(s).If the same column name exists in more than one parenttable, an error is reported unless the data types of the columnsmatch in each of the parent tables. If there is no conflict,then the duplicate columns are merged to form a single column inthe new table. If the column name list of the new tablecontains a column name that is also inherited, the data type mustlikewise match the inherited column(s), and the columndefinitions are merged into one. However, inherited and newcolumn declarations of the same name need not specify identicalconstraints: all constraints provided from any declaration aremerged together and all are applied to the new table. If thenew table explicitly specifies a default value for the column,this default overrides any defaults from inherited declarationsof the column. Otherwise, any parents that specify defaultvalues for the column must all specify the same default, or anerror will be reported..TP\fBLIKE \fIparent_table\fB [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS } ]\fRThe LIKE clause specifies a table from whichthe new table automatically copies all column names, their data types,and their not-null constraints.Unlike INHERITS, the new table and original tableare completely decoupled after creation is complete. Changes to theoriginal table will not be applied to the new table, and it is notpossible to include data of the new table in scans of the originaltable.Default expressions for the copied column definitions will only becopied if INCLUDING DEFAULTS is specified. Thedefault behavior is to exclude default expressions, resulting in thecopied columns in the new table having null defaults.Not-null constraints are always copied to the new table.CHECK constraints will only be copied ifINCLUDING CONSTRAINTS is specified; other types ofconstraints will never be copied. Also, no distinction is made betweencolumn constraints and table constraints \(em when constraints arerequested, all check constraints are copied.Note also that unlike INHERITS, copied columns andconstraints are not merged with similarly named columns and constraints.If the same name is specified explicitly or in anotherLIKE clause an error is signalled..TP\fBCONSTRAINT \fIconstraint_name\fB\fRAn optional name for a column or table constraint. If theconstraint is violated, the constraint name is present in error messages,so constraint names like col must be positive can be usedto 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..TP\fBNOT NULL\fRThe column is not allowed to contain null values..TP\fBNULL\fRThe column is allowed to contain null values. This is the default.This clause is only provided for compatibility withnon-standard SQL databases. Its use is discouraged in newapplications..TP\fBUNIQUE (column constraint)\fR.TP\fBUNIQUE ( \fIcolumn_name\fB [, ... ] ) (table constraint)\fRThe UNIQUE constraint specifies that agroup of one or more columns of a table may containonly unique values. The behavior of the unique table constraintis the same as that for column constraints, with the additionalcapability to span multiple columns.For the purpose of a unique constraint, null values are notconsidered equal.Each unique table constraint must name a set of columns that isdifferent from the set of columns named by any other unique orprimary key constraint defined for the table. (Otherwise itwould just be the same constraint listed twice.).TP\fBPRIMARY KEY (column constraint)\fR.TP\fBPRIMARY KEY ( \fIcolumn_name\fB [, ... ] ) (table constraint)\fRThe primary key constraint specifies that a column or columns of a tablemay contain only unique (non-duplicate), nonnull values.Technically, PRIMARY KEY is merely acombination of UNIQUE and NOT NULL, butidentifying a set of columns as primary key also providesmetadata about the design of the schema, as a primary keyimplies that other tablesmay rely on this set of columns as a unique identifier for rows.Only one primary key can be specified for a table, whether as acolumn constraint or a table constraint.The primary key constraint should name a set of columns that isdifferent from other sets of columns named by any uniqueconstraint defined for the same table..TP\fBCHECK ( \fIexpression\fB )\fRThe CHECK clause specifies an expression producing aBoolean result which new or updated rows must satisfy for aninsert or update operation to succeed. Expressions evaluatingto TRUE or UNKNOWN succeed. Should any row of an insert orupdate operation produce a FALSE result an error exception israised and the insert or update does not alter the database. Acheck constraint specified as a column constraint shouldreference that column's value only, while an expressionappearing in a table constraint may reference multiple columns.Currently, CHECK expressions cannot containsubqueries nor refer to variables other than columns of thecurrent row..TP\fBREFERENCES \fIreftable\fB [ ( \fIrefcolumn\fB ) ] [ MATCH \fImatchtype\fB ] [ ON DELETE \fIaction\fB ] [ ON UPDATE \fIaction\fB ] (column constraint)\fR.TP\fBFOREIGN KEY ( \fIcolumn\fB [, ... ] )\fRThese clauses specify a foreign key constraint, which requiresthat a group of one or more columns of the new table must onlycontain values that match values in the referencedcolumn(s) of some row of the referenced table. If \fIrefcolumn\fR is omitted, theprimary key of the \fIreftable\fR is used. Thereferenced columns must be the columns of a unique or primarykey constraint in the referenced table. Note that foreign keyconstraints may not be defined between temporary tables andpermanent tables.A value inserted into the referencing column(s) is matched against thevalues of the referenced table and referenced columns using thegiven match type. There are three match types: MATCHFULL, MATCH PARTIAL, and MATCHSIMPLE, which is also the default. MATCHFULL will not allow one column of a multicolumn foreign keyto be null unless all foreign key columns are null.MATCH SIMPLE allows some foreign key columnsto be null while other parts of the foreign key are notnull. MATCH PARTIAL is not yet implemented.In addition, when the data in the referenced columns is changed,certain actions are performed on the data in this table'scolumns. The ON DELETE clause specifies theaction to perform when a referenced row in the referenced table isbeing deleted. Likewise, the ON UPDATEclause specifies the action to perform when a referenced columnin the referenced table is being updated to a new value. If therow is updated, but the referenced column is not actuallychanged, no action is done. Referential actions other than theNO ACTION check cannot be deferred, even ifthe constraint is declared deferrable. There are the following possibleactions for each clause:.RS.TP\fBNO ACTION\fRProduce an error indicating that the deletion or updatewould create a foreign key constraint violation.If the constraint is deferred, thiserror will be produced at constraint check time if there stillexist any referencing rows. This is the default action..TP\fBRESTRICT\fRProduce an error indicating that the deletion or updatewould create a foreign key constraint violation.This is the same as NO ACTION except thatthe check is not deferrable..TP\fBCASCADE\fRDelete any rows referencing the deleted row, or update thevalue of the referencing column to the new value of thereferenced column, respectively..TP\fBSET NULL\fRSet the referencing column(s) to null..TP\fBSET DEFAULT\fRSet the referencing column(s) to their default values..RE.PPIf the referenced column(s) are changed frequently, it may be wise toadd an index to the foreign key column so that referential actionsassociated with the foreign key column can be performed moreefficiently..TP\fBDEFERRABLE\fR.TP\fBNOT DEFERRABLE\fRThis controls whether the constraint can be deferred. Aconstraint that is not deferrable will be checked immediatelyafter every command. Checking of constraints that aredeferrable may be postponed until the end of the transaction(using the SET CONSTRAINTS [\fBset_constraints\fR(7)] command).NOT DEFERRABLE is the default. Only foreignkey constraints currently accept this clause. All otherconstraint types are not deferrable..TP\fBINITIALLY IMMEDIATE\fR.TP\fBINITIALLY DEFERRED\fRIf a constraint is deferrable, this clause specifies the defaulttime to check the constraint. If the constraint isINITIALLY IMMEDIATE, it is checked after eachstatement. This is the default. If the constraint isINITIALLY DEFERRED, it is checked only at theend of the transaction. The constraint check time can bealtered with the SET CONSTRAINTS [\fBset_constraints\fR(7)] command..TP\fBWITH ( \fIstorage_parameter\fB [= \fIvalue\fB] [, ... ] )\fRThis clause specifies optional storage parameters for a table or index;see Storage Parameters [\fBcreate_table\fR(7)] for moreinformation. The WITH clause for atable can also include OIDS=TRUE (or just OIDS)to specify that rows of the new tableshould have OIDs (object identifiers) assigned to them, orOIDS=FALSE to specify that the rows should not have OIDs.If OIDS is not specified, the default setting depends uponthe default_with_oids configuration parameter.(If the new table inherits from any tables that have OIDs, thenOIDS=TRUE is forced even if the command saysOIDS=FALSE.)If OIDS=FALSE is specified or implied, the newtable does not store OIDs and no OID will be assigned for a row insertedinto it. This is generally considered worthwhile, since itwill reduce OID consumption and thereby postpone the wraparoundof the 32-bit OID counter. Once the counter wraps around, OIDscan no longer be assumed to be unique, which makes themconsiderably less useful. In addition, excluding OIDs from atable reduces the space required to store the table on disk by4 bytes per row (on most machines), slightly improving performance.To remove OIDs from a table after it has been created, use ALTER TABLE [\fBalter_table\fR(7)].

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?