1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter5.Data Definition</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql.html" title="PartII.The SQL Language">
<link rel="prev" href="sql-expressions.html" title="4.2.Value Expressions">
<link rel="next" href="ddl-default.html" title="5.2.Default Values">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="ddl">
<div class="titlepage"><div><div><h2 class="title">
<a name="ddl"></a>Chapter5.Data Definition</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="ddl.html#ddl-basics">5.1. Table Basics</a></span></dt>
<dt><span class="sect1"><a href="ddl-default.html">5.2. Default Values</a></span></dt>
<dt><span class="sect1"><a href="ddl-constraints.html">5.3. Constraints</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ddl-constraints.html#id574819">5.3.1. Check Constraints</a></span></dt>
<dt><span class="sect2"><a href="ddl-constraints.html#id575041">5.3.2. Not-Null Constraints</a></span></dt>
<dt><span class="sect2"><a href="ddl-constraints.html#id575183">5.3.3. Unique Constraints</a></span></dt>
<dt><span class="sect2"><a href="ddl-constraints.html#id575283">5.3.4. Primary Keys</a></span></dt>
<dt><span class="sect2"><a href="ddl-constraints.html#ddl-constraints-fk">5.3.5. Foreign Keys</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ddl-system-columns.html">5.4. System Columns</a></span></dt>
<dt><span class="sect1"><a href="ddl-alter.html">5.5. Modifying Tables</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ddl-alter.html#id576140">5.5.1. Adding a Column</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576204">5.5.2. Removing a Column</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576263">5.5.3. Adding a Constraint</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576308">5.5.4. Removing a Constraint</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576387">5.5.5. Changing a Column's Default Value</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576435">5.5.6. Changing a Column's Data Type</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576485">5.5.7. Renaming a Column</a></span></dt>
<dt><span class="sect2"><a href="ddl-alter.html#id576509">5.5.8. Renaming a Table</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ddl-priv.html">5.6. Privileges</a></span></dt>
<dt><span class="sect1"><a href="ddl-schemas.html">5.7. Schemas</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-create">5.7.1. Creating a Schema</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-public">5.7.2. The Public Schema</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-path">5.7.3. The Schema Search Path</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-priv">5.7.4. Schemas and Privileges</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-catalog">5.7.5. The System Catalog Schema</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-patterns">5.7.6. Usage Patterns</a></span></dt>
<dt><span class="sect2"><a href="ddl-schemas.html#ddl-schemas-portability">5.7.7. Portability</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ddl-inherit.html">5.8. Inheritance</a></span></dt>
<dd><dl><dt><span class="sect2"><a href="ddl-inherit.html#ddl-inherit-caveats">5.8.1. Caveats</a></span></dt></dl></dd>
<dt><span class="sect1"><a href="ddl-partitioning.html">5.9. Partitioning</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="ddl-partitioning.html#ddl-partitioning-overview">5.9.1. Overview</a></span></dt>
<dt><span class="sect2"><a href="ddl-partitioning.html#ddl-partitioning-implementation">5.9.2. Implementing Partitioning</a></span></dt>
<dt><span class="sect2"><a href="ddl-partitioning.html#ddl-partitioning-constraint-exclusion">5.9.3. Partitioning and Constraint Exclusion</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="ddl-others.html">5.10. Other Database Objects</a></span></dt>
<dt><span class="sect1"><a href="ddl-depend.html">5.11. Dependency Tracking</a></span></dt>
</dl>
</div>
<p> 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, views, functions, and triggers.
</p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="ddl-basics"></a>5.1.Table Basics</h2></div></div></div>
<a name="id574374"></a><a name="id574385"></a><a name="id574391"></a><p> 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 random order,
unless sorting is explicitly requested. This is covered in <a href="queries.html" title="Chapter7.Queries">Chapter7, <i>Queries</i></a>. 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.
</p>
<p> 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.
</p>
<p> <span class="productname">PostgreSQL</span> 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 <a href="datatype.html" title="Chapter8.Data Types">Chapter8, <i>Data Types</i></a>. Some of the frequently used data types are
<code class="type">integer</code> for whole numbers, <code class="type">numeric</code> for
possibly fractional numbers, <code class="type">text</code> for character
strings, <code class="type">date</code> for dates, <code class="type">time</code> for
time-of-day values, and <code class="type">timestamp</code> for values
containing both date and time.
</p>
<a name="id574470"></a><p> To create a table, you use the aptly named <code class="command">CREATE
TABLE</code> 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:
</p>
<pre class="programlisting">CREATE TABLE my_first_table (
first_column text,
second_column integer
);</pre>
<p>
This creates a table named <code class="literal">my_first_table</code> with
two columns. The first column is named
<code class="literal">first_column</code> and has a data type of
<code class="type">text</code>; the second column has the name
<code class="literal">second_column</code> and the type <code class="type">integer</code>.
The table and column names follow the identifier syntax explained
in <a href="sql-syntax.html#sql-syntax-identifiers" title="4.1.1.Identifiers and Key Words">Section4.1.1, “Identifiers and Key Words”</a>. The type names are
usually also identifiers, but there are some exceptions. Note that the
column list is comma-separated and surrounded by parentheses.
</p>
<p> Of course, the previous example was heavily contrived. Normally,
you would give names to your tables and columns that convey what
kind of data they store. So let's look at a more realistic
example:
</p>
<pre class="programlisting">CREATE TABLE products (
product_no integer,
name text,
price numeric
);</pre>
<p>
(The <code class="type">numeric</code> type can store fractional components, as
would be typical of monetary amounts.)
</p>
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Tip</h3>
<p> When you create many interrelated tables it is wise to choose a
consistent naming pattern for the tables and columns. For
instance, there is a choice of using singular or plural nouns for
table names, both of which are favored by some theorist or other.
</p>
</div>
<p> There is a limit on how many columns a table can contain.
Depending on the column types, it is between 250 and 1600.
However, defining a table with anywhere near this many columns is
highly unusual and often a questionable design.
</p>
<a name="id574576"></a><p> If you no longer need a table, you can remove it using the
<code class="command">DROP TABLE</code> command. For example:
</p>
<pre class="programlisting">DROP TABLE my_first_table;
DROP TABLE products;</pre>
<p>
Attempting to drop a table that does not exist is an error.
Nevertheless, it is common in SQL script files to unconditionally
try to drop each table before creating it, ignoring the error
messages.
</p>
<p> If you need to modify a table that already exists look into <a href="ddl-alter.html" title="5.5.Modifying Tables">Section5.5, “Modifying Tables”</a> later in this chapter.
</p>
<p> With the tools discussed so far you can create fully functional
tables. The remainder of this chapter is concerned with adding
features to the table definition to ensure data integrity,
security, or convenience. If you are eager to fill your tables with
data now you can skip ahead to <a href="dml.html" title="Chapter6.Data Manipulation">Chapter6, <i>Data Manipulation</i></a> and read the
rest of this chapter later.
</p>
</div>
</div></body>
</html>
|