varchar(40) NOT NULL CHECK (name <> '')
);
</programlisting>
</para>
<para>
Create a table with a 2-dimensional array:
<programlisting>
CREATE TABLE array_int (
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 <> '')
);
</programlisting>
</para>
<para>
Define a primary key table constraint for the table
<structname>films</structname>:
<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</structname>. The following two examples are
equivalent, the first using the table constraint syntax, the second
the column constraint syntax:
<programlisting>
CREATE TABLE distributors (
did integer,
name varchar(40),
PRIMARY KEY(did)
);
CREATE TABLE distributors (
did integer PRIMARY KEY,
name varchar(40)
);
</programlisting>
</para>
<para>
Assign a literal constant default value for the column
<literal>name</literal>, arrange for the default value of column
<literal>did</literal> to be generated by selecting the next value
of a sequence object, and make 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</literal> 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 same, specified as a table constraint:
<programlisting>
CREATE TABLE distributors (
did integer,
name varchar(40),
UNIQUE(name)
);
</programlisting>
</para>
<para>
Create the same table, specifying 70% fill factor for both the table
and its unique index:
<programlisting>
CREATE TABLE distributors (
did integer,
name varchar(40),
UNIQUE(name) WITH (fillfactor=70)
)
WITH (fillfactor=70);
</programlisting>
</para>
<para>
Create table <structname>circles</structname> with an exclusion
constraint that prevents any two circles from overlapping:
<programlisting>
CREATE TABLE circles (
c circle,
EXCLUDE USING gist (c WITH &&)
);
</programlisting>
</para>
<para>
Create table <structname>cinemas</structname> in tablespace <structname>diskvol1</structname>:
<programlisting>
CREATE TABLE cinemas (
id serial,
name text,