HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

INTRODUCTION TO SQL SQL (Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management. SQL is a programming language for querying and modifying data and managing databases. SQL was standardized first by the ANSI and (later) by the ISO. Most database management systems implement a majority of one of these standards and add their proprietary extensions. SQL allows the retrieval, insertion, updating, and deletion of data.

A database management system also includes management and administrative functions. Most -- if not all -- implementations also include a Command-line Interface (SQL/CLI) that allows for the entry and execution of the language commands, as opposed to only providing an API intended for access from a GUI. The first version of SQL was developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL, was designed to manipulate and retrieve data stored in IBM's original relational database product, System R. IBM patented their version of SQL in 1985, while the SQL language was not formally standardized until 1986, by the American National Standards Institute (ANSI) as SQL-86. Subsequent versions of the SQL standard have been released by ANSI and as International Organization for Standardization (ISO) standards.

pg. 1
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Originally designed as a declarative query and data manipulation language, variations of SQL have been created by SQL database management system (DBMS) vendors that add procedural constructs, control-of-flow statements, user-defined data types, and various other language extensions. With the release of the SQL:1999 standard, many such extensions were formally adopted as part of the SQL language via the SQL Persistent Stored Modules (SQL/PSM) portion of the standard. Common criticisms of SQL include a perceived lack of crossplatform portability between vendors, inappropriate handling of missing data (see Null (SQL)), and unnecessarily complex and occasionally ambiguous language grammar and semantics. FEATURES OF SQL: SQL is both an easy-to-understand language and a comprehensive tool for managing data. Some of the major features of SQL are · Vendor independence · Portability across computer systems · SQL standards · IBM endorsement and commitment (DB2) · Microsoft commitment (SQL Server , ODBC, and ADO) · Relational foundation · High-level, English-like structure · Interactive, ad hoc queries · Programmatic database access · Multiple views of data · Complete database language · Dynamic data definition · Client/server architecture · Enterprise application support · Extensibility and object technology · Internet database access pg. 2
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

· ·

Java integration (JDBC) Industry infrastructure

SQL COMMANDS SQL Consisting of DDL,DML,DCL,TCL COMMANDS. DDL Data Definition Language (DDL) statements are used to define the database structure or schema. DDL Commands: Create , Alter ,Drop , Rename, Truncate CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed RENAME - rename an object

DML Data Manipulation Language (DML) statements are used for managing data within schema objects DML Commands: Insert ,Update, Delete, Select

INSERT - insert data into a table UPDATE - updates existing data within a table pg. 3
Dept of Computer Science & Engineering

permissions. 4 Dept of Computer Science & Engineering . Revoke GRANT . It allows statements to be grouped together into logical transactions.restore database to original since the last COMMIT pg.save work done SAVEPOINT .gives user's access privileges to database REVOKE . Save point COMMIT .identify a point in a transaction to which you can later roll back ROLLBACK . and referential integrity as well it is used to control access to database by securing it.withdraw access privileges given with the GRANT command TCL Transaction Control (TCL) statements are used to manage the changes made by DML statements.retrieve data from the a database DCL Data Control Language (DCL) statements is used to create roles. TCL Commands: Commit.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT DELETE . Rollback.deletes all records from a table. DCL Commands: Grant. the space for the records remain SELECT .

To delete a column in a table ALTER TABLE table_name DROP COLUMN column_name. 5 Dept of Computer Science & Engineering . . TRUNCATE TABLE pg.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SYNTAX’S OF COMMANDS CREATE TABLE CREATE TABLE table_name ( column_name1 data_type. column_name3 data_type. ). column_name2 data_type.. DROP TABLE DROP TABLE table_name. ALTER A TABLE To add a column in a table ALTER TABLE table_name ADD column_name datatype...

value2. SELECT SELECT column_name(s) FROM table_name. UPDATE.INSERT . RENAME. column2. value3.... DELETE DELETE FROM table_name WHERE some_column=some_value..). column2=value2... column3. TRUNCATE. value2. UPDATE UPDATE table_name SET column1=value.. DELETE.). 6 Dept of Computer Science & Engineering .. INSERT INSERT INTO table_name VALUES (value1. CREATE. ( OR ) INSERT INTO table_name (column1. WHERE some_column=some_value... ON TABLES pg.) VALUES (value1..HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT TRUNCATE TABLE table_name. value3..

DOJ DATE). SQL> / Enter value for sno: 502 Enter value for sname: ASHOK Enter value for doj: 03-OCT-07 old 1: INSERT INTO STUDENT100 VALUES(&SNO.SNAME SQL> INSERT STUDENT100VALUES(&SNO.'&DOJ') new 1: INSERT INTO STUDENT100 VALUES(502.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>CREATE TABLE STUDENT(SNO VARCHAR2(15).'&SNAME'.'ASHOK'.'&DOJ') new 1: INSERT INTO STUDENT100 VALUES(501. 7 Dept of Computer Science & Engineering . OUTPUT:-TABLE CREATED NUMBER(5).'03-OCT07') pg.'&DOJ').'&SNAME'.'12-OCT-07') 1 row created.'ABI'. INTO OUTPUT:Enter value for sno: 501 Enter value for sname: ABI Enter value for doj: 12-OCT-07 old 1: INSERT INTO STUDENT100 VALUES(&SNO.'&SNAME'.

'BHAVYA'.'NIKHIL'. SQL> / Enter value for sno: 504 Enter value for sname: AKASH Enter value for doj: 05-OCT-07 old 1: INSERT INTO STUDENT100 VALUES(&SNO.'&SNAME'.'10OCT-07') 1 row created.'&DOJ') new 1: INSERT INTO STUDENT100 VALUES(505. 8 Dept of Computer Science & Engineering .'&SNAME'.'08-OCT07') pg.'&DOJ') new 1: INSERT INTO STUDENT100 VALUES(504.'&SNAME'.'AKASH'.'&DOJ') new 1: INSERT INTO STUDENT100 VALUES(503. SQL> / Enter value for sno: 503 Enter value for sname: BHAVYA Enter value for doj: 10-OCT-07 old 1: INSERT INTO STUDENT100 VALUES(&SNO. SQL> / Enter value for sno: 505 Enter value for sname: NIKHIL Enter value for doj: 08-OCT-07 old 1: INSERT INTO STUDENT100 VALUES(&SNO.'05-OCT07') 1 row created.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 row created.

OUTPUT:-Table altered. 9 Dept of Computer Science & Engineering . OUTPUT:SNO SNAME DOJ ---------. OUTPUT:-Table renamed.--------------. SQL>SELECT * FROM CSESTUDENT.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 row created. SQL> RENAME STUDENT TO CSESTUDENT.--------501 ABI 12-OCT-07 502 ASHOK 03-OCT-07 503 BHAVYA 10-OCT-07 504 AKASH 05-OCT-07 505 NIKHIL 08-OCT-07 SQL> UPDATE SNAME='ABI'. SQL> ALTER TABLE CSESTUDENT ADD BRANCH VARCHAR2(6). CSESTUDENT SET SNAME='ABILASH' WHERE OUTPUT:-1 row updated. pg.

CSESTUDENT SET BRANCH='CSE' WHERE OUTPUT:-1 row updated.--------. SQL> UPDATE SNO=503.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> UPDATE SNO=501. SQL> UPDATE SNO=504.-----BRANCH pg. SQL> SELECT * FROM CSESTUDENT. CSESTUDENT SET BRANCH='CSE' WHERE OUTPUT:-1 row updated. SQL> UPDATE SNO=502. CSESTUDENT SET BRANCH='CSE' WHERE OUTPUT:-1 row updated. SNO SNAME DOJ ---------.--------------. 10 Dept of Computer Science & Engineering . SQL> UPDATE SNO=505. CSESTUDENT SET BRANCH='CSE' WHERE OUTPUT:-1 row updated. CSESTUDENT SET BRANCH='CSE' WHERE OUTPUT:-1 row updated.

Table created.--------------. pg.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 501 502 503 504 505 ABILASH ASHOK BHAVYA AKASH NIKHIL 12-OCT-07 CSE 03-OCT-07 CSE 10-OCT-07 CSE 05-OCT-07 CSE 08-OCT-07 CSE SQL> DELETE FROM CSESTUDENT WHERE SNO=501.NAME VARCHAR2(10).BRANCH VARCHAR2(6)). SQL> SELECT * FROM CSESTUDENT. OUTPUT:-Table dropped. CREATING TABLES WITH CONSTRAINTS (NOT NULL) SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) NOT NULL. 11 Dept of Computer Science & Engineering . OUTPUT:-1 row deleted.--------. OUTPUT:SNO SNAME DOJ BRANCH ---------.-----502 ASHOK 03-OCT-07 CSE 503 BHAVYA 10-OCT-07 CSE 504 AKASH 05-OCT-07 CSE 505 NIKHIL 08-OCT-07 CSE 502 ASHOK 03-OCT-07 CSE SQL> DROP TABLE CSESTUDENT.

