create_table.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 719 行 · 第 1/2 页
7
719 行
.TP\fBWITH OIDS\fR.TP\fBWITHOUT OIDS\fRThese are obsolescent syntaxes equivalent to WITH (OIDS)and WITH (OIDS=FALSE), respectively. If you wish to giveboth an OIDS setting and storage parameters, you must usethe WITH ( ... ) syntax; see above..TP\fBON COMMIT\fRThe behavior of temporary tables at the end of a transactionblock can be controlled using ON COMMIT.The three options are:.RS.TP\fBPRESERVE ROWS\fRNo special action is taken at the ends of transactions.This is the default behavior..TP\fBDELETE ROWS\fRAll rows in the temporary table will be deleted at the endof each transaction block. Essentially, an automatic TRUNCATE [\fBtruncate\fR(7)] is doneat each commit..TP\fBDROP\fRThe temporary table will be dropped at the end of the currenttransaction block..RE.PP.TP\fBTABLESPACE \fItablespace\fB\fRThe \fItablespace\fR is the nameof the tablespace in which the new table is to be created.If not specified,default_tablespace is used, or the database'sdefault tablespace if default_tablespace is an emptystring..TP\fBUSING INDEX TABLESPACE \fItablespace\fB\fRThis clause allows selection of the tablespace in which the indexassociated with a UNIQUE or PRIMARYKEY constraint will be created.If not specified,default_tablespace is used, or the database'sdefault tablespace if default_tablespace is an emptystring..SS "STORAGE PARAMETERS".PPThe WITH clause can specify \fIstorage parameters\fRfor tables, and for indexes associated with a UNIQUE orPRIMARY KEY constraint. Storage parameters forindexes are documented in CREATE INDEX [\fBcreate_index\fR(7)]. The only storage parameter currentlyavailable for tables is:.TP\fBFILLFACTOR\fRThe fillfactor for a table is a percentage between 10 and 100.100 (complete packing) is the default. When a smaller fillfactoris specified, \fBINSERT\fR operations pack table pages onlyto the indicated percentage; the remaining space on each page isreserved for updating rows on that page. This gives \fBUPDATE\fRa chance to place the updated copy of a row on the same page as theoriginal, which is more efficient than placing it on a different page.For a table whose entries are never updated, complete packing is thebest choice, but in heavily updated tables smaller fillfactors areappropriate..SH "NOTES".PPUsing OIDs in new applications is not recommended: wherepossible, using a SERIAL or other sequencegenerator as the table's primary key is preferred. However, ifyour application does make use of OIDs to identify specificrows of a table, it is recommended to create a unique constrainton the \fBoid\fR column of that table, to ensure thatOIDs in the table will indeed uniquely identify rows even aftercounter wraparound. Avoid assuming that OIDs are unique acrosstables; if you need a database-wide unique identifier, use thecombination of \fBtableoid\fR and row OID for thepurpose..sp.RS.B "Tip:"The use of OIDS=FALSE is not recommendedfor tables with no primary key, since without either an OID or aunique data key, it is difficult to identify specific rows..RE.sp.PPPostgreSQL automatically creates anindex for each unique constraint and primary key constraint toenforce uniqueness. Thus, it is not necessary to create anindex explicitly for primary key columns. (See CREATE INDEX [\fBcreate_index\fR(7)] for more information.).PPUnique constraints and primary keys are not inherited in thecurrent implementation. This makes the combination ofinheritance and unique constraints rather dysfunctional..PPA table cannot have more than 1600 columns. (In practice, theeffective limit is usually lower because of tuple-length constraints.).SH "EXAMPLES".PPCreate table \fBfilms\fR and table\fBdistributors\fR:.sp.nfCREATE TABLE films ( code char(5) CONSTRAINT firstkey PRIMARY KEY, title varchar(40) NOT NULL, did integer NOT NULL, date_prod date, kind varchar(10), len interval hour to minute);.sp.fi.sp.nfCREATE TABLE distributors ( did integer PRIMARY KEY DEFAULT nextval('serial'), name varchar(40) NOT NULL CHECK (name <> ''));.sp.fi.PPCreate a table with a 2-dimensional array:.sp.nfCREATE TABLE array_int ( vector int[][]);.sp.fi.PPDefine a unique table constraint for the tablefilms. Unique table constraints can be definedon one or more columns of the table..sp.nfCREATE TABLE films ( code char(5), title varchar(40), did integer, date_prod date, kind varchar(10), len interval hour to minute, CONSTRAINT production UNIQUE(date_prod));.sp.fi.PPDefine a check column constraint:.sp.nfCREATE TABLE distributors ( did integer CHECK (did > 100), name varchar(40));.sp.fi.PPDefine a check table constraint:.sp.nfCREATE TABLE distributors ( did integer, name varchar(40) CONSTRAINT con1 CHECK (did > 100 AND name <> ''));.sp.fi.PPDefine a primary key table constraint for the table\fBfilms\fR:.sp.nfCREATE TABLE films ( code char(5), title varchar(40), did integer, date_prod date, kind varchar(10), len interval hour to minute, CONSTRAINT code_title PRIMARY KEY(code,title));.sp.fi.PPDefine a primary key constraint for table\fBdistributors\fR. The following two examples areequivalent, the first using the table constraint syntax, the secondthe column constraint syntax:.sp.nfCREATE TABLE distributors ( did integer, name varchar(40), PRIMARY KEY(did));.sp.fi.sp.nfCREATE TABLE distributors ( did integer PRIMARY KEY, name varchar(40));.sp.fi.PPAssign a literal constant default value for the columnname, arrange for the default value of columndid to be generated by selecting the next valueof a sequence object, and make the default value ofmodtime be the time at which the row isinserted:.sp.nfCREATE TABLE distributors ( name varchar(40) DEFAULT 'Luso Films', did integer DEFAULT nextval('distributors_serial'), modtime timestamp DEFAULT current_timestamp);.sp.fi.PPDefine two NOT NULL column constraints on the table\fBdistributors\fR, one of which is explicitlygiven a name:.sp.nfCREATE TABLE distributors ( did integer CONSTRAINT no_null NOT NULL, name varchar(40) NOT NULL);.sp.fi.PPDefine a unique constraint for the name column:.sp.nfCREATE TABLE distributors ( did integer, name varchar(40) UNIQUE);.sp.fiThe same, specified as a table constraint:.sp.nfCREATE TABLE distributors ( did integer, name varchar(40), UNIQUE(name));.sp.fi.PPCreate the same table, specifying 70% fill factor for both the tableand its unique index:.sp.nfCREATE TABLE distributors ( did integer, name varchar(40), UNIQUE(name) WITH (fillfactor=70))WITH (fillfactor=70);.sp.fi.PPCreate table \fBcinemas\fR in tablespace \fBdiskvol1\fR:.sp.nfCREATE TABLE cinemas ( id serial, name text, location text) TABLESPACE diskvol1;.sp.fi.SH "COMPATIBILITY".PPThe \fBCREATE TABLE\fR command conforms to theSQL standard, with exceptions listed below..SS "TEMPORARY TABLES".PPAlthough the syntax of CREATE TEMPORARY TABLEresembles that of the SQL standard, the effect is not the same. In thestandard,temporary tables are defined just once and automatically exist (startingwith empty contents) in every session that needs them.PostgreSQL insteadrequires each session to issue its own CREATE TEMPORARYTABLE command for each temporary table to be used. This allowsdifferent sessions to use the same temporary table name for differentpurposes, whereas the standard's approach constrains all instances of agiven temporary table name to have the same table structure..PPThe standard's definition of the behavior of temporary tables iswidely ignored. PostgreSQL's behavioron this point is similar to that of several other SQL databases..PPThe standard's distinction between global and local temporary tablesis not in PostgreSQL, since that distinctiondepends on the concept of modules, whichPostgreSQL does not have.For compatibility's sake, PostgreSQL willaccept the GLOBAL and LOCAL keywordsin a temporary table declaration, but they have no effect..PPThe ON COMMIT clause for temporary tablesalso resembles the SQL standard, but has some differences.If the ON COMMIT clause is omitted, SQL specifies that thedefault behavior is ON COMMIT DELETE ROWS. However, thedefault behavior in PostgreSQL isON COMMIT PRESERVE ROWS. The ON COMMITDROP option does not exist in SQL..SS "COLUMN CHECK CONSTRAINTS".PPThe SQL standard says that CHECK column constraintsmay only refer to the column they apply to; only CHECKtable constraints may refer to multiple columns.PostgreSQL does not enforce thisrestriction; it treats column and table check constraints alike..SS "NULL ``CONSTRAINT''".PPThe NULL ``constraint'' (actually anon-constraint) is a PostgreSQLextension to the SQL standard that is included for compatibility with someother database systems (and for symmetry with the NOTNULL constraint). Since it is the default for anycolumn, its presence is simply noise..SS "INHERITANCE".PPMultiple inheritance via the INHERITS clause isa PostgreSQL language extension.SQL:1999 and later define single inheritance using adifferent syntax and different semantics. SQL:1999-styleinheritance is not yet supported byPostgreSQL..SS "ZERO-COLUMN TABLES".PPPostgreSQL allows a table of no columnsto be created (for example, CREATE TABLE foo();). Thisis an extension from the SQL standard, which does not allow zero-columntables. Zero-column tables are not in themselves very useful, butdisallowing them creates odd special cases for \fBALTER TABLEDROP COLUMN\fR, so it seems cleaner to ignore this spec restriction..SS "WITH CLAUSE".PPThe WITH clause is a PostgreSQLextension; neither storage parameters nor OIDs are in the standard..SS "TABLESPACES".PPThe PostgreSQL concept of tablespaces is notpart of the standard. Hence, the clauses TABLESPACEand USING INDEX TABLESPACE are extensions..SH "SEE ALSO"ALTER TABLE [\fBalter_table\fR(7)], DROP TABLE [\fBdrop_table\fR(l)], CREATE TABLESPACE [\fBcreate_tablespace\fR(l)]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?