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

📄 ddl.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 5 页
字号:
   tables as you wish.  If a user attempts to store data in a column   that would violate a constraint, an error is raised.  This applies   even if the value came from the default value definition.  </para>  <sect2>   <title>Check Constraints</title>   <indexterm>    <primary>check constraint</primary>   </indexterm>   <indexterm>    <primary>constraint</primary>    <secondary>check</secondary>   </indexterm>   <para>    A check constraint is the most generic constraint type.  It allows    you to specify that the value in a certain column must satisfy an    arbitrary expression.  For instance, to require positive product    prices, you could use:<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric <emphasis>CHECK (price > 0)</emphasis>);</programlisting>   </para>   <para>    As you see, the constraint definition comes after the data type,    just like default value definitions.  Default values and    constraints can be listed in any order.  A check constraint    consists of the key word <literal>CHECK</literal> followed by an    expression in parentheses.  The check constraint expression should    involve the column thus constrained, otherwise the constraint    would not make too much sense.   </para>   <indexterm>    <primary>constraint</primary>    <secondary>name</secondary>   </indexterm>   <para>    You can also give the constraint a separate name.  This clarifies    error messages and allows you to refer to the constraint when you    need to change it.  The syntax is:<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric <emphasis>CONSTRAINT positive_price</emphasis> CHECK (price > 0));</programlisting>    So, to specify a named constraint, use the key word    <literal>CONSTRAINT</literal> followed by an identifier followed    by the constraint definition.   </para>   <para>    A check constraint can also refer to several columns.  Say you    store a regular price and a discounted price, and you want to    ensure that the discounted price is lower than the regular price.<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric CHECK (price > 0),    discounted_price numeric CHECK (discounted_price > 0),    CHECK (price > discounted_price));</programlisting>   </para>   <para>    The first two constraints should look familiar.  The third one    uses a new syntax.  It is not attached to a particular column,    instead it appears as a separate item in the comma-separated    column list.  Column definitions and these constraint    definitions can be listed in mixed order.   </para>   <para>    We say that the first two constraints are column constraints, whereas the    third one is a table constraint because it is written separately    from the column definitions.  Column constraints can also be    written as table constraints, while the reverse is not necessarily    possible.  The above example could also be written as<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric,    CHECK (price > 0),    discounted_price numeric,    CHECK (discounted_price > 0),    CHECK (price > discounted_price));</programlisting>    or even<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric CHECK (price > 0),    discounted_price numeric,    CHECK (discounted_price > 0 AND price > discounted_price));</programlisting>    It's a matter of taste.   </para>   <indexterm>    <primary>null value</primary>    <secondary sortas="check constraints">with check constraints</secondary>   </indexterm>   <para>    It should be noted that a check constraint is satisfied if the    check expression evaluates to true or the null value.  Since most    expressions will evaluate to the null value if one operand is null,    they will not prevent null values in the constrained columns.  To    ensure that a column does not contain null values, the not-null    constraint described in the next section should be used.   </para>  </sect2>  <sect2>   <title>Not-Null Constraints</title>   <indexterm>    <primary>not-null constraint</primary>   </indexterm>   <indexterm>    <primary>constraint</primary>    <secondary>NOT NULL</secondary>   </indexterm>   <para>    A not-null constraint simply specifies that a column must not    assume the null value.  A syntax example:<programlisting>CREATE TABLE products (    product_no integer <emphasis>NOT NULL</emphasis>,    name text <emphasis>NOT NULL</emphasis>,    price numeric);</programlisting>   </para>   <para>    A not-null constraint is always written as a column constraint.  A    not-null constraint is functionally equivalent to creating a check    constraint <literal>CHECK (<replaceable>column_name</replaceable>    IS NOT NULL)</literal>, but in    <productname>PostgreSQL</productname> creating an explicit    not-null constraint is more efficient.  The drawback is that you    cannot give explicit names to not-null constraints created that    way.   </para>   <para>    Of course, a column can have more than one constraint.  Just write    the constraints after one another:<programlisting>CREATE TABLE products (    product_no integer NOT NULL,    name text NOT NULL,    price numeric NOT NULL CHECK (price > 0));</programlisting>    The order doesn't matter.  It does not necessarily determine in which    order the constraints are checked.   </para>   <para>    The <literal>NOT NULL</literal> constraint has an inverse: the    <literal>NULL</literal> constraint.  This does not mean that the    column must be null, which would surely be useless.  Instead, this    simply defines the default behavior that the column may be null.    The <literal>NULL</literal> constraint is not defined in the SQL    standard and should not be used in portable applications.  (It was    only added to <productname>PostgreSQL</productname> to be    compatible with some other database systems.)  Some users, however,    like it because it makes it easy to toggle the constraint in a    script file.  For example, you could start with<programlisting>CREATE TABLE products (    product_no integer NULL,    name text NULL,    price numeric NULL);</programlisting>    and then insert the <literal>NOT</literal> key word where desired.   </para>   <tip>    <para>     In most database designs the majority of columns should be marked     not null.    </para>   </tip>  </sect2>  <sect2>   <title>Unique Constraints</title>   <indexterm>    <primary>unique constraint</primary>   </indexterm>   <indexterm>    <primary>constraint</primary>    <secondary>unique</secondary>   </indexterm>   <para>    Unique constraints ensure that the data contained in a column or a    group of columns is unique with respect to all the rows in the    table.  The syntax is<programlisting>CREATE TABLE products (    product_no integer <emphasis>UNIQUE</emphasis>,    name text,    price numeric);</programlisting>    when written as a column constraint, and<programlisting>CREATE TABLE products (    product_no integer,    name text,    price numeric,    <emphasis>UNIQUE (product_no)</emphasis>);</programlisting>    when written as a table constraint.   </para>   <para>    If a unique constraint refers to a group of columns, the columns    are listed separated by commas:<programlisting>CREATE TABLE example (    a integer,    b integer,    c integer,    <emphasis>UNIQUE (a, c)</emphasis>);</programlisting>   </para>   <para>    It is also possible to assign names to unique constraints:<programlisting>CREATE TABLE products (    product_no integer <emphasis>CONSTRAINT must_be_different</emphasis> UNIQUE,    name text,    price numeric);</programlisting>   </para>   <indexterm>    <primary>null value</primary>    <secondary sortas="unique constraints">with unique constraints</secondary>   </indexterm>   <para>    In general, a unique constraint is violated when there are (at    least) two rows in the table where the values of each of the    corresponding columns that are part of the constraint are equal.    However, null values are not considered equal in this    consideration.  That means even in the presence of a    unique constraint it is possible to store an unlimited number of    rows that contain a null value in at least one of the constrained    columns.  This behavior conforms to the SQL standard, but we have    heard that other SQL databases may not follow this rule.  So be    careful when developing applications that are intended to be    portable.   </para>  </sect2>  <sect2>   <title>Primary Keys</title>   <indexterm>    <primary>primary key</primary>   </indexterm>   <indexterm>    <primary>constraint</primary>    <secondary>primary key</secondary>   </indexterm>   <para>    Technically, a primary key constraint is simply a combination of a    unique constraint and a not-null constraint.  So, the following    two table definitions accept the same data:<programlisting>CREATE TABLE products (    product_no integer UNIQUE NOT NULL,    name text,    price numeric);</programlisting><programlisting>CREATE TABLE products (    product_no integer <emphasis>PRIMARY KEY</emphasis>,    name text,    price numeric);</programlisting>   </para>   <para>    Primary keys can also constrain more than one column; the syntax    is similar to unique constraints:<programlisting>CREATE TABLE example (    a integer,    b integer,    c integer,    <emphasis>PRIMARY KEY (a, c)</emphasis>);</programlisting>   </para>   <para>    A primary key indicates that a column or group of columns can be    used as a unique identifier for rows in the table.  (This is a    direct consequence of the definition of a primary key.  Note that    a unique constraint does not, by itself, provide a unique identifier    because it does not exclude null values.)  This is useful both for    documentation purposes and for client applications.  For example,    a GUI application that allows modifying row values probably needs    to know the primary key of a table to be able to identify rows    uniquely.   </para>   <para>    A table can have at most one primary key (while it can have many    unique and not-null constraints).  Relational database theory    dictates that every table must have a primary key.  This rule is    not enforced by <productname>PostgreSQL</productname>, but it is    usually best to follow it.   </para>  </sect2>  <sect2 id="ddl-constraints-fk">   <title>Foreign Keys</title>   <indexterm>    <primary>foreign key</primary>   </indexterm>   <indexterm>    <primary>constraint</primary>    <secondary>foreign key</secondary>   </indexterm>   <indexterm>    <primary>referential integrity</primary>   </indexterm>   <para>    A foreign key constraint specifies that the values in a column (or    a group of columns) must match the values appearing in some row    of another table.    We say this maintains the <firstterm>referential    integrity</firstterm> between two related tables.   </para>   <para>    Say you have the product table that we have used several times already:<programlisting>CREATE TABLE products (    product_no integer PRIMARY KEY,    name text,    price numeric);</programlisting>    Let's also assume you have a table storing orders of those    products.  We want to ensure that the orders table only contains    orders of products that actually exist.  So we define a foreign    key constraint in the orders table that references the products    table:<programlisting>CREATE TABLE orders (    order_id integer PRIMARY KEY,    product_no integer <emphasis>REFERENCES products (product_no)</emphasis>,    quantity integer);</programlisting>    Now it is impossible to create orders with

⌨️ 快捷键说明

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