Creating base tables
When you create a table, Db2 records a definition of the table in the Db2 catalog.
Before you begin
Consider whether you want to create the table space and database for your table or let Db2 create them for you implicitly.
For more information, see Implementing Db2 table spaces.
Procedure
To create a base table that you designed:
Issue a CREATE TABLE statement that specifies the attributes of the table and its columns.
- Table name
- When choosing the name for the table, follow the naming conventions of your organization and the basic requirements described in Guidelines for table names.
- Column list
- For each column, specify the name and attributes of the column, including the data type, length attribute, and optional default values or value constraints. For more information, see Db2 table columns.
- Referential or check constraints (optional)
- For more information, see Check constraints and Referential constraints.
- Partitioning method (optional)
- Db2 uses size-based partitions by default if you do not specify how to partition the data when you create the table. For more information, see Partitioning data in Db2 tables.
- Table location (optional)
- You can specify an existing table space and database name as the location of the new table, or you can let Db2 create these objects for your table implicitly. For more information, see Implementing Db2 table spaces.
Example
The following CREATE TABLE statement creates the EMP table, which is in a database named MYDB and in a table space named MYTS:
CREATE TABLE EMP
(EMPNO CHAR(6) NOT NULL,
FIRSTNME VARCHAR(12) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
DEPT CHAR(3) ,
HIREDATE DATE ,
JOB CHAR(8) ,
EDL SMALLINT ,
SALARY DECIMAL(9,2) ,
COMM DECIMAL(9,2) ,
PRIMARY KEY (EMPNO))
IN MYDB.MYTS; What to do next
Creating a table does not store the application data. You can put data into the table by using several methods, such as the LOAD utility or the INSERT statement. For more information, see Loading data into Db2 tables.