Oracle DDL Commands – Create Commands

Oracle Database Data Definition Language (DDL Statements) – Create Commands

To execute Oracle database commands, you need Oracle’s username, password and oracle instance name to connect to oracle database. If you haven’t got the username, contact database administrator (DBA) and get the relevant privileges. When you are working with a client, you may be provided a development schema to execute all of your database commands or privileges granted to work on all schemas depending upon your data modelling activity. All commands should be executed from sql> prompt and “;” indicates the end of an oracle command.

An Oracle database consists of DDL commands, which are useful to create, modify and drop the database objects. In this section, we will try to explain about important database CREATE commands that are used by a data modeler by relating it with our example data.

Create Table “GENDER_LKP”:

Create table statements are used for creating tables by which data is stored permanently in the database.

CREATE TABLE GENDER_LKP (

GNDR_CD VARCHAR2 (10) NOT NULL,

GNDR_DESC VARCHAR2 (50) NOT NULL,

DTTM_STMP DATE NOT NULL,

CONSTRAINT GENDER_LKP_PK PRIMARY KEY (GNDR_CD)

);

Create Table “DEPARTMENT_LKP”:

CREATE TABLE DEPARTMENT_LKP (

DPTMT_NUM NUMBER(2) NOT NULL,

DPTMT_DESC VARCHAR2(50) NOT NULL,

DTTM_STMP DATE NOT NULL,

CONSTRAINT DEPARTMENT_LKP_PK

PRIMARY KEY (DPTMT_NUM)

);

Create Table “EMPLOYEE_DTL”:

CREATE TABLE EMPLOYEE_DTL (

EMP_DTL_ID NUMBER NOT NULL,

GNDR_CD VARCHAR2(10) NOT NULL,

DPTMT_NUM NUMBER(2) NULL,

FRST_NM VARCHAR2(30) NOT NULL,

LST_NM VARCHAR2(30) NOT NULL,

MDLE_NM VARCHAR2(30) NULL,

BRTH_DT DATE NOT NULL,

SSN VARCHAR2(11) NOT NULL,

SLRY_AMT NUMBER(7,2) NOT NULL,

DTTM_STMP DATE NOT NULL,

CONSTRAINT EMPLOYEE_DTL_PK PRIMARY KEY (EMP_DTL_ID),

CONSTRAINT EMPLOYEE_DTL_FK01 FOREIGN KEY (DPTMT_NUM) REFERENCES DEPARTMENT_LKP,

CONSTRAINT EMPLOYEE_DTL_FK02 FOREIGN KEY (GNDR_CD) REFERENCES GENDER_LKP

); 

 How to copy a table with data?

As of now, table EMPLOYEE_DTL contains no data. As soon as you load the data into EMPLOYEE_DTL, try the following command.

CREATE TABLE EMPLOYEE_DTL_COPY AS SELECT * FROM EMPLOYEE_DTL;

How to copy a table with no data?

CREATE TABLE EMPLOYEE_DTL_COPY AS SELECT * FROM EMPLOYEE_DTL WHERE 1=2; 

How to create a Sequence?

This sequence is used to generate unique numbers for the column ‘EMP_DTL_ID’.

CREATE SEQUENCE SEQ_EMPLOYEE_DTL

INCREMENT BY 1

START WITH 1

NOMAXVALUE

NOMINVALUE

NOCACHE

NOCYCLE

NOORDER

;

How to create a Trigger?

Whenever a record is inserted into “EMPLOYEE_DTL” table, this trigger selects the next unique number from the sequence “SEQ_EMPLOYEE_DTL” and inserts into the column “EMP_DTL_ID”.

In our INSERT STATEMENTS example, we have not provided values for the column “EMP_DTL_ID” and inserting values into “EMP_DTL_ID is taken care by sequence and trigger.

CREATE OR REPLACE TRIGGER TRG_SEQ_EMPLOYEE_DTL

BEFORE INSERT ON EMPLOYEE_DTL

FOR EACH ROW

BEGIN

SELECT SEQ_EMPLOYEE_DTL.NEXTVAL INTO :NEW.EMP_DTL_ID FROM DUAL;

END;

How to create an Index?

