You are on page 1of 8

What are the difference between DDL, DML and DCL commands?

Submitted by admin on Wed, 2004-08-04 13:49


DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:

o CREATE - to create objects in the database


o ALTER - alters the structure of the database
o DROP - delete objects from the database
o TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
o COMMENT - add comments to the data dictionary
o RENAME - rename an object

DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:

o SELECT - retrieve data from the a database


o INSERT - insert data into a table
o UPDATE - updates existing data within a table
o DELETE - deletes all records from a table, the space for the records remain
o MERGE - UPSERT operation (insert or update)
o CALL - call a PL/SQL or Java subprogram
o EXPLAIN PLAN - explain access path to data
o LOCK TABLE - control concurrency

DCL
Data Control Language (DCL) statements. Some examples:

o GRANT - gives user's access privileges to database


o REVOKE - withdraw access privileges given with the GRANT command

TCL
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be
grouped together into logical transactions.

o COMMIT - save work done


o SAVEPOINT - identify a point in a transaction to which you can later roll back
o ROLLBACK - restore database to original since the last COMMIT
o SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Aliases:
An alias in oracle is another name used for a column or a table inside SELECT query. They are accessible only
inside the query where they are created. Once the query execution is over alias will perish. An alias can be a
single word or can be more than one word. If alias used is more than one word it must be enclosed in double
quotes

example:
current_date or "current date"
Student_name or "Student name"
Aliases are used in places when ever there is multiple usage of longer table names.
example :
STUDENT_MASTER_RECORD can be aliased as SMR
ACCOUNT_MASTER_TABLE as AMT .
There are 2 types of aliases
i) Table alias : It is another name used to refer the table within the
query.
example :
SQL> SELECT ENAME , SAL FROM EMP E WHERE E.DEPTNO = 10;
In the above query "E" is an alias for EMP.

ii) column alias


SQL> SELECT SYSDATE "Current Date" FROM DUAL;
"Current Date" is an alias.
Working Wih Multiple Tables

To retrieve desired data from more than one table we use join.
We will have more than one table name after FROM key word in
SELECT statement. A SELECT statement on multiple tables without
a proper JOIN condition will lead to a cartesian product.(i.e.
No.Of Output rows = No.of rows in table1 X No of rows in table2....)

No of join conditions = No of tables joined - 1

Types Of Joins:

1) EQUI JOIN: The equi join is normally used to join tables with primary key foreign key relation ships.

Example :
SQL> SELECT ENAME,JOB,DEPTNO FROM EMP;

ENAME JOB DEPTNO


---------- --------- ----------
SMITH CLERK 20
ALLEN SALESMAN 30
WARD SALESMAN 30
JONES MANAGER 20
MARTIN SALESMAN 30
BLAKE MANAGER 30
CLARK MANAGER 10
SCOTT ANALYST 20
KING PRESIDENT 10
TURNER SALESMAN 30
ADAMS CLERK 20
JAMES CLERK 30
FORD ANALYST 20
MILLER CLERK 10
-------------------------------------

SQL> SELECT DEPTNO,DNAME,LOC FROM DEPT;

DEPTNO DNAME LOC ---- ----------- -------------------


10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
To display ENAME of EMP table ,DNAME of DEPT table we need to join those tables as shown below.

SQL> SELECT E.DEPTNO , ENAME , DNAME FROM EMP E, DEPT D


WHERE E.DEPTNO=D.DEPTNO;

DEPTNO ENAME DNAME


-------- ---------- --------------

10 CLARK ACCOUNTING
10 KING ACCOUNTING
10 MILLER ACCOUNTING
20 SMITH RESEARCH
20 ADAMS RESEARCH
20 FORD RESEARCH
20 SCOTT RESEARCH
20 JONES RESEARCH
30 ALLEN SALES
30 BLAKE SALES
30 MARTIN SALES
30 JAMES SALES
30 TURNER SALES
30 WARD SALES

In the above query "E" and "D" are the alias names for the tables EMP
and DEPT respectively. As column DEPTNO is there in both the tables, table
name is used along with column name DEPTNO. This is to avoid the ambiguity

