db2 Hello World; Create a table in DB2 called "employee"

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Example

The following statement will create a new table called employee:

CREATE TABLE EMPLOYEE (
       EMPNO      CHAR(6)        NOT NULL,
       FIRSTNME   VARCHAR(12)    NOT NULL,
       LASTNAME   VARCHAR(15)    NOT NULL,
       SALARY     DECIMAL(9,2)           ,
       PRIMARY KEY (EMPNO)      
       )

This will create a new table called employee. The table will have a primary key on EMPNO column. The first three columns cannot have a null value and they are text. The fourth one can have nulls and it is a number.

You can create this table from db2clp (Linux, UNIX, MacOS) like this (by surrounding the statement into quotes):

db2 "CREATE TABLE EMPLOYEE (
       EMPNO      CHAR(6)        NOT NULL,
       FIRSTNME   VARCHAR(12)    NOT NULL,
       LASTNAME   VARCHAR(15)    NOT NULL,
       SALARY     DECIMAL(9,2)           ,
       PRIMARY KEY (EMPNO)      
       )"

In Linux/UNIX, you can also escape the special characters with back-slash, but this could be more difficult to write:

db2    CREATE TABLE EMPLOYEE \( \
       EMPNO      CHAR\(6\)        NOT NULL, \
       FIRSTNME   VARCHAR\(12\)    NOT NULL, \
       LASTNAME   VARCHAR\(15\)    NOT NULL, \
       SALARY     DECIMAL\(9,2\)           , \
       PRIMARY KEY \(EMPNO\)                 \
       \)


Got any db2 Question?