Categories: DM & Database

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; 
learndmdwbi

Share
Published by
learndmdwbi

Recent Posts

Online Data Modeling Training

Learn data modeling design Skills on OLTP and OLAP from a US University Professor with…

7 months ago

Oracle’s Database Dictionary Views

These SQL commands are related with Oracle's data dictionary and can be used to get…

1 year ago

Oracle important DDL Statements

important DDL Statements from Oracle like Commit, Rollback, Grant, Revoke etc..

1 year ago

Oracle Database Data Definition Language(DDL Statements)- DROP Object Commands

In this section, we will try to explain about important database DROP commands that are…

1 year ago

Oracle Database Data Manipulation Language (DML) Commands

In this section, we will try to explain about important database DML commands that are…

1 year ago

Oracle Database Data Definition Language(DDL Statements) – ALTER Commands:

In this section, we will try to explain about important database ALTER commands that are…

1 year ago