GitHub

@@ -763,38 +763,17 @@ CREATE TABLE products (

763763

price numeric

764764

);

765765

</programlisting>

766-

An explicit constraint name can also be specified, for example:

767-

<programlisting>

768-

CREATE TABLE products (

769-

product_no integer NOT NULL,

770-

name text <emphasis>CONSTRAINT products_name_not_null</emphasis> NOT NULL,

771-

price numeric

772-

);

773-

</programlisting>

774-

</para>

775-776-

<para>

777-

A not-null constraint is usually written as a column constraint. The

778-

syntax for writing it as a table constraint is

779-

<programlisting>

780-

CREATE TABLE products (

781-

product_no integer,

782-

name text,

783-

price numeric,

784-

<emphasis>NOT NULL product_no</emphasis>,

785-

<emphasis>NOT NULL name</emphasis>

786-

);

787-

</programlisting>

788-

But this syntax is not standard and mainly intended for use by

789-

<application>pg_dump</application>.

790766

</para>

791767792768

<para>

793-

A not-null constraint is functionally equivalent to creating a check

769+

A not-null constraint is always written as a column constraint. A

770+

not-null constraint is functionally equivalent to creating a check

794771

constraint <literal>CHECK (<replaceable>column_name</replaceable>

795772

IS NOT NULL)</literal>, but in

796773

<productname>PostgreSQL</productname> creating an explicit

797-

not-null constraint is more efficient.

774+

not-null constraint is more efficient. The drawback is that you

775+

cannot give explicit names to not-null constraints created this

776+

way.

798777

</para>

799778800779

<para>

@@ -811,10 +790,6 @@ CREATE TABLE products (

811790

order the constraints are checked.

812791

</para>

813792814-

<para>

815-

However, a column can have at most one explicit not-null constraint.

816-

</para>

817-818793

<para>

819794

The <literal>NOT NULL</literal> constraint has an inverse: the

820795

<literal>NULL</literal> constraint. This does not mean that the

@@ -1008,7 +983,7 @@ CREATE TABLE example (

10089831009984

<para>

1010985

A table can have at most one primary key. (There can be any number

1011-

of unique constraints, which combined with not-null constraints are functionally almost the

986+

of unique and not-null constraints, which are functionally almost the

1012987

same thing, but only one can be identified as the primary key.)

1013988

Relational database theory

1014989

dictates that every table must have a primary key. This rule is

@@ -1668,16 +1643,11 @@ ALTER TABLE products ADD CHECK (name &lt;&gt; '');

16681643

ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);

16691644

ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;

16701645

</programlisting>

1671-

</para>

1672-1673-

<para>

1674-

To add a not-null constraint, which is normally not written as a table

1675-

constraint, this special syntax is available:

1646+

To add a not-null constraint, which cannot be written as a table

1647+

constraint, use this syntax:

16761648

<programlisting>

16771649

ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;

16781650

</programlisting>

1679-

This command silently does nothing if the column already has a

1680-

not-null constraint.

16811651

</para>

1682165216831653

<para>

@@ -1718,15 +1688,12 @@ ALTER TABLE products DROP CONSTRAINT some_name;

17181688

</para>

1719168917201690

<para>

1721-

Simplified syntax is available to drop a not-null constraint:

1691+

This works the same for all constraint types except not-null

1692+

constraints. To drop a not-null constraint use:

17221693

<programlisting>

17231694

ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;

17241695

</programlisting>

1725-

This mirrors the <literal>SET NOT NULL</literal> syntax for adding a

1726-

not-null constraint. This command will silently do nothing if the column

1727-

does not have a not-null constraint. (Recall that a column can have at

1728-

most one not-null constraint, so it is never ambiguous which constraint

1729-

this command acts on.)

1696+

(Recall that not-null constraints do not have names.)

17301697

</para>

17311698

</sect2>

17321699

Read the original on github.com ↗