CREATE INDEX IND_SSN ON EMPLOYEE_DTL(SSN); 

How to create a View?

CREATE VIEW VIEW_EMPLOYEE_DTL AS SELECT * FROM EMPLOYEE_DTL; 

How to create a Synonym?

CREATE SYNONYM SYN_EMPLOYEE_DTL FOR EMPLOYEE_DTL; 

Database: Sample Data Analysis

Database: Sample Data

With a simple example of designing a data model with ‘Employee related information’, most of the data structures can be easily understood. So we will try to design a data model using the sample data given below and will explain the data modeler’s involvement in the database environment.

Sample Source Data:

First Name Middle Name Last Name SSN Gender Birth Date Salary Dept. No Dept. Desc
Melinda J Schipper 765-43-21 Female 20-APR-1973 5000
Kevin A Schulte 123-45-67 Male 13-OCT-1969 5000 10 IT

Note: If you go through our topics provided under the Data Modelling Section, you will have a brief idea about how to design a data model.

Database: Sample Data Analysis

For the sample data provided in the above table, as a Data Modeler you have to design logical data model, physical data model and generate DDL scripts. In order to do the above tasks, you need to create the following:

  • Create 3 tables GENDER_LKP, DEPARTMENT_LKP, EMPLOYEE_DTL.
  • Assign correct data types to the columns.
  • Create constraints like primary key, unique key, null, not null, check to the columns.
  • Assign correct PHYSICAL names for tables and columns.
  • Select GENDER_LKP, DEPARTMENT_LKP as parent tables and EMPLOYEE_DTL as the child table and connect them.
  • Even though column ‘DTTM_STMP’ has not been present in the sample data, you have to add ‘DTTM_STMP’ to all tables to know the date and time on which a record is inserted or updated.

GENDER_LKP Analysis:

  • Since ‘male’ and ‘female’ values will be repeated for several records in EMPLOYEE_DTL table, you have to design this lookup for saving disk space. Sometimes the detail table may not show the exact business requirements also.
  • E.g. Since the example data contains both genders; you know that there are two genders. What would have happened if all records were male? By seeing the sample data, you might have come to a conclusion that there are only ‘males’ allowed as employees. By discussing with Business Analyst, you would come to know that both genders are allowed. For this purpose, you design a lookup table, which identifies the business requirements also.
  • You have to assign NOT NULL constraint for all columns since lookup tables in general should not have null value.
  • You have to create a column Gender Code, which is not present in sample data and this column should be assigned Primary Key to validate the detailed data in EMPLOYEE_DTL table.

Sample Source Data:

 

Entity Name Table

Name

Att

Name

Col

Name

Data

type

Null Option P Key F Key
Gender Lookup GENDER_LKP Gender Code GNDR_CD VARCHAR2(10) NOT NULL Yes No
Gender Lookup GENDER_LKP Gender Description GNDR_DESC VARCHAR2(50) NOT NULL No No
Gender Lookup GENDER_LKP DateTime Stamp DTTM_STMP DATE NOT NULL No No

Analysis of DEPARTMENT_LKP:

  • Since ‘Dept No’ and ‘Dept Desc’ values will be repeated for several records in EMPLOYEE_DTL table, you have to design this lookup for saving the disk space.
  • You have to assign NOT NULL constraint for all columns. Generally lookups should not have null value.
  • You have to assign ‘Dept No’ as the primary key to validate the detailed data in EMPLOYEE_DTL table.

Sample Source Data:

Entity Name Table

Name

Att

Name

Col

Name

Data

type

Null Optn P Key F Key
Department Lookup DEPART

MENT_LKP

Depart

ment Number

DPTMT_NUM NUMBER(2) NOT NULL Yes No
Department Lookup DEPART

MENT_LKP

Depart

ment Description

DPTMT_DESC VARCHAR2(50) NOT NULL No No
Department Lookup DEPART

MENT_LKP

DateTime Stamp DTTM_STMP DATE NOT NULL No No

