Sophie

Sophie

distrib > Mandriva > 10.0 > i586 > by-pkgid > db7d48fed1469a51f3fb965d5b5b2ac1 > files > 101

postgresql-docs-7.4.1-2.5.100mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Data Definition</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 7.4.1 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="The SQL Language"
HREF="sql.html"><LINK
REL="PREVIOUS"
TITLE="Value Expressions"
HREF="sql-expressions.html"><LINK
REL="NEXT"
TITLE="System Columns"
HREF="ddl-system-columns.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
NAME="creation"
CONTENT="2003-12-22T03:48:47"></HEAD
><BODY
CLASS="CHAPTER"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 7.4.1 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-expressions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-syntax.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="dml.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="ddl-system-columns.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="DDL"
></A
>Chapter 5. Data Definition</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
>5.1. <A
HREF="ddl.html#DDL-BASICS"
>Table Basics</A
></DT
><DT
>5.2. <A
HREF="ddl-system-columns.html"
>System Columns</A
></DT
><DT
>5.3. <A
HREF="ddl-default.html"
>Default Values</A
></DT
><DT
>5.4. <A
HREF="ddl-constraints.html"
>Constraints</A
></DT
><DD
><DL
><DT
>5.4.1. <A
HREF="ddl-constraints.html#AEN1895"
>Check Constraints</A
></DT
><DT
>5.4.2. <A
HREF="ddl-constraints.html#AEN1924"
>Not-Null Constraints</A
></DT
><DT
>5.4.3. <A
HREF="ddl-constraints.html#AEN1950"
>Unique Constraints</A
></DT
><DT
>5.4.4. <A
HREF="ddl-constraints.html#AEN1972"
>Primary Keys</A
></DT
><DT
>5.4.5. <A
HREF="ddl-constraints.html#DDL-CONSTRAINTS-FK"
>Foreign Keys</A
></DT
></DL
></DD
><DT
>5.5. <A
HREF="ddl-inherit.html"
>Inheritance</A
></DT
><DT
>5.6. <A
HREF="ddl-alter.html"
>Modifying Tables</A
></DT
><DD
><DL
><DT
>5.6.1. <A
HREF="ddl-alter.html#AEN2115"
>Adding a Column</A
></DT
><DT
>5.6.2. <A
HREF="ddl-alter.html#AEN2124"
>Removing a Column</A
></DT
><DT
>5.6.3. <A
HREF="ddl-alter.html#AEN2131"
>Adding a Constraint</A
></DT
><DT
>5.6.4. <A
HREF="ddl-alter.html#AEN2140"
>Removing a Constraint</A
></DT
><DT
>5.6.5. <A
HREF="ddl-alter.html#AEN2153"
>Changing the Default</A
></DT
><DT
>5.6.6. <A
HREF="ddl-alter.html#AEN2162"
>Renaming a Column</A
></DT
><DT
>5.6.7. <A
HREF="ddl-alter.html#AEN2169"
>Renaming a Table</A
></DT
></DL
></DD
><DT
>5.7. <A
HREF="ddl-priv.html"
>Privileges</A
></DT
><DT
>5.8. <A
HREF="ddl-schemas.html"
>Schemas</A
></DT
><DD
><DL
><DT
>5.8.1. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-CREATE"
>Creating a Schema</A
></DT
><DT
>5.8.2. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PUBLIC"
>The Public Schema</A
></DT
><DT
>5.8.3. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PATH"
>The Schema Search Path</A
></DT
><DT
>5.8.4. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PRIV"
>Schemas and Privileges</A
></DT
><DT
>5.8.5. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-CATALOG"
>The System Catalog Schema</A
></DT
><DT
>5.8.6. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PATTERNS"
>Usage Patterns</A
></DT
><DT
>5.8.7. <A
HREF="ddl-schemas.html#DDL-SCHEMAS-PORTABILITY"
>Portability</A
></DT
></DL
></DD
><DT
>5.9. <A
HREF="ddl-others.html"
>Other Database Objects</A
></DT
><DT
>5.10. <A
HREF="ddl-depend.html"
>Dependency Tracking</A
></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 views, functions, and triggers.
 </P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="DDL-BASICS"
>5.1. Table Basics</A
></H1
><A
NAME="AEN1748"
></A
><A
NAME="AEN1750"
></A
><A
NAME="AEN1752"
></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"
>Chapter 7</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"
>Chapter 8</A
>.  Some of the frequently used data types are
   <TT
CLASS="TYPE"
>integer</TT
> for whole numbers, <TT
CLASS="TYPE"
>numeric</TT
> for
   possibly fractional numbers, <TT
CLASS="TYPE"
>text</TT
> for character
   strings, <TT
CLASS="TYPE"
>date</TT
> for dates, <TT
CLASS="TYPE"
>time</TT
> for
   time-of-day values, and <TT
CLASS="TYPE"
>timestamp</TT
> for values
   containing both date and time.
  </P
><A
NAME="AEN1766"
></A
><P
>   To create a table, you use the aptly named <TT
CLASS="LITERAL"
>CREATE
   TABLE</TT
> 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 <TT
CLASS="LITERAL"
>my_first_table</TT
> with
   two columns.  The first column is named
   <TT
CLASS="LITERAL"
>first_column</TT
> and has a data type of
   <TT
CLASS="TYPE"
>text</TT
>; the second column has the name
   <TT
CLASS="LITERAL"
>second_column</TT
> and the type <TT
CLASS="TYPE"
>integer</TT
>.
   The table and column names follow the identifier syntax explained
   in <A
HREF="sql-syntax.html#SQL-SYNTAX-IDENTIFIERS"
>Section 4.1.1</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 <TT
CLASS="TYPE"
>numeric</TT
> type can store fractional components, as
   would be typical of monetary amounts.)
  </P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>    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
></BLOCKQUOTE
></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="AEN1784"
></A
><P
>   If you no longer need a table, you can remove it using the
   <TT
CLASS="COMMAND"
>DROP TABLE</TT
> 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"
>Section 5.6</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"
>Chapter 6</A
> and read the
   rest of this chapter later.
  </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-expressions.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ddl-system-columns.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Value Expressions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>System Columns</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>