The following is the syntax for CREATE TABLE , which is described in detail in Table 4-1 .

  CREATE [ TEMPORARY | TEMP ] TABLE table_name (
         { column_name type [ column_constraint ]
         | table_constraint }
         [, ... ] ) 
         [ INHERITS ( inherited_table [, ... ] ) ]

Table 4-1. CREATE TABLE Syntax

Element

Description

CREATE 
[ TEMPORARY | TEMP ] 
TABLE

The TEMPORARY or TEMP SQL keyword causes the created table to automatically be destroyed upon the end of the active session to PostgreSQL. It may even have the same name as another existing table, and until it is destroyed, any references to that table name will reference the temporary table. Any indices placed on this table will also be temporary, and destroyed in the same fashion at the end of the session.

table_name (

The table_name is the identifier which your table will be named once created. The opening parenthesis symbol indicates the beginning of the column definitions.

{ column_name type 
[ column_constraint ]
| table_constraint }

Each table column and table constraint is defined within the parentheses following the table name, separated from one another by commas. Column definitions must contain a valid identifier for a name , followed by a valid data type , and may optionally include a column constraint . The requirements of column constraint definitions are dependent on the constraint, and described in the section called Constraints in Chapter 5 . Table constraints and columns may be mixed in this grouped list, though it is common practice to list columns first in the list, followed by any table constraints.

 [ , ... ] )

Each column definition may be followed by a comma in order to define a subsequent column after it. The ellipses denote that you may enter as many columns as you wish (up to the limit of 1600). Be sure that you do not follow the last column or constraint in the list with a comma, as is allowed in languages like Perl; this will cause a parsing error.

[ INHERITS ( 
inherited_table 
[ , ... ] )]

The Object-Relational capabilities of PostgreSQL allow for you to specify one or more tables (in a grouped, comma-delimited list) from which your table will inherit . This optional specification creates an implied parent-child relationship between tables. This relatively new technique to RDBMSs is discussed in more detail in the section called Inheritance in Chapter 5 .

Table 4-1 uses the terms column_constraint and table_constraint to refer to set of potentially complex constraint definitions. The syntax for these various constraints is listed in detail in the section called Constraints in Chapter 5 .