Analysis of EMPLOYEE_DTL:

  • As of now, this example entity contains only sample of 2 records. In real time, this entity would store millions of records. In order to retrieve the data in a faster way, you have to create an additional column EMP_DTL_ID in EMPLOYEE_DTL and assign it as primary key.
  • In our example, column EMP_DTL_ID will be populated through sequence initiated by a trigger and you can see the sequence code and trigger code in later sections.
  • Even though SSN can be added as a primary key in EMPLOYEE_DTL table, you have to add a new column EMP_DTL_ID as the primary key to ensure the fastest retrieval of data.
  • Tip: Wherever it is required, you have to create new columns.

Sample Source Data:

Entity Name Table

Name

Att

Name

Col

Name

Data

type

Null Option P Key F Key
Employee Detail EMPLOYEE

_DTL

Employee Detail Identifier EMP_DTL_ID NUMBER NOT NULL Yes No
Employee Detail EMPLOYEE

_DTL

Department Number DPTMT_NUM NUMBER(2) NULL No Yes
Employee Detail EMPLOYEE

_DTL

Gender Code GNDR_CD VARCHAR2(10) NOT NULL No Yes
Employee Detail EMPLOYEE

_DTL

First Name FRST_NM VARCHAR2(30) NOT NULL No No
Employee Detail EMPLOYEE

_DTL

Last Name LST_NM VARCHAR2(30) NOT NULL No No
Employee Detail EMPLOYEE

_DTL

Middle Name MDLE_NM VARCHAR2(30) NULL No No
Employee Detail EMPLOYEE

_DTL

SSN SSN VARCHAR2(11) NOT NULL No No
Employee Detail EMPLOYEE

_DTL

Birth Date BRTH_DT DATE NOT NULL No No
Employee Detail EMPLOYEE

_DTL

Salary Amount SLRY_AMT NUMBER(7,2) NOT NULL No No
Employee Detail EMPLOYEE

_DTL

DateTime Stamp DTTM_STMP DATE NOT NULL No No

 

Oracle Database Objects Overview

Oracle Database Objects Overview:

A database can have many schemas; one schema can contain multiple database objects like tables, views, Synonym etc. A brief explanation on each of these Oracle database objects is given below. For more detailed explanations, please refer the official website of Oracle at www.oracle.com.

Schema:

This is also known as USER and is a collection of database objects and as a data modeler one should know how to login into a particular schema and to manage these database objects.

Table:

A set of related data, arranged in the form of rows and columns.

Column:

This is also known as Field that provides the structure for organizing the rows and contains the related information.

Data type:

This is set of property associated with a column, which helps to store and identify the type of data and its length.

Null:

This is a value that indicates that the column contains no valid data.

Not Null:

This is a constraint that indicates that the column should contain data.

Primary Key Constraint:

This is a constraint imposed on the column so that all values in the column should be different from each other. This constraint can be imposed on one column or group of columns. The primary key will be always used as a parent key when adding a referential constraint by connecting it to a child table.

  • Unique Constraint: Unique + Null Values
  • Primary Key Constraint: Unique + Not Null Values

Foreign Key Constraint:

This is a constraint imposed on the child table. Whatever values are present in the child table, their corresponding values should be present in the parent table. This constraint can be imposed on one column or group of columns and NULL values are allowed in the child table.

Unique Constraint:

This is a constraint imposed on a column so that all NON-NULL values in the column should be different from each other. This constraint can be imposed on one column or group of columns.

Check Constraint:

This is a constraint that is imposed to validate the data within some value or range of values. This constraint can be imposed on one column or group of columns.

Index:

Index is a database object that enables faster retrieval of data. Unique Index, Bitmap Index etc., are the different types of Index.

Sequence:

This is a database object that generates unique numbers.

View:

This is a PSEUDO table that is not stored in the database and it is just a query.

Materialized Views:

Materialized Views are similar to a view but these are permanently stored in the database and often refreshed. This is used in optimization for the faster data retrieval and is useful in aggregation and summation of data.

Synonym:

This is an alias name for the object in the database created with CREATE SYNONYM command.

Procedure:

This is a program that contains set of code, which will carry out a specific action when called by other programs.

Function:

This is a program that contains set of code, which will do a specific action when called by other programs.

Package:

This is a collection of procedures, functions, PL/SQL tables, etc., that contains set of code, which will do a specific action when called by other programs.

Trigger:

This is a program that contains set of code for doing some useful action when a record is inserted or deleted or updated in a table.