CREATE TABLE

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Synopsis

The CREATE TABLE statement creates a new table in a database. It defines the table name, column names and types, primary key, and table properties.

Syntax

Diagram

create_table

CREATE TABLE - 图1

table_element

CREATE TABLE - 图2

table_column

CREATE TABLE - 图3

column_constraint

CREATE TABLE - 图4

table_constraints

CREATE TABLE - 图5

column_list

CREATE TABLE - 图6

Grammar

  1. create_table ::= CREATE TABLE [ IF NOT EXISTS ] table_name
  2. '(' table_element [ ',' table_element ...] ')';
  3. table_element ::= table_column | table_constraints
  4. table_column ::= column_name column_type [ column_constraint ...]
  5. column_constraint ::= PRIMARY KEY
  6. table_constraints ::= PRIMARY KEY '(' column_list ')'
  7. column_list ::= column_name [ ',' column_name ...]

Where

  • table_name and column_name are identifiers (table_name can be a qualified name).

Semantics

  • An error is raised if table_name already exists in the specified database unless the IF NOT EXISTS option is used.

PRIMARY KEY

  • Primary key can be defined in either column_constraint or table_constraint but not in both of them.
  • Each row in a table is uniquely identified by its primary key.

See Also

DELETEDROP TABLEINSERTSELECTUPDATEOther PostgreSQL Statements