2) NON-EQUI JOIN :
A join condition where any relation operator other than "=" equal to
operator is used.

Consider the following examples Using SALGRADE and EMP tables.

SQL> SELECT * FROM SALGRADE;

GRADE LOSAL HISAL


---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999

SQL> SELECT ENAME,SAL, GRADE FROM EMP E,SALGRADE S


WHERE SAL BETWEEN LOSAL AND HISAL

ENAME SAL GRADE


---------- ---------- ----------
SMITH 800 1
JAMES 950 1
ADAMS 1100 1
WARD 1250 2
MARTIN 1250 2
MILLER 1300 2
TURNER 1500 3
ALLEN 1600 3
CLARK 2450 4
BLAKE 2850 4
JONES 2975 4
SCOTT 3000 4
FORD 3000 4
KING 5000 5
3) OUTER JOIN :
In EQUI JOIN rows that does not satisfy specified condition would not be displayed. Example: DEPTNO 40 is
not displayed in the previous example
because there are no employees in it. If we want to diplay its detail also
then we have to use OUTER JOIN.Otherwise OUTER JOIN is imilar to EQUI JOIN
except for the difference it uses outer join (+) operator. (A plus within
parenthesis) towards the side not having required data. Outer join operator will substitute null values when
there are no values available.
SQL> SELECT E.DEPTNO,ENAME,DNAME FROM EMP E , DEPT D
2 WHERE E.DEPTNO (+) = D.DEPTNO;
DEPTNO ENAME DNAME
---------- ---------- --------------
10 CLARK ACCOUNTING
10 KING ACCOUNTING
10 MILLER ACCOUNTING
20 SMITH RESEARCH
20 ADAMS RESEARCH
20 FORD RESEARCH
20 SCOTT RESEARCH
20 JONES RESEARCH
30 ALLEN SALES
30 BLAKE SALES
30 MARTIN SALES
30 JAMES SALES
30 TURNER SALES
30 WARD SALES
OPERATIONS

SQL>SELECT D.DEPTNO,ENAME,DNAME FROM EMP E , DEPT D


WHERE E.DEPTNO (+) = D.DEPTNO

DEPTNO ENAME DNAME


---------- ---------- --------------
10 CLARK ACCOUNTING
10 KING ACCOUNTING
10 MILLER ACCOUNTING
20 SMITH RESEARCH
20 ADAMS RESEARCH
20 FORD RESEARCH
20 SCOTT RESEARCH
20 JONES RESEARCH
30 ALLEN SALES
30 BLAKE SALES
30 MARTIN SALES
30 JAMES SALES
30 TURNER SALES
30 WARD SALES
40 OPERATIONS
SQL> SELECT D.DEPTNO , NVL(ENAME, ' No employee') , DNAME
FROM EMP E,DEPT D
WHERE D.DEPTNO= E.DEPTNO(+);

DEPTNO NVL(ENAME,'N DNAME


------- ------------ --------------
10 CLARK ACCOUNTING
10 KING ACCOUNTING
10 MILLER ACCOUNTING
20 SMITH RESEARCH
20 ADAMS RESEARCH
20 FORD RESEARCH
20 SCOTT RESEARCH
20 JONES RESEARCH
30 ALLEN SALES
30 BLAKE SALES
30 MARTIN SALES
30 JAMES SALES
30 TURNER SALES
30 WARD SALES
40 No employee OPERATIONS

4) SELF JOIN:
When we join a table to itself it is called self join.To join a
table itself means that each row of the table is combined with itself
and with every other row of the table. The self join can be seen as join
of two copies of the same table.

SQL> SELECT E.ENAME,M.ENAME FROM EMP E,EMP


WHERE E.MGR=M.EMPNO;

ENAME ENAME
---------- ----------
SCOTT JONES
FORD JONES
ALLEN BLAKE
WARD BLAKE
JAMES BLAKE
TURNER BLAKE
MARTIN BLAKE
MILLER CLARK
ADAMS SCOTT
JONES KING
CLARK KING
BLAKE KING
SMITH FORD
Above query uses EMP table to display names of the employees and their managers

