You are on page 1of 5

SQL STATEMENTS ARE CLASSIFIED INTO 5 SUBLANGUAGES BASED ON THEIR FUNCTIONALITY:

1. DDL(Data Definition language)


2. DML(Data Manipulation language)
3. DRL(Data Retrieval language)
4. TCL(Transaction Control Language)
5. DCL(Data Control Language USED BY DBA).
1. DDL: (DATA DEFINITION LANGUAGE)
----------
USED TO PERFORM STRUCTURE ORIENTED CHANGES.
STATEMENTS AVAILABLE ARE
1. CREATE 2.DROP 3. ALTER (keywords add , modify , drop , rename)
4. RENAME 5.TRUNCATE
1.create: This statement is used create new
----------- objects like table,view,synonym,etc.
Table is base object in rdbms.
Table is used to hold data.
syntax for creating tables:
----------------------------------
create table <tablename>(columnname1 datatype,...);
Maximum -> 1000 columns/table can be given.
open sqlplus tool:
-----------------------
select start/programs/oracle/application development/sql plus
username - scott
password - tiger
By default scott user is available in oracle used for testing purpose.
sql prompts open
Ex1:
sql>create table item_masters ( it_no number(3) ,
it_name varchar2(10) , qoh number(3) , rol number(3) ) ;
QOH -> QUANTITY ON HAND
ROL -> REORDER LEVEL
To check for table in database:
----------------------------------------
sql> desc item_masters;
(DESCRIBES THE STRUCTURE)
Ex2:
sql>create table item_trans(it_no number(3) ,
tran_type char(1) , qty number(3) , tran_date date) ;
sql>desc item_trans;
======================================================
2. drop: Used to remove existing objects.
----------
syn: drop table <tablename>;
ex:
sql>drop table item_trans;
sql>desc itemtrans;
======================================================
3. ALTER: STATEMENT USED TO CHANGE EXISTING
-------------- STRUCTURES USING KEYWORDS LIKE
ADD, MODIFY, DROP, RENAME.
ADD: USED TO ADD NEW COLUMNS IN EXISTING
------- STRUCTURES.
syntax: alter table <tablename> add(columnname1 datatype,..);
Ex1:
sql>alter table item_masters add uom varchar2(10);
sql>desc item_masters;
UOM -> UNIT OF MEASUREMENT
Ex2:
sql>alter table item_trans add updt char(1);
sql>desc item_trans;
updt -> update
--------------------------------------------------------------------------------
---------------
MODIFY: USED TO INCREASE / DECREASE WIDTH (or)
------------- change EXISTING DATATYPEs if that column is empty.
syntax: alter table <tabname> modify (colname1 datatype,..);
Ex1:
sql>alter table item_masters modify it_name varchar2(20);
sql>desc item_masters;
Ex2:
sql>alter table item_masters modify it_no varchar2(10);
sql>desc item_masters;
--------------------------------------------------------------------------------
---------------
DROP: USED TO REMOVE EXISTING COLUMNS along
--------- with data.
Syntax: alter table <tablename> drop column columnname;
Ex1:
SQL>ALTER TABLE ITEM_MASTERS DROP COLUMN UOM;
SQL>DESC ITEM_MASTERS1
note:We cannot drop multiple columns at a time.
--------------------------------------------------------------------------------
--------------
RENAME: USED TO CHANGE EXISTING COLUMNNAMEs.
--------------
syntax: alter table <tablename> rename column <oldcolname> to <newcolname>;
Ex1:
SQL>ALTER TABLE ITEM_MASTERS RENAME COLUMN
IT_NAME TO ITEMNAME;
SQL> DESC ITEM_MASTERS;
NOTE: We cannot rename multiple columns at a time.
======================================================
4. RENAME : Statement USED TO CHANGE existing objectname.
syntax: rename <oldobjectname> to <newobjname>;
Ex1:
SQL> RENAME ITEM_MASTERS TO ITEMMAT;
SQL> DESC ITEM_MASTERS --does not exists
SQL> DESC ITEMMAT --object exists.
--------------------------------------------------------------------------------
--------------
5. TRUNCATE : USED TO EMPTY/remove existing table data
-------------------- ( i.e data will be removed permanently,
but empty structure remains).
syntax: Truncate table <tablename>;
note: emp , dept tables given by manufacturer along
------- with data used for testing purpose.
To check tables:
---------------------
sql>desc dept --3 columns
sql>desc emp --8 columns
To check for data:
----------------------
sql>select * from dept; --4 rows
sql>select * from emp; --14 rows
Ex1:
SQL>TRUNCATE TABLE emp;
sql>desc emp; --3 columns
sql>select * from emp; --no rows selected
======================================================
2. DML: (DATA MANUPILATION LANGUAGE)
----------
USED TO PERFORM RECORD ORIENTED CHANGES.
STATEMENTS AVAILABLE ARE INSERT,UPDATE,DELETE.
1.INSERT: STATEMENT USED TO PROVIDE NEW RECORDS TO EXISTING TABLES.
CHARACTERS AND DATES SHOULD BE GIVEN WITHIN SINGLE QUOTES.
characters are case sensitive.
oracle statements are case insensitive.
SYNTAX:
-------------
INSERT INTO <TABLENAME>[(COLUMNNAMES)]
VALUES(LIST OF VALUES);
Insert can be used in 3 ways:
1. Inserting data to all columns.
2. Inserting data to specific columns.
3. Inserting data dynamically.
1. Inserting data to all columns:
----------------------------------------
SQL>SELECT * FROM DEPT;
SQL>INSERT INTO DEPT VALUES(50,'HR','HYD');
SQL>SELECT * FROM DEPT;
2. Inserting data to specific columns:
-----------------------------------------------
SQL>SELECT * FROM EMP;
SQL>INSERT INTO EMP(EMPNO,ENAME,JOB,HIREDATE,DEPTNO)
VALUES(100,'NAVEEN','MANAGER','01-JAN-01',20);
SQL>SELECT * FROM EMP;
3. Inserting data dynamically:
---------------------------------------
IT CAN BE DONE USING INSERTION OPERATOR -> & PROMPT.
SQL>INSERT INTO EMP(EMPNO,ENAME,SAL,DEPTNO)
VALUES(&eno,'&ename',&SAL,&DEPTNO);
Enter value for eno: 1003
Enter value for ename: SAI
Enter value for sal: 70000
Enter value for deptno: 30
SQL> /
(REEXECUTES LATEST STATEMENT IN BUFFER)
Enter value for eno: 1004
Enter value for ename: RAM
Enter value for sal: 40000
Enter value for deptno: 10
SQL>SELECT * FROM EMP;
--------------------------------------------------------------------------------
---------------
2.UPDATE: STATEMENT USED TO CHANGE EXISTING RECORDS.
SYNTAX:
-------------
UPDATE <TABLENAME> SET COLUMNNAME1 = VALUE , ... [WHERE <CONDITION>];
WHERE CLAUSE: USED TO PROVIDE CONDITIONS
-------------------------- ON REQUIRED RECORDS.
where clause will support for update,select,delete statements.
EXAMPLES:
-----------------
SQL>UPDATE EMP SET COMM = 5000 WHERE EMPNO=7902 or EMPNO = 7566 or empno = 7900;
SQL>SELECT * FROM EMP WHERE EMPNO=7902 or EMPNO = 7566 or empno = 7900;
sql>UPDATE EMP SET SAL=25000 , COMM = 5000;
SQL>SELECT * FROM EMP;
--------------------------------------------------------------------------------
---------------
3.DELETE: STATEMENT USED TO REMOVE REQUIRED RECORDS.
---------------
syntax:
-------
delete from <tablename> [where <condition>];
SQL>DELETE FROM EMP WHERE JOB = 'CLERK';
SQL>SELECT * FROM EMP;
1) ddl statements are permanent.
2) dml statements are temporary(transactions).
======================================================
3.TCL :(Transaction control language).
----------
Statements available are (commit & rollback).
commit : used to save the transacted data permanently in database.
rollback:used to cancel the transaction from memory.
sql>rollback;
sql>select * from emp;
SQL>DELETE FROM EMP WHERE JOB = 'CLERK';
sql>commit;
SQL>SELECT * FROM EMP;
note: when we exit,ddl,dcl then autocommit takes place.
savepoint : used to temporarily save the transaction.
======================================================

You might also like