-------. SQL> / Enter value for rollno: 502 Enter value for name: ABI Enter value for branch: CSE old 1: INSERT INTO VALUES(&ROLLNO. 12 Dept of Computer Science & Engineering STUD .---------. Name Null? Type ----------------------------------------.'ABHILASH'.'&NAME'.'&NAME'.'CSE') 1 row created.'&BRANCH') new 1: INSERT INTO STUD VALUES(502.'&BRANCH').-----BRANCH pg.---------------------------ROLLNO NOT NULL NUMBER(6) NAME VARCHAR2(10) BRANCH VARCHAR2(6) SQL> INSERT INTO STUD VALUES(&ROLLNO. Enter value for rollno: 501 Enter value for name: ABHILASH Enter value for branch: CSE old 1: INSERT INTO STUD VALUES(&ROLLNO.'ABI'. ROLLNO NAME ---------.'&NAME'. SQL> SELECT * FROM STUD.'CSE') 1 row created.'&BRANCH') new 1: INSERT INTO STUD VALUES(501.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> DESC STUD.

13 Dept of Computer Science & Engineering STUD STUD . SQL> INSERT INTO VALUES(&ROLLNO.BRANCH VARCHAR2(6)).'CSE') * ERROR:CANNOT INSERT NULL INTO("SCOTT".'&NAME'.'&BRANCH').'BHAVYA'. Enter value for rollno: Enter value for name: BHAVYA Enter value for branch: CSE old 1: INSERT INTO STUD VALUES(&ROLLNO.'STUD'.ROLLNO) (UNIQUE) SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) UNIQUE .'&BRANCH') new 1: INSERT INTO STUD VALUES(501.'BHAVYA'.'&NAME'.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 501 ABHILASH CSE 502 ABI CSE SQL> INSERT INTO STUD VALUES(&ROLLNO.'CSE') INSERT INTO STUD VALUES(.'&NAME'.'abhilash'.'&NAME'.'cse') pg.NAME VARCHAR2(10). Enter value for rollno: 501 Enter value for name: abhilash Enter value for branch: cse old 1: INSERT INTO VALUES(&ROLLNO.'&BRANCH'). Table created.'&BRANCH') new 1: INSERT INTO STUD VALUES(.

'&NAME'.'BHAVYA'.'&BRANCH') new 1: INSERT INTO STUD VALUES(502.'&NAME'. SQL> / Enter value for rollno: 502 Enter value for name: ABI Enter value for branch: CSE old 1: INSERT INTO VALUES(&ROLLNO.BRANCH VARCHAR2(6)).'&BRANCH') new 1: INSERT INTO STUD VALUES(502.SYS_C001290) violated (PRIMARY KEY) SQL> CREATE TABLE STUD(ROLLNO NUMBER(6) PRIMARY KEY .'CSE') 1 row created. Table created.'CSE') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.'BHAVYA'.NAME VARCHAR2(10). pg. STUD SQL> / Enter value for rollno: 502 Enter value for name: BHAVYA Enter value for branch: CSE old 1: INSERT INTO STUD VALUES(&ROLLNO.'CSE') INSERT INTO STUD VALUES(502.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 row created.'ABI'. 14 Dept of Computer Science & Engineering .

'&BRANCH') new 1: INSERT INTO STUD VALUES(501.'BHAVYA'.'CSE') INSERT INTO STUD VALUES(502.'&NAME'.'&BRANCH'). STUD STUD STUD SQL> / Enter value for rollno: 502 Enter value for name: BHAVYA Enter value for branch: CSE old 1: INSERT INTO STUD VALUES(&ROLLNO.'ABI'.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> INSERT INTO VALUES(&ROLLNO.'CSE') 1 row created.'&BRANCH') new 1: INSERT INTO STUD VALUES(502.'&NAME'.'BHAVYA'.'cse') 1 row created. SQL> / Enter value for rollno: 502 Enter value for name: ABI Enter value for branch: CSE old 1: INSERT INTO VALUES(&ROLLNO. Enter value for rollno: 501 Enter value for name: abhilash Enter value for branch: cse old 1: INSERT INTO VALUES(&ROLLNO.SYS_C001290) violated pg.'&NAME'.'&BRANCH') new 1: INSERT INTO STUD VALUES(502.'abhilash'. 15 Dept of Computer Science & Engineering .'CSE') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.'&NAME'.

16 Dept of Computer Science & Engineering .HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> INSERT INTO STUD VALUES(&ROLLNO.'&NAME'. SQL> insert into stud values(&rno. Table created.-----501 ABHILASH CSE 502 ABI CSE (CHECK) SQL> create table stud(rno number(5).ROLLNO) SQL> SELECT * FROM STUD.name varchar2(10).'BHAVYA'.'STUD'. Enter value for rno: 567 pg.sal number(10) constraint no_ck check(sal between 10000 and 30000)).'&name'. ROLLNO NAME BRANCH ---------. Enter value for rollno: Enter value for name: BHAVYA Enter value for branch: CSE old 1: INSERT INTO STUD VALUES(&ROLLNO.'&BRANCH').'CSE') INSERT INTO STUD VALUES(.'CSE') * ERROR:CANNOT INSERT NULL INTO("SCOTT".'&NAME'.'&BRANCH') new 1: INSERT INTO STUD VALUES(.&sal).---------.'BHAVYA'.