SQL> SELECT E.ENAME,NVL(M.ENAME,'He is managed by none') FROM EMP E,EMP M


WHERE E.MGR=M.EMPNO(+)
ENAME NVL(M.ENAME,'HEISMANA
---------- ---------------------
FORD JONES
SCOTT JONES
JAMES BLAKE
TURNER BLAKE
MARTIN BLAKE
WARD BLAKE
ALLEN BLAKE
MILLER CLARK
ADAMS SCOTT
CLARK KING
BLAKE KING
JONES KING
SMITH FORD
KING He is managed by none

SUBQUERY :
A query within another quey. A select statement whose output is substituted in the condition of another select
statement .(A query is a statement written for returning specific data). The subquery is executed only once. A
subquery is enclosed in parenthesis

Conside the following queries.

1) SELECT DEPTNO FROM EMP WHERE ENAME = 'SMITH';


2) SELECT ENAME FROM EMP WHERE DEPTNO= 20
These two queries can be combined as follows.
SQL> SELECT ENAME FROM EMP
WHERE DEPTNO = (SELECT DEPTNO FROM EMP
WHERE ENAME = 'SMITH');
The SELECT statement inside parenthesis is called subquery and the one which
uses the values returned by it as main query.
SQL> SELECT ENAME FROM EMP
WHERE JOB=( SELECT JOB FROM EMP
WHERE ENAME = 'SCOTT');
ENAME
----------
SCOTT
FORD

CORRELATED QUERY:
In a correlated subquery the table used in outer query refers to the table used in the inner query. The
correlated subquery is executed repeatedly once
for each row of the main query table.
Query to diplay name of highest salary taker.
SQL> SELECT EMPNO, ENAME FROM EMP A
WHERE 1 > ( SELECT COUNT(*) FROM EMP B
WHERE A.SAL < B.SAL)

EMPNO ENAME
-------- ----------
7839 KING

Query to diplay name of lowest salary taker.


SQL> SELECT EMPNO, ENAME,SAL FROM EMP A
WHERE 1 > ( SELECT COUNT(*) FROM EMP B
WHERE A.SAL > B.SAL)

EMPNO ENAME SAL


---------- ---------- ----------
7369 SMITH 800

SET OPERATORS
They combine results from two or more queries into one result.Data type
of all selected colums must be of same type.

UNION: It returns rows of first query plus rows of second query but avoids duplicates.(UNION ALL will give
duplicates also).

To display designations in department 10 and 20

SQL> SELECT JOB FROM EMP WHERE DEPTNO=10


UNION
SELECT JOB FROM EMP WHERE DEPTNO=20;

JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT

To display designations in department 10,20 and 30

SQL> SELECT JOB FROM EMP WHERE DEPTNO=10


UNION
SELECT JOB FROM EMP WHERE DEPTNO=20
UNION
SELECT JOB FROM EMP WHERE DEPTNO=30;

JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

Similar output can be produced by writing


SQL> SELECT DISTINCT JOB FROM EMP;

JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

INTERSECT: It returns rows that are common to all queries.

To diplay the designations that are common in DEPTNO 10 and 20 write.

SQL> SELECT JOB FROM EMP WHERE DEPTNO=10


INTERSECT
SELECT JOB FROM EMP WHERE DEPTNO=20;

JOB
---------
CLERK
MANAGER

MINUS : It returns rows unique to the first query.


To diplay designations unique in DEPTNO 10 we can write.

SQL> SELECT JOB FROM EMP WHERE DEPTNO=10


MINUS
SELECT JOB FROM EMP WHERE DEPTNO=20;

JOB
---------
PRESIDENT

SQL> SELECT JOB FROM EMP WHERE DEPTNO=10


MINUS
SELECT JOB FROM EMP WHERE DEPTNO=20
MINUS
SELECT JOB FROM EMP WHERE DEPTNO=30;

JOB
---------
PRESIDENT

You might also like