Create Statement
Create Statement
The CREATE
statement is a powerful SQL command used to define and initialize new database objects, such as tables, indexes, views, and storage groups.
It creates the structure of database objects by specifying their attributes, data types, constraints, and other properties.
The CREATE
statement in DB2 is used to create new database objects, including:
- Tables: Define columns, data types, and constraints for storing data.
- Indexes: Optimize data retrieval.
- Views: Create virtual tables from one or more tables.
- Storage Groups: Define storage locations for tables and indexes.
Syntax -
CREATE TABLE table_name ( column_name1 data_type [NOT NULL] [DEFAULT default_value], column_name2 data_type [NOT NULL] [DEFAULT default_value], ... PRIMARY KEY (column_name1, column_name2), FOREIGN KEY (column_name3) REFERENCES other_table (other_column) );
- table_name: Name of the table.
- column_name1, column_name2, ...: Table columns with specified data types.
- NOT NULL: Ensures column must have a value.
- DEFAULT: Sets a default value.
- PRIMARY KEY: Defines unique row identifier.
- FOREIGN KEY: References a column in another table.
Examples - CREATE TABLE
CREATE TABLE EMPLOYEE ( EMP_ID INTEGER NOT NULL, EMP_NAME CHAR(30), DEPT_ID INTEGER, HIRE_DATE DATE DEFAULT CURRENT DATE, PRIMARY KEY (EMP_ID), FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT (DEPT_ID) );
Using CREATE Statements in a COBOL Program
We can't able to use the CREATE
statement in COBOL-DB2 program as it is a DDL statement.
Only administrators and other users who has ADIM authority can use the CREATE statement in DB2 tools (SPUFI or QMF).
CREATE can be used on the below list of objects -
CREATE ALIAS CREATE DATABASE CREATE FUNCTION CREATE INDEX CREATE PACKAGE CREATE PROCEDURE CREATE ROLE CREATE SEQUENCE CREATE STOGROUP CREATE SYNONYM CREATE TABLE CREATE TABLESPACE CREATE TRIGGER CREATE TYPE CREATE VIEW