IBM Books

SQL Getting Started


Creating Tables

Create your own tables using the CREATE TABLE statement, specifying the column names and types, as well as constraints. Constraints are discussed in Enforcing Business Rules with Constraints and Triggers.

The following statement creates a table named PERS, which is similar to the STAFF table but has an additional column for date of birth.

     CREATE TABLE PERS
         ( ID              SMALLINT        NOT NULL,
           NAME            VARCHAR(9),
           DEPT            SMALLINT WITH DEFAULT 10,
           JOB             CHAR(5),
           YEARS           SMALLINT,
           SALARY          DECIMAL(7,2),
           COMM            DECIMAL(7,2),
           BIRTH_DATE      DATE) 

This statement creates a table with no data in it. The next section describes how to insert data into a new table.

As shown in the example, you specify both a name and a data type for each column. Data types are discussed in Data Types. NOT NULL is optional and may be specified to indicate that null values are not allowed in a column. Default values are also optional.

There are many other options you can specify in a CREATE TABLE statement, such as unique constraints or referential constraints. For more information about all of the options, see the CREATE TABLE statement in the SQL Reference.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]