pg.&per) new 1: insert into adm values(1.&sal) new 1: insert into stud values(565.29000) 1 row created.'abi'. 17 Dept of Computer Science & Engineering . Enter value for stuid: 1 Enter value for sname: abi Enter value for per: 80 old 1: insert into adm values(&stuid.80) 1 row created.sname varchar2(15).'&name'.per number(5)). SQL> insert into adm values(&stuid.&sal) new 1: insert into stud values(567.'sachin'.35000) insert into stud values(565.NO_CK) violated ( FOREIGN KEY ) SOL>create table adm(stuid number(6) constraint stuid_pk primary key.35000) * ERROR at line 1: ORA-02290: check constraint (SCOTT. Table created.'&name'.'&sname'.'rohit'.&per).HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT Enter value for name: sachin Enter value for sal: 29000 old 1: insert into stud values(&rno.'&sname'. SQL> / Enter value for rno: 565 Enter value for name: rohit Enter value for sal: 35000 old 1: insert into stud values(&rno.'rohit'.

'&sname'. SQL> select * from adm.&per) new 1: insert into adm values(3.89) 1 row created.---------1 abi 80 2 rohit 89 pg. SQL> / Enter value for stuid: 3 Enter value for sname: sachin Enter value for per: 99 old 1: insert into adm values(&stuid.'&sname'.--------------.&per) new 1: insert into adm values(2.'rohit'.99) 1 row created.'naveen'. 18 Dept of Computer Science & Engineering .&per) new 1: insert into adm values(4.'&sname'.70) 1 row created.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> / Enter value for stuid: 2 Enter value for sname: rohit Enter value for per: 89 old 1: insert into adm values(&stuid. SQL> / Enter value for stuid: 4 Enter value for sname: naveen Enter value for per: 70 old 1: insert into adm values(&stuid.'sachin'. STUID SNAME PER ---------.

Table created.'b') * ERROR at line 1: ORA-02291: integrity constraint (SCOTT.'cse'.sec varchar2(10)).'&sec') new 1: insert into course values(1. 19 Dept of Computer Science & Engineering .'&branch'. Enter value for stuid: 1 Enter value for branch: cse Enter value for sec: a old 1: insert into course values(&stuid.'cse'.parent key not found SQL> delete from adm where stuid=1.'&branch'.'a') 1 row created.branch varchar2(5).'&sec').'&branch'.'&sec') new 1: insert into course values(5. SQL> / Enter value for stuid: 5 Enter value for branch: cse Enter value for sec: b old 1: insert into course values(&stuid. SQL> insert into course values(&stuid.SID_FK) violated . * ERROR at line 1: pg.'cse'.'b') insert into course values(5.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 3 sachin 4 naveen 99 70 SQL> create table course(stuid number(6) constraint sid_fk references adm(stuid).

STUID SNAME PER ----.---------2 rohit 89 3 sachin 99 4 naveen 70 CREATING AND DROPING OF VIEWS SQL> select * from emp2. SQL> delete from adm where stuid=1. OUTPUT:ENAME STREET CITY -------------------. SQL>select * from adm.--------------. 1 row deleted. 20 Dept of Computer Science & Engineering . 1 row deleted. pg.SID_FK) violated .-------------------coyote toon hollywood rabbit tunnel carrot ville smith revolver death valley williams sea view sea attle SQL> create view emp22 as select * from emp2..HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT ORA-02292: integrity constraint (SCOTT.-------------------.child record found SQL> delete from course where stuid=1.

pg.-------------------. OUTPUT:ENAME STREET CITY -------------------.-------------------coyote toon hollywood rabbit tunnel carrot ville smith revolver death valley williams sea view sea attle SQL> update emp22 set city='hyd' where ename='coyote'. OUTPUT:1 row updated.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT OUTPUT:View created.-------------------. SQL> select * from emp22. SQL> select * from emp2.-------------------coyote toon hyd rabbit tunnel carrot ville smith revolver death valley williams sea view sea attle SQL> select * from emp22. 21 Dept of Computer Science & Engineering . OUTPUT:ENAME STREET CITY -------------------.

-------------------. OUTPUT:Table dropped.EMP22" has errors DCL AND TCL COMMANDS CREATING A USER SQL>CONNECT SYSTEM/MANAGER. 22 Dept of Computer Science & Engineering . SQL>CREATE USER "USERNAME" IDENTIFIED BY "PASSWORD" pg. SQL> select * from emp22.-------------------coyote toon hyd rabbit tunnel carrot ville smith revolver death valley williams sea view sea attle SQL> drop table emp2.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT OUTPUT:ENAME STREET CITY -------------------. OUTPUT:select * from emp22 * ERROR at line 1: ORA-04063: view "SCOTT.

OUTPUT:EID ---------1 2 3 4 5 6 7 8 ENAME ---------scotty wayne david max mark jeff matt steve ESAL EBRANCH -------------------6000 a 10000 b 7000 c 8000 b 7000 a 12000 a 10000 b 7000 c pg. SQL>CONNECT CSE2/CSECSE. SQL>CREATE USER CSE2 IDENTIFIED BY CSECSE.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>GRANT DBA TO "USERNAME" SQL>CONNECT "USERNAME"/"PASSWORD". IMPLEMENTING COMMINT. SQL>REVOKE DBA FROM CSE2. 23 Dept of Computer Science & Engineering . SQL>GRANT DBA TO CSE2. EXAMPLE CREATING A USER SQL>CONNECT SYSTEM/MANAGER. ROLLBACK SQL> select * from emp1. SAVEPOINT.

pg. SQL> update emp1 set esal=6500 where ename='scotty'.11000.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 8 rows selected. SQL> savepoint s1. SQL> savepoint s2.'c'). SQL> savepoint s3. 24 Dept of Computer Science & Engineering . OUTPUT:1 row deleted. OUTPUT:Savepoint created. SQL> insert into emp1 values(9. OUTPUT:Savepoint created. SQL> delete from emp1 where ename='wayne'.'bruce'. OUTPUT:Savepoint created. OUTPUT:1 row updated. OUTPUT:1 row created.

OUTPUT:Rollback complete. SQL> select * from emp1.---------------1 scotty 6500 a 3 david 7000 c 4 max 8000 b 5 mark 7000 a 6 jeff 12000 a 7 matt 10000 b 8 steve 7000 c pg. OUTPUT:EID ENAME ESAL EBRANCH ---------------------------. SQL> rollback to s3. OUTPUT:EID ---------1 3 4 5 6 7 8 9 ENAME ---------scotty david max mark jeff matt steve bruce ESAL EBRANCH ---------.---------------6500 a 7000 c 8000 b 7000 a 12000 a 10000 b 7000 c 11000 c 8 rows selected.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select * from emp1. 25 Dept of Computer Science & Engineering .

SQL> select * from emp1. OUTPUT:EID ENAME ---------. OUTPUT:Rollback complete.---------1 scotty 2 wayne 3 david 4 max 5 mark 6 jeff 7 matt 8 steve ESAL EBRANCH -------------------6500 a 10000 b 7000 c 8000 b 7000 a 12000 a 10000 b 7000 c SQL> rollback to s1.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> rollback to s2. OUTPUT:Rollback complete. 26 Dept of Computer Science & Engineering . OUTPUT:pg. SQL> select * from emp1.

27 Dept of Computer Science & Engineering . OUTPUT:EID ENAME ---------. OUTPUT:1 row deleted. SQL> select * from emp1.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT EID ENAME ---------. OUTPUT:Savepoint created. SQL> delete from emp1 where eid=1.---------1 scotty 2 wayne 3 david 4 max 5 mark 6 jeff 7 matt 8 steve ESAL EBRANCH -------------------6000 a 10000 b 7000 c 8000 b 7000 a 12000 a 10000 b 7000 c SQL> savepoint s4.---------2 wayne 3 david ESAL EBRANCH -------------------10000 b 7000 c pg.

---------2 wayne 3 david 4 max 5 mark 6 jeff 7 matt 8 steve 7 rows selected.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 4 max 5 mark 6 jeff 7 matt 8 steve 7 rows selected. 8000 7000 12000 10000 7000 b a a b c OUTPUT:* ERROR at line 1: ORA-01086 :savepoint 's4' never established SQL> select * from emp1. OUTPUT:Commit complete. SQL> commit. ESAL EBRANCH -------------------10000 b 7000 c 8000 b 7000 a 12000 a 10000 b 7000 c pg. OUTPUT:EID ENAME ---------. SQL> rollback to s4. 28 Dept of Computer Science & Engineering .

---------.--------. 29 Dept of Computer Science & Engineering .--------.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT DRL-DATA RETRIEVAL LANGUAGE IMPLEMENTING SELECT COMMANDS SQL> select * from emp. EMPNO ENAME JOB MGR HIREDATE COMM DEPTNO ---------.---------.---------.---------.---------7369 SMITH CLERK 7902 17-DEC-80 20 SAL 800 7499 ALLEN 300 30 SALESMAN 7698 20-FEB-81 1600 7521 WARD 500 30 7566 JONES 20 20 SALESMAN 7698 22-FEB-81 1250 MANAGER 7839 02-APR-81 2975 pg.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7654 MARTIN 1400 SALESMAN 7698 28-SEP-81 1250 7698 BLAKE 30 MANAGER 7839 01-MAY-81 2850 10 7782 CLARK MANAGER 7839 09-JUN-81 2450 7788 SCOTT 20 ANALYST 7566 19-APR-87 3000 7839 KING 10 PRESIDENT 17-NOV-81 5000 0 7844 TURNER 30 SALESMAN 7698 08-SEP-81 1500 7876 ADAMS 20 CLERK 7788 23-MAY-87 1100 7900 JAMES 30 CLERK 7698 03-DEC-81 950 pg. 30 Dept of Computer Science & Engineering .

---------.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7902 FORD 20 ANALYST 7566 03-DEC-81 3000 7934 MILLER 10 CLERK 7782 23-JAN-82 1300 14 rows selected.---------7900 JAMES 7902 FORD 7934 MILLER SAL 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 SAL 950 3000 1300 pg. EMPNO ENAME ---------. SQL> select empno. 31 Dept of Computer Science & Engineering .ename.sal from emp.---------7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS EMPNO ENAME ---------.---------.

EMPNO ENAME ---------. 32 Dept of Computer Science & Engineering .ename. SQL> select empno. ENAME JOB SAL DEPTNO ---------.deptno between 1500 and 5000.---------SMITH CLERK 800 20 WARD SALESMAN 1250 30 MARTIN SALESMAN 1250 30 ADAMS CLERK 1100 20 JAMES CLERK 950 30 MILLER CLERK 1300 10 6 rows selected.sal.---------.--------.job. SQL> select empno.sal from emp where sal between 2500 and 5000.---------7369 SMITH 7839 KING SAL 800 5000 pg.---------.---------.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 14 rows selected.5000).---------7566 JONES 7698 BLAKE 7788 SCOTT 7839 KING 7902 FORD SAL 2975 2850 3000 5000 3000 from emp where sal not SQL>select ename. EMPNO ENAME ---------.sal from emp where sal in (800.ename.

5000).sal in(800.---------.1250. SAL 1600 2975 2850 2450 1500 1100 950 1300 from emp where sal not SQL> select empno. EMPNO ENAME ---------.---------7369 SMITH 7566 JONES 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER SAL 800 2975 2850 2450 3000 5000 1100 950 3000 1300 pg.---------7499 ALLEN 7566 JONES 7698 BLAKE 7782 CLARK 7844 TURNER 7876 ADAMS 7900 JAMES 7934 MILLER 8 rows selected.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select empno.sal from emp where comm is null. EMPNO ENAME ---------. 33 Dept of Computer Science & Engineering .---------.ename.ename.3000.

---------.sal from emp where comm is not null. SQL> select empno. 34 job not Dept of Computer Science & Engineering .ename.ename.---------.sal from emp where ename like'S%'.job.---------7369 SMITH CLERK 7566 JONES MANAGER 7698 BLAKE MANAGER 7782 CLARK MANAGER 7788 SCOTT ANALYST 7839 KING PRESIDENT 7876 ADAMS CLERK SAL 800 2975 2850 2450 3000 5000 1100 pg.---------7369 SMITH CLERK 7788 SCOTT ANALYST SAL 800 3000 SQL> select empno.--------.---------7499 ALLEN 7521 WARD 7654 MARTIN 7844 TURNER SAL 1600 1250 1250 1500 SQL> select empno.sal from emp where like'S%'.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 10 rows selected.job. EMPNO ENAME JOB ---------. EMPNO ENAME ---------. EMPNO ENAME JOB ---------.---------.ename.--------.

--------. ENAME JOB ---------.---------JONES MANAGER BLAKE MANAGER SCOTT ANALYST KING PRESIDENT FORD ANALYST SAL 2975 2850 3000 5000 3000 SQL> select ename. pg. ENAME JOB ---------.sal from emp where sal>2500.---------SMITH CLERK ALLEN SALESMAN WARD SALESMAN MARTIN SALESMAN CLARK MANAGER TURNER SALESMAN ADAMS CLERK JAMES CLERK MILLER CLERK 9 rows selected.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7900 JAMES 7902 FORD 7934 MILLER 10 rows selected. CLERK ANALYST CLERK 950 3000 1300 SQL> select ename.sal from emp where sal<2500. 35 Dept of Computer Science & Engineering SAL 800 1600 1250 1250 2450 1500 1100 950 1300 .job.--------.job.

---------.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select empno.--------. EMPNO ENAME ---------.job.--------.--------7369 SMITH 7900 JAMES 7876 ADAMS 7521 WARD 7654 MARTIN 7934 MILLER 7844 TURNER 7499 ALLEN 7782 CLARK 7698 BLAKE 7566 JONES JOB SAL ---------CLERK 800 CLERK 950 CLERK 1100 SALESMAN 1250 SALESMAN 1250 CLERK 1300 SALESMAN 1500 SALESMAN 1600 MANAGER 2450 MANAGER 2850 MANAGER 2975 SAL 3000 3000 5000 EMPNO ENAME JOB ---------.sal from emp order by sal desc.---------. SQL> select empno. 36 Dept of Computer Science & Engineering .job.sal from emp order by sal. EMPNO ENAME JOB ---------.ename.ename.---------7788 SCOTT ANALYST 7902 FORD ANALYST 7839 KING PRESIDENT 14 rows selected.---------7839 KING PRESIDENT 7788 SCOTT ANALYST 7902 FORD ANALYST 7566 JONES MANAGER 7698 BLAKE MANAGER 7782 CLARK MANAGER SAL 5000 3000 3000 2975 2850 2450 pg.---------.

SUBQUERIES (APPLYING IN. ALL.UNION.MINUS) SQL>select * from emp. 37 Dept of Computer Science & Engineering MGR --------7902 HIREDATE ---------- 17-DEC-80 7698 20-FEB-81 7698 22-FEB-81 . ANY.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7499 7844 7934 7521 7654 ALLEN TURNER MILLER WARD MARTIN SALESMAN 1600 SALESMAN 1500 CLERK 1300 SALESMAN 1250 SALESMAN 1250 JOB ---------CLERK CLERK CLERK SAL 1100 950 800 EMPNO ENAME ---------.INTERSECT. NOT EXISTS.---------.--------7876 ADAMS 7900 JAMES 7369 SMITH 14 rows selected. OUTPUT:EMPNO ENAME JOB SAL COMM DEPTNO --------------------------------------------------7369 SMITH CLERK 800 20 7499 1600 7521 1250 ALLEN SALESMAN 300 30 WARD SALESMAN 500 30 pg. EXISTS.

38 Dept of Computer Science & Engineering .HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7566 2975 7654 1250 7698 2850 7782 2450 7788 3000 7839 5000 7844 1500 7876 1100 7900 950 7902 3000 JONES 20 MARTIN 1400 BLAKE MANAGER 7839 02-APR-81 SALESMAN 30 MANAGER 30 MANAGER 7698 28-SEP-81 7839 01-MAY-81 CLARK 10 SCOTT 7839 09-JUN-81 ANALYST 20 PRESIDENT 10 SALESMAN 30 CLERK 20 CLERK 30 ANALYST 20 CLERK 10 7566 19-APR-87 KING 17-NOV-81 TURNER 0 ADAMS 7698 08-SEP-81 7788 23-MAY-87 JAMES 7698 03-DEC-81 FORD 7566 03-DEC-81 7934 MILLER 1300 7782 23-JAN-82 pg.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>select * from dept.5000). OUTPUT:- EMPNO ENAME JOB SAL COMM DEPTNO ------------------------------. OUTPUT:DEPTNO DNAME ----------------------10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS LOC -----------NEW YORK DALLAS CHICAGO BOSTON SQL> select * from emp where sal in(3000.---------------------7788 3000 7839 5000 7902 3000 SCOTT KING FORD ANALYST 20 PRESIDENT 10 ANALYST 20 pg. 39 MGR ---------- HIREDATE ------------- 7566 19-APR-87 17-NOV-81 7566 03-DEC-81 Dept of Computer Science & Engineering .

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select empno. 40 Dept of Computer Science & Engineering .job.ename from emp where sal in(select max(sal) from emp group by deptno). OUTPUT:EMPNO ENAME ------------------7839 KING SQL> select empno.ename from emp where sal in(select max(sal) from emp).sal from emp where sal>all(select avg(sal) from emp group by deptno). OUTPUT:EMPNO ENAME ------------------7698 BLAKE 7788 SCOTT 7902 FORD 7839 KING SQL> select empno.ename. OUTPUT:EMPNO ENAME ------------------7566 JONES 7788 SCOTT 7839 KING 7902 FORD JOB --------MANAGER ANALYST PRESIDENT ANALYST SAL ---------2975 3000 5000 3000 pg.

---------.-----------------7369 SMITH CLERK 800 7521 WARD SALESMAN 1250 7654 MARTIN SALESMAN 1250 7844 TURNER SALESMAN 1500 7876 ADAMS CLERK 1100 7900 JAMES CLERK 950 7934 MILLER CLERK 1300 7 rows selected.ename.job.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select empno.sal from emp where sal<all(select avg(sal) from emp group by deptno).--------ALLEN SALESMAN WARD SALESMAN JONES MANAGER MARTIN SALESMAN BLAKE MANAGER CLARK MANAGER SCOTT ANALYST KING PRESIDENT TURNER SALESMAN ADAMS CLERK SAL ---------1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 pg. SQL> select ename. OUTPUT:EMPNO ENAME JOB SAL ---------.job. OUTPUT:ENAME JOB ---------.sal from emp where sal>any(select sal from emp where job='CLERK'). 41 Dept of Computer Science & Engineering .

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT JAMES CLERK 950 SAL ---------3000 1300 ENAME JOB ---------.job.sal from emp where sal=any(select sal from emp where job='CLERK'). OUTPUT:ENAME JOB ---------.sal from emp where sal<any(select sal from emp where job='CLERK'). SQL> select ename. 42 Dept of Computer Science & Engineering . OUTPUT:ENAME JOB -----------------SMITH CLERK JAMES CLERK ADAMS CLERK MILLER CLERK SAL ---------800 950 1100 1300 pg.--------FORD ANALYST MILLER CLERK 13 rows selected.--------SMITH CLERK WARD SALESMAN MARTIN SALESMAN ADAMS CLERK JAMES CLERK SAL ---------800 1250 1250 1100 950 SQL> select ename.job.

deptno=e.rating number(5).deptno).'&sname'.deptno=e.age number(10)).7.dname from dept d where not exists(select * from emp e where d.&age) new 1: insert into s1 values(22.'&sname'.deptno). SQL> insert into s1 values(&sid.sname varchar2(10). OUTPUT:DEPTNO ---------40 DNAME -------------OPERATIONS SQL> create table s1(sid number(5). OUTPUT:DEPTNO DNAME ----------------------10 ACCOUNTING 20 RESEARCH 30 SALES SQL> select deptno.'dustin'. Enter value for sid: 22 Enter value for sname: dustin Enter value for rating: 7 Enter value for age: 45 old 1: insert into s1 values(&sid. 43 Dept of Computer Science & Engineering .45) pg. Table created.&age).&rating.&rating.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select deptno.dname from dept d where exists(select * from emp e where d.

'rusty'.'&sname'. Enter value for sid: 28 Enter value for sname: yuppy Enter value for rating: 9 Enter value for age: 25 old 1: insert into s2 values(&sid.&rating.8. SQL> create table s2(sid number(5).&age) new 1: insert into s2 values(28.56) 1 row created.'lubber'. SQL> / Enter value for sid: 31 Enter value for sname: lubber Enter value for rating: 8 Enter value for age: 56 old 1: insert into s1 values(&sid.'&sname'.9.'yuppy'.'&sname'. Table created.25) pg.10.&rating.&rating.&age) new 1: insert into s1 values(31.'&sname'.&rating.age number(10)).HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 row created.sname varchar2(10). SQL> insert into s2 values(&sid.rating number(5). SQL> / Enter value for sid: 58 Enter value for sname: rusty Enter value for rating: 10 Enter value for age: 35 old 1: insert into s1 values(&sid.35) 1 row created.&age). 44 Dept of Computer Science & Engineering .&age) new 1: insert into s1 values(58.

pg. SQL> / Enter value for sid: 44 Enter value for sname: guppy Enter value for rating: 5 Enter value for age: 35 old 1: insert into s2 values(&sid.35) 1 row created.'lubber'.35) 1 row created.'guppy'.&rating.55) 1 row created. SQL> select * from s2. 45 Dept of Computer Science & Engineering .&rating. SQL> / Enter value for sid: 31 Enter value for sname: lubber Enter value for rating: 8 Enter value for age: 55 old 1: insert into s2 values(&sid.&age) new 1: insert into s2 values(44.'rusty'.'&sname'.8. SQL> / Enter value for sid: 58 Enter value for sname: rusty Enter value for rating: 10 Enter value for age: 35 old 1: insert into s2 values(&sid.&rating.10.5.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 row created.&age) new 1: insert into s2 values(58.&age) new 1: insert into s2 values(31.'&sname'.'&sname'.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

SID SNAME RATING ---------- ---------- ---------- ---------28 yuppy 9 25 31 lubber 8 55 44 guppy 5 35 58 rusty 10 35

AGE

SQL> select sname from s1 union select sname from s2; SNAME ---------dustin guppy lubber rusty yuppy SQL> select sname from s1 union select sname from s2; SNAME ---------dustin lubber rusty yuppy lubber guppy rusty 7 rows selected. SQL> select sid from s1 intersect select sid from s2; pg. 46
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

SID ---------31 58 SQL> select age from s1 minus select age from s2; AGE ---------45 56

FUNCTIONS: AGGREGATE FUNCTIONS SQL> SELECT * FROM EMP;

output:ENO ENAME JOB ---------- ---------- ---------- ---------1001 STEVE SALESMAN 1002 ADAM CLERK 1003 EVE MANAGER 1004 JAMES DIRECTOR SAL 1500 1000 5220 7000

SQL> SELECT COUNT(*)FROM EMP; output:pg. 47
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

COUNT(*) ---------4

SQL> SELECT SUM(SAL) FROM EMP; output:SUM(SAL) ---------14720

SQL> SELECT AVG(SAL) FROM EMP; output:AVG(SAL) ---------3680

SQL> SELECT MAX(SAL) FROM EMP;

output:MAX(SAL) ---------7000 pg. 48
Dept of Computer Science & Engineering

--------.---------7369 SMITH CLERK 800 20 7499 ALLEN SALESMAN 1600 30 7521 WARD SALESMAN 1250 30 7566 JONES MANAGER 2975 20 7654 MARTIN SALESMAN 1250 30 7698 BLAKE MANAGER 2850 30 7782 CLARK MANAGER 2450 10 7788 SCOTT ANALYST 3000 20 7839 KING PRESIDENT 5000 10 7844 TURNER SALESMAN 1500 30 7876 ADAMS CLERK 1100 20 pg. output:MIN(SAL) ---------1000 SQL>select * from emp.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> SELECT MIN(SAL) FROM EMP.---------.---------. 49 Dept of Computer Science & Engineering . OUTPUT:- EMPNO ENAME JOB SAL DEPTNO ---------.

CLERK ANALYST CLERK 950 3000 1300 30 20 10 APPLYING BY GROUP BY --SQL> select deptno. OUTPUT:DEPTNO ---------10 20 30 MAX(SAL) ---------5000 3000 2850 SQL> select deptno.max(sal) from emp group by deptno having max(sal)<3000. 50 Dept of Computer Science & Engineering . OUTPUT:pg. OUTPUT:DEPTNO ---------10 20 30 MIN(SAL) ---------1300 800 950 SQL> select deptno.max(sal) from emp group by deptno.min(sal) from emp group by deptno.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 7900 JAMES 7902 FORD 7934 MILLER 14 rows selected.

OUTPUT:DEPTNO COUNT(*) ------------------10 3 20 5 30 6 3 rows selected.'rn')from dual. OUTPUT:lxv pg.'RN')from dual. CONVERSION FUNCTIONS(TO_CHAR) SQL>select to_char(65.min(sal) from emp group by deptno having min(sal)>1000.count(*) from emp group by deptno.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT DEPTNO ---------30 MAX(SAL) ---------2850 SQL>select deptno. OUTPUT:DEPTNO MIN(SAL) ------------------10 1300 SQL> select ename. 51 Dept of Computer Science & Engineering . OUTPUT:LXV SQL>select to_char(65.

OUTPUT:1.000 SELECT TO_CHAR(2234.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>select to_char(58.'L9999'.'9g99g999d999')from dual.456.23.'MONTH')FROM DUAL.'s9999')from dual.456 SQL> select to_char(1234.'9g99g999')from dual.'l9999')from dual. OUTPUT:TWO THOUSAND NINE SQL> select to_char(123456. OUTPUT:-100 SQL> select to_char(41. OUTPUT:RS2234 pg. OUTPUT:JANUARY SQL> SELECT TO_CHAR(SYSDATE. 52 Dept of Computer Science & Engineering .'XXXX')from dual.'NLS_CURRENCY=RS')FROM DUAL.'XXXX')from dual. OUTPUT:MONDAY SQL> SELECT TO_CHAR(SYSDATE.'YEAR')FROM DUAL.'s9999')from dual. OUTPUT:+58 SQL>select to_char(-100.'day')from dual. OUTPUT:A SQL> select to_char(sysdate. OUTPUT:$1234 SQL> select to_char(123456. OUTPUT:1. OUTPUT:29 SQL> select to_char(10.23.

’S’)FROM DUAL. pg.’CORPORATION’)FROM DUAL. OUTPUT:-MITHSS SQL>SELECT RTRIM(‘SSMITHSS’.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT STRING FUNCTIONS SQL>SELECT CONCAT(‘ORACLE’.’*’)FROM DUAL. OUTPUT:-ORACLECORPORATION SQL>SELECT LPAD(‘ORACLE’.15.15. OUTPUT:-DBMS SQL>SELECT INITCAP(‘ORACLE’.’CORPORATION’)FROM DUAL. OUTPUT:-SSMITH SQL>SELECT LOWER(‘DBMS’)FROM DUAL.’S’)FROM DUAL. OUTPUT:-ORACLE********* SQL>SELECT LTRIM(‘SSMITHSS’. OUTPUT:-dbms SQL>SELECT UPPER(‘dbms’)FROM DUAL. OUTPUT:-*********ORACLE SQL>SELECT RPAD(‘ORACLE’.’*’)FROM DUAL. 53 Dept of Computer Science & Engineering .

3. OUTPUT:-05-JAN-09 SQL>SELECT ADD_MONTHS(SYSDATE. 54 Dept of Computer Science & Engineering .2)FROM DUAL.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT OUTPUT:-Oracle Corporation SQL>SELECT LENGTH(‘DATABASE’)FROM DUAL.'OR'. OUTPUT:-8 SQL>SELECT SUBSTR(‘ABCDEFGHIJ’3.2)FROM DUAL. OUTPUT:-28-FEB-09 SQL>SELECT LAST_DAY(SYSDATE)FROM DUAL. OUTPUT:-31-DEC-08 pg. OUTPUT:-CDEF SQL>SELECT INSTR('CORPORATE FLOOR'.’WED’)FROM DUAL.4)FROM DUAL. OUTPUT:-29-DEC-08 SQL>SELECT NEXT_DAY(SYSDATE. OUTPUT:-14 DATE FUNCTIONS SQL>SELECT SYSDATE FROM DUAL.

OUTPUT:-10-JAN-07 SQL>SELECT GREATEST('10-JAN-07'.'DAY')FROM DUAL.HIREDATE)FROM SQL>SELECT LEAST('10-JAN-07'.'12-OCT-07')FROM DUAL. OUTPUT:-01-JAN-08 SQL>SELECT ROUND(SYSDATE.'12-OCT-07')FROM DUAL. OUTPUT:-01-DEC-08 SQL>SELECT TRUNC(SYSDATE. OUTPUT:-28-DEC-08 pg. OUTPUT:- MONTHS_BETWEEN(SYSDATE. OUTPUT:-10-JAN-07 SQL>SELECT TRUNC(SYSDATE.'DAY')FROM DUAL.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>SELECT EMP. 55 Dept of Computer Science & Engineering .'YEAR')FROM DUAL. OUTPUT:-28-DEC-08 SQL>SELECT TRUNC(SYSDATE.'MONTH')FROM DUAL.

ceil(12.23) -----------11 SQL> select ceil(16.87) -----------12 FLOOR(11.87).36). OUTPUT:-01-JAN-09 NUMBER FUNCTIONS SQL> select round(12. 56 Dept of Computer Science & Engineering .23). floor(11. OUTPUT:-01-JAN-09 SQL>SELECT ROUND(SYSDATE.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL>SELECT ROUND(SYSDATE.'MONTH')FROM DUAL.63) from dual. round(14. OUTPUT:ROUND(12. OUTPUT:pg.78) from dual.63) -----------15 SQL> select floor(12. OUTPUT:FLOOR(12.36) -----------12 ROUND(14.23) from dual.'YEAR')FROM DUAL.

4) from dual.63) from dual. OUTPUT:TRUNC(56.78) ----------13 SQL> select trunc(56.4) ---------3 SQL> select power(2.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT CEIL(16. 57 Dept of Computer Science & Engineering . OUTPUT:POWER(2. OUTPUT:SIGN(0) ---------SIGN(34) ---------SIGN(-56) ---------pg.3) ---------8 SQL> select sign(0).3) from dual. OUTPUT:MOD(11.63) -----------56 SQL> select mod(11.sign(-56) from dual.23) ----------17 CEIL(12.sign(34).

the CLIENT and the SERVER. 58 Dept of Computer Science & Engineering . PL/SQL can be used for both server-side and Client side Development. PL/SQL has syntax and rules that determine how programming statements work together. OUTPUT:ABS(12) ABS(-89) ------------------12 89 SQL> select sqrt(25) from dual. PL/SQL is a part of the ORACLE RDBMS and hence can reside in two environments. Either in CLIENT/SERVER environments any PL/SQL Block or the PL/SQL Engine processes Subroutine.abs(-89) from dual. Any MODULE that is developed using PL/SQL can be moved easily between SERVER SIDE and CLIENT SIDE applications. PL/SQL is not a stand alone Programming Language. pg.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 0 1 -1 SQL> select abs(12). OUTPUT:SQRT(25) -------5 INTRODUCTION TO PL/SQL PL/SQL stands for PROCEDURAL Language Extensions to SQL. PL/SQL extends SQL by adding programming structures and subroutines available in any high level language.

constants.the entire block is executed on the CLIENT SIDE.the whole PL/SQL block is passed to the PL/SQL Engine on the ORACLE SERVER..in PL/SQL BEGIN --SQL executable statements --PL/SQL executable statements EXCEPTION pg.are sent to the ORACLE SERVER for further processing. As per the necessity the PL/SQL Engine can be located either at SERVER CLIENT When PL/SQL Engine is locted upon the SERVER.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT PL/SQL Engine is a special component that processes and executes any PL/SQL statements and sends any SQL statement to the SQL statement processor. If the PL/SQL block does not contain any SQL statements. PL/SQL BLOCK DECLARE --Declarations of memory variables. 59 Dept of Computer Science & Engineering .cursors etc. The SQL statement processes are always located on the ORACLE SERVER. When the PL/SQL Engine is located upon the CLIENT.All SQL statenents that are embedded within the PL/SQL block.the PL/SQL processing is done on the CLIENT SIDE.

END IF. SYNTAX's of CONTROL STATEMENTS in PL/SQL 1.ELSE IF SIMPLE IF IF condition THEN statement1. IF-THEN-ELSE STATEMENT IF condition THEN pg.ELSIF 3. BRANCHING 2.Simple IF 2. statement2. LOOPING BRANCHING STATEMENTS 1. SELECTION 3.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT /*SQL or PL/SQL code to handle errors that may arise during the execution of the code block between BEGIN and EXCEPTION section END. 60 Dept of Computer Science & Engineering .

ELSIF condition2 THEN statement2. ELSE statementn. ELSE statement2.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT statement1. NESTED IF IF condition THEN statement1. END IF. 61 Dept of Computer Science & Engineering . ELSE statement3. END IF. END IF. ELSIF condition3 THEN statement3. ELSE IF condition THEN statement2. pg. ELSIF STATEMENTS IF condition1 THEN statement1. END IF. ELSE statement3.

SELECTION IN PL/SQL SIMPLE CASE CASE SELECTOR WHEN Expr1 THEN statement1. : : : ELSE statementn. 62 Dept of Computer Science & Engineering .HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT END IF. END CASE. SEARCHED CASE CASE WHEN searchcondition1 THEN statement1. : : : ELSE pg. WHEN searchcondition2 THEN statement2. WHEN Expr2 THEN statement2.

UpperBound LOOP statement1. statement2.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT statementn. END LOOP. 63 Dept of Computer Science & Engineering . END CASE. statement2. FOR LOOP FOR counter IN [REVERSE] LowerBound. END LOOP. END LOOP. pg. ITERATIONS IN PL/SQL SIMPLE LOOP LOOP statement1.. EXIT [ WHEN Condition]. WHILE LOOP WHILE condition LOOP statement1.

new 5: a:=5. dbms_output. 64 Dept of Computer Science & Engineering . THE PREV VALUES OF A AND B WERE pg.put_line(b). end. OUTPUT: SQL> @ SWAPPING. dbms_output. b:=a-b. b number(10).put_line(b). dbms_output. dbms_output. Enter value for b: 3 old 6: b:=&b.put_line('THE PREV VALUES OF A AND B WERE'). new 6: b:=3. a:=a-b. dbms_output. begin a:=&a.put_line(a). dbms_output.put_line('THE VALUES OF A AND B ARE').HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT WRITE A PL/SQL PROGRAM TO SWAP TWO NUMBERS WITH OUT TAKING THIRD VARIABLE declare a number(10). b:=&b.SQL 17 / Enter value for a: 5 old 5: a:=&a. a:=a+b.put_line(a).

end. a:=&a. c:=a. a:=b.put_line('THE VALUES OF A AND B ARE'). b:=&b.put_line('THE PREV VALUES OF A AND B WERE'). WRITE A PL/SQL PROGRAM TO SWAP TWO NUMBERS BY TAKING THIRD VARIABLE declare a number(10). pg. dbms_output.put_line(b).put_line(a). begin dbms_output.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 5 3 THE VALUES OF A AND B ARE 3 5 PL/SQL procedure successfully completed.put_line(a).put_line(b). dbms_output. dbms_output. 65 Dept of Computer Science & Engineering . dbms_output. b:=c. c number(10). b number(10). dbms_output.

new 7: b:=3. b number. pg. Enter value for b: 3 old 7: b:=&b.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT OUTPUT: SQL> @ SWAPPING2. new 6: a:=5. begin a:=&a.SQL 19 / Enter value for a: 5 old 6: a:=&a. WRITE A PL/SQL PROGRAM TO FIND THE LARGEST OF TWO NUMBERS declare a number. 66 Dept of Computer Science & Engineering . b:=&b. THE PREV VALUES OF A AND B WERE 5 3 THE VALUES OF A AND B ARE 3 5 PL/SQL procedure successfully completed.

put_line('BOTH ARE EQUAL').put_line('B IS GREATER'). A IS GREATER PL/SQL procedure successfully completed. new 5: a:=5. elsif a>b then dbms_output.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT if a=b then dbms_output. end if.put_line('A IS GREATER'). Enter value for b: 2 old 6: b:=&b. pg.sql 13 / Enter value for a: 5 old 5: a:=&a. 67 Dept of Computer Science & Engineering . end. OUTPUT: SQL> @ GREATESTOF2. new 6: b:=2. else dbms_output.

68 Dept of Computer Science & Engineering . pg. b number. else dbms_output. if a=b and b=c and c=a then dbms_output.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT WRITE A PL/SQL PROGRAM TO FIND THE LARGEST OF THREE NUMBERS declare a number. c number.put_line('ALL ARE EQUAL'). end. OUTPUT: SQL> @ GREATESTOF3. new 6: a:=8.put_line('A IS GREATER'). end if.put_line('C IS GREATER'). elsif a>b and a>c then dbms_output. new 7: b:=9. c:=&c.put_line('B IS GREATER'). Enter value for b: 9 old 7: b:=&b. elsif b>c then dbms_output. b:=&b.sql 17 / Enter value for a: 8 old 6: a:=&a. begin a:=&a.

ppl number(10). dbms number(10). co number(10). per number(10). se:=&se. WRITE A PL/SQL PROGRAM TO FIND THE TOTAL AND AVERAGE OF 6 SUBJECTS AND DISPLAY THE GRADE declare java number(10). pg. avgs number(10). es:=&es. new 8: c:=7. dbms:=&dbms.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT Enter value for c: 7 old 8: c:=&c.put_line('ENTER THE MARKS'). B IS GREATER PL/SQL procedure successfully completed. es number(10). java:=&java. se number(10). begin dbms_output. co:=&co. total number(10). 69 Dept of Computer Science & Engineering .

new 13: dbms:=70. elsif per>55 and per<65 then dbms_output. Enter value for co: 89 old 14: co:=&co. else dbms_output. if per>75 then dbms_output. dbms_output.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT ppl:=&ppl. end.put_line('TOTAL IS '||total). Enter value for dbms: 70 old 13: dbms:=&dbms. end if.put_line('GRADE A').put_line('FAIL'). pg. dbms_output. 70 Dept of Computer Science & Engineering . per:=(total/600)*100. new 14: co:=89. total:=(java+dbms+co+se+es+ppl).put_line('INVALID INPUT'). new 12: java:=80.put_line('GRADE B'). Enter value for se: 72 old 15: se:=&se.put_line('PERCENTAGE IS '||per). elsif per>65 and per<75 then dbms_output.put_line('GRADE C'). if java<40 or dbms<40 or co<40 or se<40 or es<40 or ppl<40 then dbms_output.sql 31 / Enter value for java: 80 old 12: java:=&java. OUTPUT: SQL> @ GRADE.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

new 15: se:=72; Enter value for es: 76 old 16: es:=&es; new 16: es:=76; Enter value for ppl: 71 old 17: ppl:=&ppl; new 17: ppl:=71; GRADE A PERCENTAGE IS 76 TOTAL IS 458 PL/SQL procedure successfully completed.

pg. 71
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

WRITE A PL/SQL PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS AN ARMSTRONG NUMBER OR NOT

declare a number; t number; arm number; d number; begin a:=&a; t:=a; arm:=0; while t>0 loop d:=mod(t,10); arm:=arm+power(d,3); t:=trunc(t/10); end loop; if arm=a then dbms_output.put_line('given no is an armstrong no'|| a); else dbms_output.put_line('given no is not an armstrong no'); end if; end; OUTPUT: SQL> @ ARMSTRONGNUM.sql Enter value for a: 407 old 7: a:=&a; new 7: a:=407; given no is an armstrong no407 pg. 72
Dept of Computer Science & Engineering

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT

PL/SQL procedure successfully completed. SQL> / Enter value for a: 406 old 7: a:=&a; new 7: a:=406; given no is not an armstrong no PL/SQL procedure successfully completed.

WRITE A PL/SQL PROGRAM TO FIND THE SUM OF DIGITS IN A GIVEN NUMBER

declare a number; d number:=0; sum1 number:=0; begin a:=&a; while a>0 loop d:=mod(a,10); sum1:=sum1+d; a:=trunc(a/10); end loop; dbms_output.put_line('sum is'|| sum1); end; OUTPUT: SQL> @ SUMOFDIGITS.sql 16 / pg. 73
Dept of Computer Science & Engineering

rev number. WRITE A PL/SQL PROGRAM TO DISPLAY THE NUMBER IN REVERSE ORDER declare a number.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT Enter value for a: 564 old 7: a:=&a. new 7: a:=564. pg. d number. 74 Dept of Computer Science & Engineering . sum is15 PL/SQL procedure successfully completed.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT begin a:=&a. a:=trunc(a/10).put_line('no is'|| rev). rev:=0. rev:=(rev*10)+d. dbms_output. new 6: a:=536. end loop. OUTPUT: SQL> @ REVERSE2. no is635 PL/SQL procedure successfully completed. pg. while a>0 loop d:=mod(a. end.sql 16 / Enter value for a: 536 old 6: a:=&a.10). 75 Dept of Computer Science & Engineering .

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT WRITE A PL/SQL PROGRAM TO DISPLAY NUMBER IN REVERSE ORDER USING STRING FUNCTION declare gn varchar2(5):=4567. for i in reverse 1. end loop. dbms_output. end.. begin sl:=length(gn). sl number(2).put_line('given no r is'||gn).1).sl loop rv:=rv||substr(gn. dbms_output. 76 Dept of Computer Science & Engineering .i.sql 14 / given no r is4567 given no in reverse order is7654 pg. rv varchar2(5).put_line('given no in reverse order is'||rv). OUTPUT: SQL> @ REVERSE.

i number.. else dbms_output.put_line(a ||'is not a prime number'). pg. begin a:=&a. for i in 1.put_line(a ||'is a prime number'). end loop. end if. end. 77 Dept of Computer Science & Engineering . c number:=0. end if.i)=0 then c:=c+1.a loop if mod(a. WRITE A PL/SQL PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR NOT declare a number.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT PL/SQL procedure successfully completed. if c=2 then dbms_output.

. 78 Dept of Computer Science & Engineering .HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT OUTPUT: SQL> @ PRIME.SQL 19 / Enter value for a: 11 old 6: a:=&a. f number:=1. begin n:=&n. WRITE A PL/SQL PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER declare n number. for i in 1.n loop pg. 11is a prime number PL/SQL procedure successfully completed. new 6: a:=11.

new 5: n:=5. the factorial is120 PL/SQL procedure successfully completed. OUTPUT: SQL> @ FACTORIAL.sql 12 / Enter value for n: 5 old 5: n:=&n. end loop. end.put_line('the factorial is'|| f).HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT f:=f*i. dbms_output. 79 Dept of Computer Science & Engineering . pg.

a:=b. i number. c number. 0 pg. end loop. dbms_output. for i in 1. begin n:=&n. b:=c..put_line(c). new 8: n:=5. 80 Dept of Computer Science & Engineering . OUTPUT: SQL> @ FIBONACCI. a:=0.sql 21 / Enter value for n: 5 old 8: n:=&n.put_line(b).n-2 loop c:=a+b. n number. end. b number. b:=1.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT WRITE A PL/SQL PROGRAM TO GENERATE FIBONACCI SERIES declare a number. dbms_output. dbms_output.put_line(a).

CONSISTING OF TWO COLUMNS RADIUS & AREA TABLE NAME:AREAS RADIUS AREA SQL> create table areas(radius number(10). STORE THE RADIUS AND THE CORRESPONDING VALUES OF CALCULATED AREA IN AN EMPTY TABLE NAMED AREAS . 81 Dept of Computer Science & Engineering . Table created. --PROGRAM declare pi constant number(4. pg. WRITE A PL/SQL CODE BLOCK TO CALCULATE THE AREA OF A CIRCLE FOR A VALUE OF RADIUS VARYING FROM 3 TO 7.14.2):=3.2)).HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 1 1 2 3 PL/SQL procedure successfully completed. radius number(5):=3.area number(6.

SQL> insert into stud values('&sname'.26 4 50.04 WRITE A PL/SQL CODE BLOCK THAT WILL ACCEPT AN ACCOUNT NUMBER FROM THE USER.cur_bal number(10). SQL> create table acct(name varchar2(10). 82 Dept of Computer Science & Engineering . begin while radius<7 loop area:=pi*power(radius.acctno number(6. radius:=radius+1.area).&rollno.SQL 13 / PL/SQL procedure successfully completed. SQL> SELECT * FROM AREAS.100/FROM THE BALANCE.2)).HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT area number(6.ONLY THEN DEDUCT RS. end loop.2). end. pg.THIS PROCESS IS FIRED ON THE ACCT TABLE.24 5 78. insert into areas values(radius.CHECK IF THE USERS BALANCE IS LESS THAN MINIMUM BALANCE. OUTPUT: SQL> @ AREAOFCIRCLE.2).5 6 113.&marks). RADIUS AREA ------------------3 28.

ACCTNO NAME ------------------777 sirius 765 john 855 sam 353 peter --PROGRAM declare mano number(5). if mcb<minibal then update acct set cur_bal=cur_bal-fine where acctno=mano. fine number(6. new 7: mano:=855. CUR_BAL ---------10000 1000 500 800 OUTPUT: SQL> @ BANKACC. PL/SQL procedure successfully completed.00.00. end if.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select * from acct. minibal constant number(7.2). begin mano:=&mano.sql 13 / Enter value for mano: 855 old 7: mano:=&mano.2):=1000. pg. end. mcb number(6.2):=100. select cur_bal into mcb from acct where acctno=mano. 83 Dept of Computer Science & Engineering .

84 Dept of Computer Science & Engineering .HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> select * from acct. ACCTNO ---------777 765 855 353 NAME ---------sirius john sam peter CUR_BAL ---------10000 1000 400 800 pg.

FUNCTIONS CUBE create or replace function cube(n number) return number as c number. CUBE(6) -----216 pg. return c.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT PL/SQL PROGRAMS. 85 Dept of Computer Science & Engineering . begin c:=n*n*n. output:SQL> @cub / Function created. end. SQL> select cube(6)from dual.

output:pg. return fac. begin for i in 1.n loop fac:=fac*i. end. end loop..HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT FACTORIAL create or replace function fact(n number)return number as fac number:=1. 86 Dept of Computer Science & Engineering .

depname varchar2. 87 Dept of Computer Science & Engineering . 12 / Function created. FACT(5) ---------120 IN.PROCEDURE create or replace procedure inpro(dno number.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> @func. SQL> select fact(5) from dual.sql.city varchar2) as pg.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT begin insert into dept (deptno. 88 Dept of Computer Science & Engineering .depname.dname.'MARKETING'.-------------. SQL> exec inpro(50. SQL> select * from dept. DEPTNO DNAME LOC ---------.-------------. PL/SQL procedure successfully completed. OUTPUT:SQL> select * from dept.------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON SQL> @pro1 / Procedure created. DEPTNO DNAME LOC ---------.city).loc)values(dno.------------10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON pg.'HYDERABAD'). end.

5 begin 6 ch:=&ch. 4 ch number. 9 end. 7 outpro(ch. 89 Dept of Computer Science & Engineering .loc into depname.city from dept where deptno=dno.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT 50 MARKETING HYDERABAD OUT-PROCEDURE create or replace procedure outpro(dno number. OUTPUT: SQL> @proc2 / Procedure created. SQL> declare 2 x varchar2(20).put_line(x || ' ' || y). 10 / pg.y).x. 8 dbms_output.depname out varchar2.city out varchar2) as begin select dname. end. 3 y varchar2(30).

if we='sat' or we='sun' then raise_application_error(-20015. 90 Dept of Computer Science & Engineering . ACCOUNTING NEW YORK PL/SQL procedure successfully completed. BEFORE TRIGGER create or replace trigger tday before insert or delete or update on emp declare we varchar2(10). output:pg. end if.'its a weekend'). begin we:=to_char(sysdate. end. new 6: ch:=10.'dy').HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT Enter value for ch: 10 old 6: ch:=&ch.

HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT SQL> @trig1 / Trigger created. line 6 ORA-04088: error during execution of trigger 'SCOTT. ERROR at line 1: ORA-20015: its a weekend ORA-06512: at "SCOTT. SQL> delete from emp where empno=7902. 91 Dept of Computer Science & Engineering .TDAY' AFTER TRIGGER pg.TDAY".

'not working hours'). 10 / Trigger created.TTIME". if tt not between 10 and 17 then raise_application_error(-20010. 92 Dept of Computer Science & Engineering . line 6 ORA-04088: error during execution of trigger 'SCOTT.TTIME' pg.HYDERABAD INSTITUTE OF TECHNOLOGY AND MANAGEMENT create or replace trigger ttime after insert or delete or update on emp1 for each row declare tt varchar2(5). begin tt:=to_char(sysdate.'hh24'). SQL> update emp1 set empno=7777 where empno=7902. * ERROR at line 1: ORA-20010: not working hours ORA-06512: at "SCOTT. end if. output:SQL> @trig2. end.

Sign up to vote on this title
UsefulNot useful