Examples of DB2 DDL Create a table $ db2-> create table employee (ID SMALLINT NOT NULL, NAME VARCHAR(9), DEPT SMALLINT CHECK (DEPT BETWEEN 10 AND 100), JOB CHAR(5) CHECK (JOB IN ('Sales', 'Mgr', 'Clerk')), HIREDATE DATE, SALARY DECIMAL(7,2), COMM DECIMAL(7,2), PRIMARY KEY (ID), CONSTRAINT YEARSAL CHECK (YEAR(HIREDATE) > 1986 OR SALARY > 40500) ) A simple version: db2-> create table employee ( Empno smallint, Name varchar(30)) Create a schema If a user has SYSADM or DBADM authority, then the user can create a schema with any valid name. When a database is created, IMPLICIT_SCHEMA authority is granted to PUBLIC (that is, to all users). The following example creates a schema for an individual user with the authorization ID 'joe' CREATE SCHEMA joeschma AUTHORIZATION joe Create an alias The following SQL statement creates an alias WORKERS for the EMPLOYEE table: CREATE ALIAS WORKERS FOR EMPLOYEE You do not require special authority to create an alias, unless the alias is in a schema other than the one owned by your current authorization ID, in which case DBADM authority is required. Create an Index: The physical storage of rows in a base table is not ordered. When a row is inserted, it is placed in the most convenient storage location that can accommodate it. When searching for rows of a table that meet a particular selection condition and the table has no indexes, the entire table is scanned. An index optimizes data retrieval without performing a lengthy sequential search. The following SQL statement creates a non-unique index called LNAME from the LASTNAME column on the EMPLOYEE table, sorted in ascending order: CREATE INDEX LNAME ON EMPLOYEE (LASTNAME ASC) The following SQL statement creates a unique index on the phone number column: CREATE UNIQUE INDEX PH ON EMPLOYEE (PHONENO DESC) Drop a database: Db2 drop database sample Alter tablespace Adding a Container to a DMS Table Space You can increase the size of a DMS table space (that is, one created with the MANAGED BY DATABASE clause) by adding one or more containers to the table space. The following example illustrates how to add two new device containers (each with 40 000 pages) to a table space on a UNIX-based system: ALTER TABLESPACE RESOURCE ADD (DEVICE '/dev/rhd9' 10000, DEVICE '/dev/rhd10' 10000) The following SQL statement drops the table space ACCOUNTING: DROP TABLESPACE ACCOUNTING You can reuse the containers in an empty table space by dropping the table space but you must COMMIT the DROP TABLESPACE command, or have had AUTOCOMMIT on, before attempting to reuse the containers. The following SQL statement creates a new temporary table space called TEMPSPACE2: CREATE TEMPORARY TABLESPACE TEMPSPACE2 MANAGED BY SYSTEM USING ('d') Once TEMPSPACE2 is created, you can then drop the original temporary table space TEMPSPACE1 with the command: DROP TABLESPACE TEMPSPACE1 Add Columns to an Existing Table When a new column is added to an existing table, only the table description in the system catalog is modified, so access time to the table is not affected immediately. Existing records are not physically altered until they are modified using an UPDATE statement. When retrieving an existing row from the table, a null or default value is provided for the new column, depending on how the new column was defined. Columns that are added after a table is created cannot be defined as NOT NULL: they must be defined as either NOT NULL WITH DEFAULT or as nullable. Columns can be added with an SQL statement. The following statement uses the ALTER TABLE statement to add three columns to the EMPLOYEE table: ALTER TABLE EMPLOYEE ADD MIDINIT CHAR(1) NOT NULL WITH DEFAULT ADD HIREDATE DATE ADD WORKDEPT CHAR(3)