📄 create_table.sgml
字号:
<!--$PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.109 2007/07/17 05:02:00 neilc 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 | CONSTRAINTS | INDEXES } ] ... } [, ... ]] )[ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ][ WITH ( <replaceable class="PARAMETER">storage_parameter</replaceable> [= <replaceable class="PARAMETER">value</replaceable>] [, ... ] ) | WITH OIDS | WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ][ TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ]where <replaceable class="PARAMETER">column_constraint</replaceable> is:[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]{ NOT NULL | NULL | UNIQUE <replaceable class="PARAMETER">index_parameters</replaceable> | PRIMARY KEY <replaceable class="PARAMETER">index_parameters</replaceable> | 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> [, ... ] ) <replaceable class="PARAMETER">index_parameters</replaceable> | PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) <replaceable class="PARAMETER">index_parameters</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 ]<replaceable class="PARAMETER">index_parameters</replaceable> in <literal>UNIQUE</> and <literal>PRIMARY KEY</> constraints are:[ WITH ( <replaceable class="PARAMETER">storage_parameter</replaceable> [= <replaceable class="PARAMETER">value</replaceable>] [, ... ] ) ][ USING INDEX TABLESPACE <replaceable class="PARAMETER">tablespace</replaceable> ]</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 cannot be given when creating a temporary table. The name of the table 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> The optional constraint clauses specify constraints (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 for use when 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 <literal>ON COMMIT</literal> 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 can include array specifiers. For more information on the data types supported by <productname>PostgreSQL</productname>, refer to <xref linkend="datatype">. </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>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. </para> <para> Use of <literal>INHERITS</> creates a persistent relationship between the new child table and its parent table(s). Schema modifications to the parent(s) normally propagate to children as well, and by default the data of the child table is included in scans of the parent(s). </para> <para> 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 name 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>LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ]</literal></term> <listitem> <para> The <literal>LIKE</literal> clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints. </para> <para> Unlike <literal>INHERITS</literal>, 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. </para> <para> Default expressions for the copied column definitions will only be copied if <literal>INCLUDING DEFAULTS</literal> is specified. The default behavior is to exclude default expressions, resulting in the copied columns in the new table having null defaults. </para> <para> Not-null constraints are always copied to the new table. <literal>CHECK</literal> constraints will only be copied if <literal>INCLUDING CONSTRAINTS</literal> is specified; other types of constraints will never be copied. Also, no distinction is made between column constraints and table constraints — when constraints are requested, all check constraints are copied. </para> <para> Any indexes on the original table will not be created on the new table, unless the <literal>INCLUDING INDEXES</literal> clause is specified. </para> <para> Note also that unlike <literal>INHERITS</literal>, copied columns and constraints are not merged with similarly named columns and constraints. If the same name is specified explicitly or in another <literal>LIKE</literal> clause, an error is signalled. </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 the constraint is violated, the constraint name is present in error messages, so constraint names like <literal>col must be positive</> 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. </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 provided 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 columns of a table can 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. </para> <para> For the purpose of a unique constraint, null values are not considered equal. </para> <para> Each unique table constraint must name a set of columns that is different from the set of columns named by any other unique or primary key constraint defined for the table. (Otherwise it would just be the same constraint listed twice.) </para> </listitem> </varlistentry> <varlistentry> <term><literal>PRIMARY KEY</> (column constraint)</term> <term><literal>PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term> <listitem> <para> The primary key constraint specifies that a column or columns of a table can contain only unique (non-duplicate), nonnull values. Technically, <literal>PRIMARY KEY</literal> is merely a combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but identifying a set of columns as primary key also provides metadata about the design of the schema, as a primary key implies that other tables can rely on this set of columns as a unique identifier for rows. </para> <para> Only one primary key can be specified for a table, whether as a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -