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

📄 create_table.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 3 页
字号:
  <para>   Create table <structname>films</> and table   <structname>distributors</>:<programlisting>CREATE 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);</programlisting><programlisting>CREATE TABLE distributors (     did    integer PRIMARY KEY DEFAULT nextval('serial'),     name   varchar(40) NOT NULL CHECK (name &lt;&gt; ''));</programlisting>  </para>  <para>   Create a table with a 2-dimensional array:<programlisting>CREATE TABLE array (    vector  int[][]);</programlisting>  </para>  <para>   Define a unique table constraint for the table   <literal>films</literal>.  Unique table constraints can be defined   on one or more columns of the table.<programlisting>CREATE 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));</programlisting>  </para>    <para>   Define a check column constraint:<programlisting>CREATE TABLE distributors (    did     integer CHECK (did > 100),    name    varchar(40));</programlisting>  </para>  <para>   Define a check table constraint:<programlisting>CREATE TABLE distributors (    did     integer,    name    varchar(40)    CONSTRAINT con1 CHECK (did > 100 AND name &lt;&gt; ''));</programlisting>  </para>   <para>   Define a primary key table constraint for the table   <structname>films</>.  Primary key table constraints can be defined   on one or more columns of the table.<programlisting>CREATE 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));</programlisting>  </para>  <para>   Define a primary key constraint for table   <structname>distributors</>.  The following two examples are   equivalent, the first using the table constraint syntax, the second   the column constraint notation.<programlisting>CREATE TABLE distributors (    did     integer,    name    varchar(40),    PRIMARY KEY(did)); </programlisting><programlisting>CREATE TABLE distributors (    did     integer PRIMARY KEY,    name    varchar(40));</programlisting>  </para>  <para>   This assigns a literal constant default value for the column   <literal>name</literal>, arranges for the default value of column   <literal>did</literal> to be generated by selecting the next value   of a sequence object, and makes the default value of   <literal>modtime</literal> be the time at which the row is   inserted.<programlisting>CREATE TABLE distributors (    name      varchar(40) DEFAULT 'Luso Films',    did       integer DEFAULT nextval('distributors_serial'),    modtime   timestamp DEFAULT current_timestamp);</programlisting>  </para>  <para>   Define two <literal>NOT NULL</> column constraints on the table   <classname>distributors</classname>, one of which is explicitly   given a name:<programlisting>CREATE TABLE distributors (    did     integer CONSTRAINT no_null NOT NULL,    name    varchar(40) NOT NULL);</programlisting>    </para>    <para>     Define a unique constraint for the <literal>name</literal> column:<programlisting>CREATE TABLE distributors (    did     integer,    name    varchar(40) UNIQUE);</programlisting>     The above is equivalent to the following specified as a table constraint:<programlisting>CREATE TABLE distributors (    did     integer,    name    varchar(40),    UNIQUE(name));</programlisting>  </para> </refsect1>  <refsect1 id="SQL-CREATETABLE-compatibility">  <title id="SQL-CREATETABLE-compatibility-title">Compatibility</title>  <para>   The <command>CREATE TABLE</command> command conforms to SQL92   and to a subset of SQL99, with exceptions listed below.  </para>  <refsect2>   <title>Temporary Tables</title>   <para>    Although the syntax of <literal>CREATE TEMPORARY TABLE</literal>    resembles that of the SQL standard, the effect is not the same.  In the    standard,     temporary tables are defined just once and automatically exist (starting    with empty contents) in every session that needs them.    <productname>PostgreSQL</productname> instead    requires each session to issue its own <literal>CREATE TEMPORARY    TABLE</literal> command for each temporary table to be used.  This allows    different sessions to use the same temporary table name for different    purposes, whereas the standard's approach constrains all instances of a    given temporary table name to have the same table structure.   </para>   <para>    The standard's definition of the behavior of temporary tables is    widely ignored.  <productname>PostgreSQL</productname>'s behavior    on this point is similar to that of several other SQL databases.   </para>   <para>    The standard's distinction between global and local temporary tables    is not in <productname>PostgreSQL</productname>, since that distinction    depends on the concept of modules, which    <productname>PostgreSQL</productname> does not have.    For compatibility's sake, <productname>PostgreSQL</productname> will    accept the <literal>GLOBAL</literal> and <literal>LOCAL</literal> keywords    in a temporary table declaration, but they have no effect.   </para>   <para>    The <literal>ON COMMIT</literal> clause for temporary tables    also resembles the SQL standard, but has some differences.    If the <literal>ON COMMIT</> clause is omitted, SQL specifies that the    default behavior is <literal>ON COMMIT DELETE ROWS</>.  However, the    default behavior in <productname>PostgreSQL</productname> is    <literal>ON COMMIT PRESERVE ROWS</literal>.  The <literal>ON COMMIT    DROP</literal> option does not exist in SQL.   </para>  </refsect2>  <refsect2>   <title>Column Check Constraints</title>   <para>    The SQL standard says that <literal>CHECK</> column constraints    may only refer to the column they apply to; only <literal>CHECK</>    table constraints may refer to multiple columns.    <productname>PostgreSQL</productname> does not enforce this    restriction; it treats column and table check constraints alike.   </para>  </refsect2>  <refsect2>   <title><literal>NULL</literal> <quote>Constraint</quote></title>   <para>    The <literal>NULL</> <quote>constraint</quote> (actually a    non-constraint) is a <productname>PostgreSQL</productname>    extension to the SQL standard that is included for compatibility with some    other database systems (and for symmetry with the <literal>NOT    NULL</literal> constraint).  Since it is the default for any    column, its presence is simply noise.   </para>  </refsect2>     <refsect2>   <title>Inheritance</title>   <para>    Multiple inheritance via the <literal>INHERITS</literal> clause is    a <productname>PostgreSQL</productname> language extension.  SQL99    (but not SQL92) defines single inheritance using a different    syntax and different semantics.  SQL99-style inheritance is not    yet supported by <productname>PostgreSQL</productname>.   </para>  </refsect2>  <refsect2>   <title>Object IDs</title>   <para>    The <productname>PostgreSQL</productname> concept of OIDs is not    standard.   </para>  </refsect2>  <refsect2>   <title>Zero-column tables</title>   <para>    <productname>PostgreSQL</productname> allows a table of no columns    to be created (for example, <literal>CREATE TABLE foo();</>).  This    is an extension from the SQL standard, which does not allow zero-column    tables.  Zero-column tables are not in themselves very useful, but    disallowing them creates odd special cases for <command>ALTER TABLE    DROP COLUMN</>, so it seems cleaner to ignore this spec restriction.   </para>  </refsect2> </refsect1> <refsect1>  <title>See Also</title>  <simplelist type="inline">   <member><xref linkend="sql-altertable" endterm="sql-altertable-title"></member>   <member><xref linkend="sql-droptable" endterm="sql-droptable-title"></member>  </simplelist> </refsect1></refentry><!-- Keep this comment at the end of the fileLocal variables:mode: sgmlsgml-omittag:nilsgml-shorttag:tsgml-minimize-attributes:nilsgml-always-quote-attributes:tsgml-indent-step:1sgml-indent-data:tsgml-parent-document:nilsgml-default-dtd-file:"../reference.ced"sgml-exposed-tags:nilsgml-local-catalogs:"/usr/lib/sgml/catalog"sgml-local-ecat-files:nilEnd:-->

⌨️ 快捷键说明

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