<!-- doc/src/sgml/ddl.sgml -->
<chapter id="ddl">
<title>Data Definition</title>
<para>
This chapter covers how one creates the database structures that
will hold one's data. In a relational database, the raw data is
stored in tables, so the majority of this chapter is devoted to
explaining how tables are created and modified and what features are
available to control what data is stored in the tables.
Subsequently, we discuss how tables can be organized into
schemas, and how privileges can be assigned to tables. Finally,
we will briefly look at other features that affect the data storage,
such as inheritance, table partitioning, views, functions, and
triggers.
</para>
<sect1 id="ddl-basics">
<title>Table Basics</title>
<indexterm zone="ddl-basics">
<primary>table</primary>
</indexterm>
<indexterm>
<primary>row</primary>
</indexterm>
<indexterm>
<primary>column</primary>
</indexterm>
<para>
A table in a relational database is much like a table on paper: It
consists of rows and columns. The number and order of the columns
is fixed, and each column has a name. The number of rows is
variable — it reflects how much data is stored at a given moment.
SQL does not make any guarantees about the order of the rows in a
table. When a table is read, the rows will appear in an unspecified order,
unless sorting is explicitly requested. This is covered in <xref
linkend="queries"/>. Furthermore, SQL does not assign unique
identifiers to rows, so it is possible to have several completely
identical rows in a table. This is a consequence of the
mathematical model that underlies SQL but is usually not desirable.
Later in this chapter we will see how to deal with this issue.
</para>
<para>
Each column has a data type. The data type constrains the set of
possible values that can be assigned to a column and assigns
semantics to the data stored in the column so that it can be used
for computations. For instance, a column declared to be of a
numerical type will not accept arbitrary text strings, and the data
stored in such a column can be used for mathematical computations.
By contrast, a column declared to be of a character string type
will accept almost any kind of data but it does not lend itself to
mathematical calculations, although other operations such as string
concatenation are available.
</para>
<para>
<productname>PostgreSQL</productname> includes a sizable set of
built-in data types that fit many applications. Users can also
define their own data types. Most built-in data types have obvious
names and semantics, so we defer a detailed explanation to <xref
linkend="datatype"/>. Some of the frequently used data types are
<type>integer</type> for whole numbers, <type>numeric</type> for
possibly fractional numbers, <type>text</type> for character
strings, <type>date</type> for dates, <type>time</type> for
time-of-day values, and <type>timestamp</type> for values
containing both date and time.
</para>
<indexterm>
<primary>table</primary>
<secondary>creating</secondary>
</indexterm>
<para>
To create a table, you use the aptly named <xref
linkend="sql-createtable"/> command.
In this command you specify at least a name for the new table, the
names of the columns and the data type of each column. For
example:
<programlisting>
CREATE TABLE my_first_table (
first_column text,
second_column integer
);
</programlisting>
This creates a table named <literal>my_first_table</literal> with
two columns. The first column is named
<literal>first_column</literal> and has a data type of
<type>text</type>; the second column has the name
<literal>second_column</literal> and the type <type>integer</type>.
The table and column names follow the identifier syntax explained
in <xref linkend="sql-syntax-identifiers"/>. The type names