Creating tables

Creating a table provides a logical place to store related data on a DB2® subsystem.

About this task

To create a table, use a CREATE TABLE statement that includes the following elements:
  • The name of the table
  • A list of the columns that make up the table. For each column, specify the following information:
    • The column's name (for example, SERIAL).
    • The data type and length attribute (for example, CHAR(8)).
    • Optionally, a default value.
    • Optionally, a referential constraint or check constraint.
    Separate each column description from the next with a comma, and enclose the entire list of column descriptions in parentheses.
Example: The following SQL statement creates a table named PRODUCT:
CREATE TABLE PRODUCT
  (SERIAL       CHAR(8)     NOT NULL,
   DESCRIPTION  VARCHAR(60) DEFAULT,
   MFGCOST      DECIMAL(8,2),
   MFGDEPT      CHAR(3),
   MARKUP       SMALLINT,
   SALESDEPT    CHAR(3),
   CURDATE      DATE        DEFAULT);

For more information about referential constraints, see Referential constraints

For more information about check constraints, see Check constraints.

Identifying column defaults and constraining column inputs:
If you want to constrain the input or identify the default of a column, you can use the following values:
  • NOT NULL, when the column cannot contain null values.
  • UNIQUE, when the value for each row must be unique, and the column cannot contain null values.
  • DEFAULT, when the column has one of the following DB2-assigned defaults:
    • For numeric columns, 0 (zero) is the default value.
    • For character or graphic fixed-length strings, blank is the default value.
    • For binary fixed-length strings, a set of hexadecimal zeros is the default value.
    • For variable-length strings, including LOB strings, the empty string (a string of zero-length) is the default value.
    • For datetime columns, the current value of the associated special register is the default value.
  • DEFAULT value, when you want to identify one of the following values as the default value:
    • A constant
    • NULL
    • SESSION_USER, which specifies the value of the SESSION_USER special register at the time when a default value is needed for the column
    • CURRENT SQLID, which specifies the value of the CURRENT SQLID special register at the time when a default value is needed for the column
    • The name of a cast function that casts a default value (of a built-in data type) to the distinct type of a column
Related reference:
CREATE TABLE

Related information