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 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 (
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 (
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 ); |
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; |
CREATE TABLE EMPLOYEE_DTL_COPY AS SELECT * FROM EMPLOYEE_DTL WHERE 1=2; |
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 ; |
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; |
CREATE INDEX IND_SSN ON EMPLOYEE_DTL(SSN); |
CREATE VIEW VIEW_EMPLOYEE_DTL AS SELECT * FROM EMPLOYEE_DTL; |
CREATE SYNONYM SYN_EMPLOYEE_DTL FOR EMPLOYEE_DTL; |
Learn data modeling design Skills on OLTP and OLAP from a US University Professor with…
These SQL commands are related with Oracle's data dictionary and can be used to get…
important DDL Statements from Oracle like Commit, Rollback, Grant, Revoke etc..
In this section, we will try to explain about important database DROP commands that are…
In this section, we will try to explain about important database DML commands that are…
In this section, we will try to explain about important database ALTER commands that are…