⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 create_table.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 3 页
字号:
<!--$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_table.sgml,v 1.73.2.2 2003/11/14 22:56:25 tgl Exp $PostgreSQL documentation--><refentry id="SQL-CREATETABLE"> <refmeta>  <refentrytitle id="sql-createtable-title">CREATE TABLE</refentrytitle>  <refmiscinfo>SQL - Language Statements</refmiscinfo> </refmeta> <refnamediv>  <refname>CREATE TABLE</refname>  <refpurpose>define a new table</refpurpose> </refnamediv> <indexterm zone="sql-createtable">  <primary>CREATE TABLE</primary> </indexterm> <refsynopsisdiv><synopsis>CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable class="PARAMETER">table_name</replaceable> (  { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ DEFAULT <replaceable>default_expr</> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]    | <replaceable>table_constraint</replaceable>    | LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } DEFAULTS ] }  [, ... ])[ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ][ WITH OIDS | WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]where <replaceable class="PARAMETER">column_constraint</replaceable> is:[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]{ NOT NULL | NULL | UNIQUE | PRIMARY KEY |  CHECK (<replaceable class="PARAMETER">expression</replaceable>) |  REFERENCES <replaceable class="PARAMETER">reftable</replaceable> [ ( <replaceable class="PARAMETER">refcolumn</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]    [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]and <replaceable class="PARAMETER">table_constraint</replaceable> is:[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]{ UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) |  PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) |  CHECK ( <replaceable class="PARAMETER">expression</replaceable> ) |  FOREIGN KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) REFERENCES <replaceable class="PARAMETER">reftable</replaceable> [ ( <replaceable class="PARAMETER">refcolumn</replaceable> [, ... ] ) ]    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ] }[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]</synopsis>   </refsynopsisdiv> <refsect1 id="SQL-CREATETABLE-description">  <title>Description</title>  <para>   <command>CREATE TABLE</command> will create a new, initially empty table   in the current database. The table will be owned by the user issuing the   command.  </para>  <para>   If a schema name is given (for example, <literal>CREATE TABLE   myschema.mytable ...</>) then the table is created in the   specified schema.  Otherwise it is created in the current schema.   Temporary tables exist in a special schema, so a schema name may not be   given when creating a temporary table.   The table name must be distinct from the name of any other table,   sequence, index, or view in the same schema.  </para>  <para>   <command>CREATE TABLE</command> also automatically creates a data   type that represents the composite type corresponding   to one row of the table.  Therefore, tables cannot have the same   name as any existing data type in the same schema.  </para>  <para>   A table cannot have more than 1600 columns.  (In practice, the   effective limit is lower because of tuple-length constraints).  </para>  <para>   The optional constraint clauses specify constraints (or tests) that   new or updated rows must satisfy for an insert or update operation   to succeed.  A constraint is an SQL object that helps define the   set of valid values in the table in various ways.  </para>  <para>   There are two ways to define constraints: table constraints and   column constraints.  A column constraint is defined as part of a   column definition.  A table constraint definition is not tied to a   particular column, and it can encompass more than one column.   Every column constraint can also be written as a table constraint;   a column constraint is only a notational convenience if the   constraint only affects one column.  </para> </refsect1> <refsect1>  <title>Parameters</title>  <variablelist>   <varlistentry>    <term><literal>TEMPORARY</> or <literal>TEMP</></term>    <listitem>     <para>      If specified, the table is created as a temporary table.      Temporary tables are automatically dropped at the end of a      session, or optionally at the end of the current transaction       (see ON COMMIT below).  Existing permanent tables with the same       name are not visible to the current session while the temporary       table exists, unless they are referenced with schema-qualified       names. Any indexes created on a temporary table are automatically      temporary as well.     </para>     <para>      Optionally, <literal>GLOBAL</literal> or <literal>LOCAL</literal>      can be written before <literal>TEMPORARY</> or <literal>TEMP</>.      This makes no difference in <productname>PostgreSQL</>, but see      <xref linkend="sql-createtable-compatibility"      endterm="sql-createtable-compatibility-title">.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><replaceable class="PARAMETER">table_name</replaceable></term>    <listitem>     <para>      The name (optionally schema-qualified) of the table to be created.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><replaceable class="PARAMETER">column_name</replaceable></term>    <listitem>     <para>      The name of a column to be created in the new table.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><replaceable class="PARAMETER">data_type</replaceable></term>    <listitem>     <para>      The data type of the column. This may include array specifiers.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>DEFAULT    <replaceable>default_expr</replaceable></literal></term>    <listitem>     <para>      The <literal>DEFAULT</> 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.     </para>     <para>      The 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.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } DEFAULTS ]</literal></term>    <listitem>     <para>      The <literal>LIKE</literal> clause specifies a table from which      the new table automatically inherits all column names, their data types, and      not-null constraints.     </para>     <para>      Unlike <literal>INHERITS</literal>, the new table and inherited table      are complete decoupled after creation has been completed.  Data inserted      into the new table will not be reflected into the parent table.     </para>     <para>      Default expressions for the inherited column definitions will only be included if      <literal>INCLUDING DEFAULTS</literal> is specified.  The default is to exclude      default expressions.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>INHERITS ( <replaceable>parent_table</replaceable> [, ... ] )</literal></term>    <listitem>     <para>      The optional <literal>INHERITS</> clause specifies a list of      tables from which the new table automatically inherits all      columns.  If the same column name exists in more than one parent      table, an error is reported unless the data types of the columns      match in each of the parent tables.  If there is no conflict,      then the duplicate columns are merged to form a single column in      the new table.  If the column name list of the new table      contains a column that is also inherited, the data type must      likewise match the inherited column(s), and the column      definitions are merged into one.  However, inherited and new      column declarations of the same name need not specify identical      constraints: all constraints provided from any declaration are      merged together and all are applied to the new table.  If the      new table explicitly specifies a default value for the column,      this default overrides any defaults from inherited declarations      of the column.  Otherwise, any parents that specify default      values for the column must all specify the same default, or an      error will be reported.     </para><!--     <para>      <productname>PostgreSQL</> automatically allows the     created table to inherit      functions on tables above it in the inheritance hierarchy; that      is, if we create table <literal>foo</literal> inheriting from      <literal>bar</literal>, then functions that accept the tuple      type <literal>bar</literal> can also be applied to instances of      <literal>foo</literal>.  (Currently, this works reliably for      functions on the first or only parent table, but not so well for      functions on additional parents.)     </para>-->    </listitem>   </varlistentry>   <varlistentry>    <term><literal>WITH OIDS</></term>    <term><literal>WITHOUT OIDS</></term>    <listitem>     <para>      This optional clause specifies whether rows of the new table      should have OIDs (object identifiers) assigned to them.  The      default is to have OIDs.  (If the new table inherits from any      tables that have OIDs, then <literal>WITH OIDS</> is forced even      if the command says <literal>WITHOUT OIDS</>.)     </para>     <para>      Specifying <literal>WITHOUT OIDS</> allows the user to suppress      generation of OIDs for rows of a table.  This may be worthwhile      for large tables, since it will reduce OID consumption and      thereby postpone wraparound of the 32-bit OID counter.  Once the      counter wraps around, uniqueness of OIDs can no longer be      assumed, which considerably reduces their usefulness. Specifying      <literal>WITHOUT OIDS</literal> also reduces the space required      to store the table on disk by 4 bytes per row of the table,      thereby improving performance.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable></literal></term>    <listitem>     <para>      An optional name for a column or table constraint.  If not specified,      the system generates a name.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>NOT NULL</></term>    <listitem>     <para>      The column is not allowed to contain null values.     </para>    </listitem>   </varlistentry>   <varlistentry>    <term><literal>NULL</></term>    <listitem>     <para>      The column is allowed to contain null values. This is the default.     </para>     <para>      This clause is only available for compatibility with      non-standard SQL databases.  Its use is discouraged in new      applications.     </para>    </listitem>   </varlistentry>      <varlistentry>    <term><literal>UNIQUE</> (column constraint)</term>    <term><literal>UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>    <listitem>     <para>      The <literal>UNIQUE</literal> constraint specifies that a      group of one or more distinct columns of a table may contain      only unique values. The behavior of the unique table constraint      is the same as that for column constraints, with the additional      capability to span multiple columns.

⌨️ 快捷键说明

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