Dbms Lab Exercise.pdf

46
1. Creation of a database and writing SQL queries to retrieve information from the database. CREATE COMMAND: SQL> CREATE TABLE employee ( Employee_name varchar2(10),employee_no number(8), dept_name varchar2(10),dept_no number (5),date_of_join date); Table created. TABLE DESCRIPTION ------------------------------- SQL> desc employee; Name Null? Type ------------------------------- -------- ------------------------ EMPLOYEE_NAME VARCHAR2(10) EMPLOYEE_NO NUMBER(8) DEPT_NAME VARCHAR2(10) DEPT_NO NUMBER(5) DATE_OF_JOIN DATE INSERT COMMAND: SQL> insert into employee values ('Vijay',345,'CSE',21,'21-jun- 2006'); 1 row created. SQL>insert into employee (Employee_name,employee_no,dept_name,dept_no,date_of_join) values ('Vishva',128, 'ECE',87,'25-dec-2006'); 1 row created. SELECT COMMAND: SELECT Employee_name FROM employee; SELECT Employee_name, dept_name FROM employee; SELECT * FROM employee; 1

Transcript of Dbms Lab Exercise.pdf

1. Creation of a database and writing SQL queries to retrieve information from the database.

CREATE COMMAND:

SQL> CREATE TABLE employee ( Employee_name varchar2(10),employee_no number(8), dept_name varchar2(10),dept_no number (5),date_of_join date);Table created.TABLE DESCRIPTION-------------------------------SQL> desc employee; Name Null? Type ------------------------------- -------- ------------------------ EMPLOYEE_NAME VARCHAR2(10) EMPLOYEE_NO NUMBER(8) DEPT_NAME VARCHAR2(10) DEPT_NO NUMBER(5) DATE_OF_JOIN DATE

INSERT COMMAND:

SQL> insert into employee values ('Vijay',345,'CSE',21,'21-jun-2006');1 row created.SQL>insert into employee (Employee_name,employee_no,dept_name,dept_no,date_of_join) values ('Vishva',128, 'ECE',87,'25-dec-2006');1 row created.SELECT COMMAND:SELECT Employee_name FROM employee;SELECT Employee_name, dept_name FROM employee;SELECT * FROM employee;

2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on conditions.

ALTER To add a column in a table SQL> alter table employee add (salary number);Table altered.To delete a column in a table SQL> alter table employee drop column salary;UPDATE SQL> update employee set salary = 100 where employee_no=98;1 row updated. MODIFYSQL> alter table employee modify ( salary varchar2(10));Table altered.SELECT:Select employee_name from employee where dept_name = CSE;DELETE SQL>delete from employee where employee_no = 344;DROP TABLE SYNTAX:drop table employee; Table droppedTRUNCATE TABLE SYNTAX:truncate table employee;Table truncatedRENAMING THE TABLE STUDSQL> rename stud to studs;Table renamed.ARITHMETIC OPERATIONSQL> select sname, sid+100 "stid" from studs;SNAME stid------------------------------ ---------ashwin 201bhavesh 202pruthvik 203charith 204

CONCATENATION OPERATORSQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;PROFESSION-------------------------------------------------------------------ashwin is a aeronautical engineer.bhavesh is a marine engineer.pruthvik is a aerospace engineer.charith is a mechanical engineer.

DISPLAY ONLY DISTINCT VALUESSQL> select distinct sarea from studs;SAREA--------------------anna nagarkilpauknungambakkamUSING THE WHERE CLAUSESQL> select sname,sage from studs where sage select sname,sarea, sid from studs where sid between 102 and 104;SNAME SAREA SID------------------------------ -------------------- ----------bhavesh nungambakkam 102pruthvik anna nagar 103charith kilpauk 104IN PREDICATESQL> select sname,sarea , sid from studs where sid in(102,104);SNAME SAREA SID------------------------------ -------------------- ----------bhavesh nungambakkam 102charith kilpauk 104PATTERN MATCHINGSQL> select sname, sarea from studs where sarea like '%g%';

SNAME SAREA------------------------------ --------------------ashwin anna nagarbhavesh nungambakkampruthvik anna nagar

LOGICAL AND OPERATORSQL> select sname ,sid from studs where sid>102 and sarea='anna nagar';SNAME SID------------------------------ ----------pruthvik 103

LOGICAL OR OPERATORSQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';SNAME SID------------------------------ ----------ashwin 101pruthvik 103charith 104 NOT IN PREDICATESQL> select sname, sid from studs where sid not in(102,104);SNAME SID------------------------------ ----------ashwin 101pruthvik 103

UPDATING THE TABLESQL> alter table studs add ( spocket varchar2(20) );Table altered.SQL> update studs set spocket=750 where sid=101;1 row updated.SQL> update studs set spocket=500 where sid=102;1 row updated.SQL> update studs set spocket=250 where sid=103;1 row updated.SQL> update studs set spocket=100 where sid=104;1 row updated.SQL> select * from studs;SNAME SID SAGE SAREA SDEPT------------------------------ ---------- --------- -------------------- --------------------SPOCKET--------------------ashwin 101 19 anna nagar aeronautical 750bhavesh 102 18 nungambakkam marine500pruthvik 103 20 anna nagar aerospace250charith 104 20 kilpauk mechanical100

AGGREGATE FUNCTIONSSQL> select avg( spocket ) result from studs; RESULT--------- 400SQL> select min(spocket) result from studs;RESULT--------------------100SQL> select count(spocket) result from studs; RESULT--------- 4SQL> select count(*) result from studs; RESULT--------- 4SQL> select count(spocket) result from studs where sarea='anna nagar'; RESULT--------- 2SQL> select max(spocket) result from studs;RESULT--------------------750SQL> select sum(spocket) result from studs; RESULT--------- 1600NUMERIC FUNCTIONSSQL> select abs(-20) result from dual; RESULT--------- 20SQL> select power (2,10) result from dual; RESULT--------- 1024SQL> select round(15.359,2) result from dual; RESULT--------- 15.36SQL> select sqrt (36) result from dual; RESULT--------- 6STRING FUNCTIONSSQL> select lower('ORACLE') result from dual;RESULT------oracleSQL> select upper('oracle') result from dual;RESULT------ORACLESQL> select initcap('Oracle') result from dual;RESULT------OracleSQL> select substr('oracle' ,2 ,5) result from dual; RESULT-----racleSQL> select lpad('oracle',10,'#') result from dual;

RESULT----------####oracleSQL> select rpad ('oracle',10,'^') result from dual;RESULT----------oracle^^^^DATE FUNCTIONSSQL> select sysdate from dual;SYSDATE---------16-JUL-08SQL> select sysdate,add_months(sysdate,4) result from dual;SYSDATE RESULT--------- ---------16-JUL-08 16-NOV-08

SQL> select sysdate, last_day(sysdate) result from dual;SYSDATE RESULT--------- ---------16-JUL-08 31-JUL-08

SQL> select sysdate, next_day(sysdate,'sunday') result from dual;SYSDATE RESULT--------- ---------16-JUL-08 20-JUL-08

SQL> select months_between('09-aug-91','11-mar-90') result from dual; RESULT --------- 16.935484

GROUP BY CLAUSESQL> select sarea, sum(spocket) result from studs group by sarea;SAREA RESULT-------------------- ------------anna nagar 1000nungambakkam 500kilpauk 100

HAVING CLAUSESQL> select sarea, sum(spocket) result from studs group by sarea having spocket delete from studs where sid=101;1 row deleted.SQL> select * from studs;

SNAME SID SAGE SAREA SDEPT SPOCKET------------------------------ ---------- --------- -------------------- -------------------- -------------------bhavesh 102 18 nungambakkam marine 500pruthvik 103 20 anna nagar aerospace 250charith 104 20 kilpauk mechanical 100

CREATING TABLES FOR DOING SET OPERATIONSTO CREATE PRODUCT TABLESQL> create table product(prodname varchar2(30), prodno varchar2(10));Table created.SQL> select * from product;PRODNAME PRODNO------------------------------ ----------table 10001chair 10010desk 10110cot 11110sofa 10010tvstand 11010

TO CREATE SALE TABLE

SQL> create table sale(prodname varchar2(30),orderno number(10),prodno varchar2(10));Table created.SQL> select * from sale;PRODNAME ORDERNO PRODNO------------------------------ --------- ----------table 801 10001chair 805 10010desk 809 10110cot 813 11110sofa 817 10010

SET OPERATIONS

SQL> select prodname from product where prodno=10010 union select prodname from sale where prodno=10010;PRODNAME------------------------------chairsofaSQL> select prodname from product where prodno=11110 intersect select prodname from sale where prodno=11110;PRODNAME------------------------------cotSQL> select * from sstud1;SNAME PLACE-------------------- --------------------prajan chennaianand chennaikumar chennairavi ChennaiSQL> select * from sstud2;SNAME DEPT MARKS-------------------- ---------- ---------prajan cse 700anand it 650vasu cse 680ravi it 600

NESTED QUERIES

SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from sstud2 );SNAME--------------------anandprajanraviSQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2 );SNAME--------------------kumarSQL> select sname from sstud2 where marks > some(select marks from sstud2 where dept='cse');SNAME--------------------prajanSQL> select sname from sstud2 where marks >= some (select marks from sstud2 where dept='cse' );SNAME--------------------prajanvasuSQL> select sname from sstud2 where marks > any ( select marks from sstud2 where dept='cse' );SNAME--------------------prajanSQL> select sname from sstud2 where marks >= any ( select marks from sstud2 where dept='cse' );SNAME--------------------prajanvasuSQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );no rows selectedSQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );SNAME--------------------anandraviSQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2 where sstud1.sname=sstud2.sname );

SNAME--------------------prajananandraviSQL> select sname from sstud1 where not exists ( select sstud2.sname from sstud2 where sstud1.sname=sstud2.sname );SNAME--------------------kumar

3. Creation of Views, Synonyms, Sequence, Indexes, Save point.

SQL> create table department(d_no number(10) primary key, dept_name varchar2(10) not null, manager varchar2(20));Table created.

SQL> create table employee(emp_no number(10) primary key, emp_name varchar2(20) not null, dept_no number(10), ph_no number(10), foreign key (dept_no) references department (d_no));Table created.

SQL> select * from employee; EMP_NO EMP_NAME PH_NO DEPT_NO---------- -------------------- ---------- ---------- 1 Abi 445566 101 2 Banu 586987 102 3 Chitra 4578126 103 4 Dhivya 5246642 104 5 Emy 4756895 105 6 Fredey 42563478 106 7 Haritha 4523689 101 8 Gowri 4425635 101 9 Lalitha 4563563 102 10 Kowsalya 4585695 10210 rows selected.

SQL> select * from department;

DEPT_NO DEPT_NAME MANAGER---------- ---------- -------------------- 101 CSE AJay 102 ECE Gowtham 103 MECH Dharveesh 104 CIVIL Vijay 105 EEE Kumaran 106 EI Arun6 rows selected.SQL> select * from emp_manager;EMP_NAME MANAGER-------------------- --------------------Abi AJayBanu GowthamChitra DharveeshDhivya VijayEmy KumaranFredey ArunHaritha AJayGowri AJayLalitha GowthamKowsalya Gowtham10 rows selected.

SQL> select * from emp_manager where manager = 'AJay';EMP_NAME MANAGER-------------------- --------------------Abi AJayHaritha AJayGowri AJay

SYNONYMS

SQL> create synonym emp for employee;Synonym created.

SQL> select * from emp; EMP_NO EMP_NAME PH_NO DEPT_NO---------- -------------------- ---------- ---------- 1 Abi 445566 101 2 Banu 586987 102 3 Chitra 4578126 103 4 Dhivya 5246642 104 5 Emy 4756895 105 6 Fredey 42563478 106 7 Haritha 4523689 101 8 Gowri 4425635 101 9 Lalitha 4563563 102 10 Kowsalya 4585695 102 10 rows selected.

SQL> select * from emp where dept_no = '102'; EMP_NO EMP_NAME PH_NO DEPT_NO---------- -------------------- ---------- ---------- 2 Banu 586987 102 9 Lalitha 4563563 102 10 Kowsalya 4585695 102

SEQUENCES

SQL> create sequence order_no_seq 2 increment by 1 3 start with 1 4 maxvalue 100 5 minvalue 1 6 cycle 7 cache 10;Sequence created.

SQL> create table sales_order(order_number number(4) primary key, order_amt number(5));Table created.

SQL> insert into sales_order values(order_no_seq.nextval, 235);1 row created.

SQL> select * from sales_order;ORDER_NUMBER ORDER_AMT------------ ---------- 1 235

After inserting 5 recordsSQL> select * from sales_order;ORDER_NUMBER ORDER_AMT------------ ---------- 1 235 2 450 3 528 4 234 5 780INDEXES:

SQL> create index manager on department (manager);Index created.

SAVEPOINT

SQL> savepoint s1;Savepoint created.

SQL> delete from employee where dept_no = '102';3 rows deleted

SQL> select * from employee; EMP_NO EMP_NAME PH_NO DEPT_NO---------- -------------------- ---------- ---------- 1 Abi 445566 101 3 Chitra 4578126 103 4 Dhivya 5246642 104 5 Emy 4756895 105 6 Fredey 42563478 106 7 Haritha 4523689 101 8 Gowri 4425635 1017 rows selected.SQL> roll back s1;Rollback complete.

SQL> select * from employee; EMP_NO EMP_NAME PH_NO DEPT_NO---------- -------------------- ---------- ---------- 1 Abi 445566 101 2 Banu 586987 102 3 Chitra 4578126 103 4 Dhivya 5246642 104 5 Emy 4756895 105 6 Fredey 42563478 106 7 Haritha 4523689 101 8 Gowri 4425635 101 9 Lalitha 4563563 102 10 Kowsalya 4585695 102 10 rows selected.

4. Creating an Employee database to set various constraints

Create table employee(emp_id number(5) primary key,emp_name varchar2(25));SQL>desc employee;Name Null? Type..EMP_IDNOT NULLNUMBER(5)EMP_NAME VARCHAR2(25)

Valid Test Data:

SQL>insert into employee values(&emp_id,&emp_name);SQL>select * from employee;

EMP_ID EMP_NAME.10Robert21Coulthard 30 Fernando Alonso 39 Kartikeyan 87 Kimmi

SQL>create table department(dept_id number(5) primary key,dept_name varchar2(20));

SQL>desc department;Name Null? Type..DEPT_ID NOT NULL NUMBER(5)DEPT_NAME VARCHAR2(20)

SQL>insert into department values(&dept_id,&dept_name);SQL>select * from department;

DEPT_ID DEPT_NAME..100 sales101 accounts102 administration103 production104 supervisorSQL>create table paydetails(emp_id number(5) references employee(emp_id),dept_id number(5) references department(dept_id),basic number(7,2),deductions number(5,2),additions number(5,2),doj date);

SQL>desc paydetails;

Name Null? Type..EMP_IDNUMBER(5)DEPT_IDNUMBER(5)BASICNUMBER(7,2)DEDUCTIONSNUMBER(5,2)ADDITIONSNUMBER(5,2)DOJDATE

Different Data Sets:

SQL>insert into paydeatils values(&emp_id,&dept_id,&basic,&deductions,&additions,&doj);

SQL>select * from paydeatils;

EMP_ID DEPT_ID BASIC DEDUCTIONS ADDITIONS DOJ..10 101 25023.12 43.09 71.2308-JAN-9321 100 10500.29 23.98 40.901-JAN-06 30 102 6500.5 30.54 1506-JUL-9739 103 9700.45 32.78 65.0908-AUG-0387 104 15000 97.66 154.824-SEP-04

SQL>create table payroll(emp_id number(5)references employee(emp_id),pay_date date);SQL>desc payroll;

Name Null? Type..EMP_IDNUMBER(5)PAY_DATEDATE

SQL>insert into payroll values(&emp_id,&date);

SQL>select * from payroll;EMP_ID PAY_DATE.1031-JAN-062103-FEB-063015-JAN-063927-JAN-068704-FEB-06c) List the employee details department wise

SQL>select empid,deptid from paydet;

EMPID DEPTID401 500402 200403 600404 400405 1200

d) List all the employee names who joined after particular date

SQL>select e,empname from employee e,paydet p where e.empid=p.empid and p.doj>=05-mar-06;

EMPNAMEAVINASHNITINPHALGUN

e) List the details of employees whose basic salary is between 10,000 and 20,000

sqL> Select empid,empname from employee where salary between 10000 and 20000;

EMPID EMPNAME.402 AKHILA403 aaaaaaaa

EMPID EMPNAME.AKHILA

f) Give a count of how many employees are working in each departmentSQL>select count(empid),deptid from paydet group by deptid;

COUNT (EMPID) DEPTID1200 14001 5001 6001 1200

g) Give a names of the employees whose netsalary>10,000

SQL> select empname from employee where empid in(select empid from paydet where basic-deduction>10000);

EMPNAMEAVINASHAKHILAHARISHNITINPHALGUN

h) List the details for an employee_id=5

sql> select * from employee where empid=5;

empidempname------------------------------------------5 Coulthard

DOMAIN INTEGRITY CONSTRAINTS

NOT NULL CONSTRAINTSQL> create table empl (ename varchar2(30) not null, eid varchar2(20) not null);Table created.SQL> insert into empl values ('abcde',11);1 row created.SQL> insert into empl values ('fghij',12);1 row created.SQL> insert into empl values ('klmno',null);insert into empl values ('klmno',null)*ERROR at line 1:ORA-01400: cannot insert NULL into ("ITA"."EMPL"."EID")SQL> select * from empl;ENAME EID------------------------------ --------------------abcde 11fghij 12

CHECK AS A COLUMN CONSTRAINT

SQL> create table depts ( dname varchar2(30) not null, did number(20) not null check (did insert into depts values ('sales ',9876);1 row created.SQL> insert into depts values ('marketing',5432);1 row created.SQL> insert into depts values ('accounts',789645);insert into depts values ('accounts',789645)*ERROR at line 1:ORA-02290: check constraint (ITA.SYS_C003179) violated

SQL> select * from depts;DNAME DID------------------------------ ---------sales 9876marketing 5432

CHECK AS A TABLE CONSTRAINT

SQL> create table airports (aname varchar2(30) not null , aid number(20) not null, acity varchar2(30) check( acity in ('chennai','hyderabad','bangalore')));Table created.SQL> insert into airports values( 'abcde', 100,'chennai');1 row created.SQL> insert into airports values( 'fghij', 101,'hyderabad');1 row created.SQL> insert into airports values( 'klmno', 102,'bangalore');1 row created.SQL> insert into airports values( 'pqrst', 103,'mumbai');insert into airports values( 'pqrst', 103,'mumbai')*ERROR at line 1:ORA-02290: check constraint (ITA.SYS_C003187) violatedSQL> select * from airports;ANAME AID ACITY------------------------------ --------- ------------------------------abcde 100 chennaifghij 101 hyderabadklmno 102 bangalore

ENTITY INTEGRITY CONSTRAINTS UNIQUE AS A COLUMN CONSTRAINTSQL> create table book (bname varchar2(30) not null, bid number(20) not null unique);Table created.SQL> insert into book values ('fairy tales',1000);1 row created.SQL> insert into book values ('bedtime stories',1001);1 row created.SQL> insert into book values ('comics',1001);insert into book values ('comics',1001)*ERROR at line 1:ORA-00001: unique constraint (ITA.SYS_C003130) violatedSQL> select * from book;BNAME BID------------------------------ ---------fairy tales 1000bedtime stories 1001

UNIQUE AS A TABLE CONSTRAINT

SQL> create table orders( oname varchar2(30) not null , oid number(20) not null , unique(oname,oid));Table created.SQL> insert into orders values ('chair', 2005);1 row created.SQL> insert into orders values ('table',2006);1 row created.SQL> insert into orders values ('chair',2007);1 row created.SQL> insert into orders values ('chair', 2005);insert into orders values ('chair', 2005)*ERROR at line 1:ORA-00001: unique constraint (ITA.SYS_C003152) violatedSQL> select * from orders;ONAME OID------------------------------ ---------chair 2005table 2006chair 2007

PRIMARY KEY AS A COLUMN CONSTRAINT

SQL> create table custo ( cname varchar2(30) not null , cid number(20) not null primary key);Table created.SQL> insert into custo values ( 'jones', 506);1 row created.SQL> insert into custo values ('hayden',508);1 row created.SQL> insert into custo values ('ricky',506);insert into custo values ('ricky',506)*ERROR at line 1:ORA-00001: unique constraint (ITA.SYS_C003165) violatedSQL> select * from custo;CNAME CID------------------------------ ---------jones 506hayden 508

PRIMARY KEY AS A TABLE CONSTRAINT

SQL> create table branches( bname varchar2(30) not null , bid number(20) not null , primary key(bnam e,bid));Table created.SQL> insert into branches values ('anna nagar', 1005);1 row created.SQL> insert into branches values ('adyar',1006);1 row created.SQL> insert into branches values ('anna nagar',1007);1 row created.SQL> insert into branches values ('anna nagar', 1005);insert into branches values ('anna nagar', 1005)*ERROR at line 1:ORA-00001: unique constraint (ITA.SYS_C003173) violatedSQL> select * from branches;BNAME BID------------------------------ ---------anna nagar 1005adyar 1006anna nagar 1007

REFERENTIAL INTEGRITY CONSTRAINTS

TO CREATE DEPTS TABLESQL> create table depts(city varchar2(20), dno number(5) primary key);Table created.SQL> insert into depts values('chennai', 11);1 row created.SQL> insert into depts values('hyderabad', 22);1 row created.

TO CREATE SEMP TABLE

SQL> create table semp(ename varchar2(20), dno number(5) references depts(dno));Table created.SQL> insert into semp values('x', 11);1 row created.SQL> insert into semp values('y', 22);1 row created.SQL> select * from semp;ENAME DNO-------------------- ---------x 11y 22

ALTER TABLE

SQL> alter table semp add(eddress varchar2(20));Table altered.SQL> update semp set eddress='10 gandhi road' where dno=11;1 row updated.SQL> update semp set eddress='12 m.g. road' where dno=22;1 row updated.SQL> select * from semp;

ENAME DNO EDDRESS-------------------- --------- --------------------x 11 10 gandhi roady 22 12 m.g. road

SQL> select city, ename from depts, s2emp where depts.dno = s2emp.dno;

CITY ENAME-------------------- --------------------chennai xhyderabad y

5. Creating relationship between the databases. Creating Dept table:SQL> create table dept(dno number(10),dname varchar(10),loc varchar(10));Table created.SQL> select * from dept; DNO DNAME LOC---------- ---------- ---------- 10 inventory hyd 20 finance bglr 30 HR mumbaiCreating emp2 table:SQL> create table emp2(eno number(10),ename varchar(10),job varchar(10),Mer(10),dno number(10));Table created.SQL> select * from emp2; ENO ENAME JOB MGR DNO---------- ---------- ---------- ---------- ---------- 111 saketh analyst 444 10 222 sandeep clerk 333 20 333 jagan manager 111 10 444 madhu engineer 222 40

1. Equijoin:

SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno;

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 333 jagan manager inventory hyd

Using Clause:SQL> select eno,ename,job,dname,loc from emp2 e join dept d using(dno);

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 333 jagan manager inventory hyd

On Clause:SQL> select eno,ename,job,dname,loc from emp2 e join dept d on(e.dno=d.dno);

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 333 jagan manager inventory hyd

2. Non-Equijoin:SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno>d.dno;

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 222 sandeep clerk inventory hyd 444 madhu engineer inventory hyd 444 madhu engineer finance bglr 444 madhu engineer HR Mumbai

3. Self Join:

SQL> select e1.eno,e2.ename,e1.job,e2.dno from emp2 e1,emp2 e2 where e1.eno=e2gr;

ENO ENAME JOB DNO---------- ---------- ---------- ---------- 444 saketh engineer 10 333 sandeep manager 20 111 jagan analyst 10 222 madhu clerk 40

4. Natural Join:SQL> select eno,ename,job,dname,loc from emp2 natural join dept;

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 333 jagan manager inventory hyd

5. Cross Join:

SQL> select eno,ename,job,dname,loc from emp2 cross join dept;

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk inventory hyd 333 jagan manager inventory hyd 444 madhu engineer inventory hyd 111 saketh analyst finance bglr 222 sandeep clerk finance bglr 333 jagan manager finance bglr 444 madhu engineer finance bglr 111 saketh analyst HR mumbai 222 sandeep clerk HR mumbai 333 jagan manager HR mumbai

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 444 madhu engineer HR mumbai

12 rows selected.

6. Outer Join:6.1 Left Outer Join:SQL> select eno,ename,job,dname,loc from emp2 e left outer join dept d on(e.dno=d.dno);(OR)SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno=d.dno(+);

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 333 jagan manager inventory hyd 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 444 madhu engineer

6.2 Right Outer Join:SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno =d.dno);(OR)SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 333 jagan manager inventory hyd HR mumbai6.3 Full Outer Join:SQL> select eno,ename,job,dname,loc from emp2 e full outer join dept d on(e.dno=d.dno);

ENO ENAME JOB DNAME LOC---------- ---------- ---------- ---------- ---------- 333 jagan manager inventory hyd 111 saketh analyst inventory hyd 222 sandeep clerk finance bglr 444 madhu engineer HR Mumbai

6. Study of PL/SQL block.

1. TO DISPLAY HELLO MESSAGESQL> set serveroutput on;SQL> declare 2 a varchar2(20); 3 begin 4 a:='Hello'; 5 dbms_output.put_line(a); 6 end; 7 /HelloPL/SQL procedure successfully completed.2. Insert the record into Sailors table by reading the values from the Keyboard.SQL> create table sailors(sid number(10),sname varchar(10),rating number(10),age number(10));Table created.

SQL> set serveroutput onSQL> declare 2 sid number(10):=&sid; 3 sname varchar(10):='&sname'; 4 rating number(10):=&rating; 5 age number(10):=&age; 6 begin 7 insert into sailors values(sid,sname,rating,age); 8 end; 9 /Enter value for sid: 02old 2: sid number(10):=&sid;new 2: sid number(10):=02;Enter value for sname: lavanyaold 3: sname varchar(10):='&sname';new 3: sname varchar(10):='lavanya';Enter value for rating: 01old 4: rating number(10):=&rating;new 4: rating number(10):=01;Enter value for age: 25old 5: age number(10):=&age;new 5: age number(10):=25;

PL/SQL procedure successfully completed.

SQL> /Enter value for sid: 03old 2: sid number(10):=&sid;new 2: sid number(10):=03;Enter value for sname: vaniold 3: sname varchar(10):='&sname';new 3: sname varchar(10):='vani';Enter value for rating: 02old 4: rating number(10):=&rating;new 4: rating number(10):=02;Enter value for age: 25old 5: age number(10):=&age;new 5: age number(10):=25;

PL/SQL procedure successfully completed.

SQL> select * from sailors;

SID SNAME RATING AGE---------- ---------- ---------- ---------- 2 lavanya 1 25 3 vani 2 25

7. PL/SQL block to satisfy some conditions by accepting user input

To Input A Value From The User And Display ItSQL> set serveroutput on;SQL> declare 2 a varchar2(20); 3 begin 4 a:=&a; 5 dbms_output.put_line(a); 6 end; 7 /Enter value for a: 5old 4: a:=&a;new 4: a:=5;5PL/SQL procedure successfully completed.Greatest Of Three NumbersSQL> set serveroutput on;SQL> declare 2 a number(7); 3 b number(7); 4 c number(7); 5 begin 6 a:=&a; 7 b:=&b; 8 c:=&c; 9 if(a>b and a>c) then 10 dbms_output.put_line (' The greatest of the three is ' || a); 11 else if (b>c) then 12 dbms_output.put_line (' The greatest of the three is ' || b); 13 else 14 dbms_output.put_line (' The greatest of the three is ' || c); 15 end if; 16 end if; 17 end; 18 /Enter value for a: 5old 6: a:=&a;new 6: a:=5;Enter value for b: 7old 7: b:=&b;new 7: b:=7;Enter value for c: 1old 8: c:=&c;new 8: c:=1;The greatest of the three is 7PL/SQL procedure successfully completed.

Print Numbers From 1 To 5 Using For Loop

SQL> set serveroutput on;SQL> declare 2 a number:=1; 3 begin 4 for a in 1..5 5 loop 6 dbms_output.put_line (a); 7 end loop; 8 end; 9 /12345PL/SQL procedure successfully completed.

TO CREATE SACCOUNT TABLE

SQL> create table saccount ( accno number(5), name varchar2(20), bal number(10));Table created.SQL> insert into saccount values ( 1,'mala',20000);1 row created.SQL> insert into saccount values (2,'kala',30000);1 row created.SQL> select * from saccount; ACCNO NAME BAL--------- -------------------- --------- 1 mala 20000 2 kala 30000SQL> set serveroutput on;SQL> declare 2 a_bal number(7); 3 a_no varchar2(20); 4 debit number(7):=2000; 5 minamt number(7):=500; 6 begin 7 a_no:=&a_no; 8 select bal into a_bal from saccount where accno= a_no; 9 a_bal:= a_bal-debit; 10 if (a_bal > minamt) then 11 update saccount set bal=bal-debit where accno=a_no; 12 end if; 13 end; 14 15 /Enter value for a_no: 1old 7: a_no:=&a_no;new 7: a_no:=1;PL/SQL procedure successfully completed.SQL> select * from saccount; ACCNO NAME BAL--------- -------------------- --------- 1 mala 18000 2 kala 30000

TO CREATE TABLE SROUTES

SQL> create table sroutes ( rno number(5), origin varchar2(20), destination varchar2(20), fare number(10), distance number(10));Table created.SQL> insert into sroutes values ( 2, 'chennai', 'dindugal', 400,230);1 row created.SQL> insert into sroutes values ( 3, 'chennai', 'madurai', 250,300);1 row created.SQL> insert into sroutes values ( 6, 'thanjavur', 'palani', 350,370);1 row created.SQL> select * from sroutes; RNO ORIGIN DESTINATION FARE DISTANCE--------- -------------------- -------------------- --------- --------- 2 chennai dindugal 400 230 3 chennai madurai 250 300 6 thanjavur palani 350 370SQL> set serveroutput on;SQL> declare 2 route sroutes.rno % type; 3 fares sroutes.fare % type; 4 dist sroutes.distance % type; 5 begin 6 route:=&route; 7 select fare, distance into fares , dist from sroutes where rno=route; 8 if (dist < 250) then 9 update sroutes set fare=300 where rno=route; 10 else if dist between 250 and 370 then 11 update sroutes set fare=400 where rno=route; 12 else if (dist > 400) then 13 dbms_output.put_line('Sorry'); 14 end if; 15 end if; 16 end if; 17 end; 18 /Enter value for route: 3old 6: route:=&route;new 6: route:=3;

PL/SQL procedure successfully completed.

SQL> select * from sroutes;

RNO ORIGIN DESTINATION FARE DISTANCE--------- -------------------- -------------------- --------- --------- 2 chennai dindugal 400 230 3 chennai madurai 400 300 6 thanjavur palani 350 370

Ex. No: 8 PL/SQL block that handles all types of exceptions.

Program with user defined exception:DeclareA numberB number;C number;Mydivide_zero EXCEPTION;BeginA:=&a;B:=&b;If(B=0) thenRaise Mydivide_zero;elseC:=a/b;Dbms_output.put_line(division is || C);End if;ExceptionIf (mydivide_zero) thenDbms_output.put_line(b could not be zero);End if;End;

Valid Test Data:Enter the value for a:10Enter the value for b:0OUTPUTb could not be zero

Handling the Exceptions on no data foundSQL> create table employee1 ( 2 id number, 3 employee_type_id number, 4 external_id varchar2(30), 5 first_name varchar2(30), 6 middle_name varchar2(30), 7 last_name varchar2(30), 8 name varchar2(100), 9 birth_date date , 10 gender_id number );Table created.

SQL> create table gender ( 2 id number, 3 code varchar2(30), 4 description varchar2(80), 5 active_date date default SYSDATE not null, 6 inactive_date date );

Table created.

SQL> insert into gender ( id, code, description ) values ( 1, 'F', 'Female' );1 row created.SQL> insert into gender ( id, code, description ) values ( 2, 'M', 'Male' );1 row created.SQL> insert into gender ( id, code, description ) values ( 3, 'U', 'Unknown' );1 row created.SQL> set serveroutput on size 1000000;SQL> declare 2 3 d_birth_date employee1.birth_date%TYPE; 4 n_gender_id employee1.gender_id%TYPE; 5 n_selected number := -1; 6 n_id employee1.id%TYPE; 7 v_first_name employee1.first_name%TYPE; 8 v_last_name employee1.last_name%TYPE; 9 v_middle_name employee1.middle_name%TYPE; 10 v_name employee1.name%TYPE; 11 12 begin 13 v_first_name := 'JOHN'; 14 v_middle_name := 'J.'; 15 v_last_name := 'DOUGH'; 16 v_name := rtrim(v_last_name||', '||v_first_name||' '||v_middle_name); 17 d_birth_date := to_date('19800101', 'YYYYMMDD'); 18 19 begin 20 select id into n_gender_id from gender where code = 'M'; 21 exception 22 when OTHERS then 23 raise_application_error(-20001, SQLERRM||' on select gender'); 24 end; 25 26 begin 27 select id 28 into n_id 29 from employee1 30 where name = v_name 31 and birth_date = d_birth_date 32 and gender_id = n_gender_id; 33 34 n_selected := sql%rowcount; 35 exception 36 when NO_DATA_FOUND then 37 n_selected := sql%rowcount; 38 DBMS_OUTPUT.PUT_LINE('Caught raised exception NO_DATA_FOUND'); 39 when OTHERS then 40 raise_application_error(-20002, SQLERRM||' on select employee'); 41 end; 42 43 DBMS_OUTPUT.PUT_LINE(to_char(n_selected)||' row(s) selected.'); 44 end; 45 /Caught raised exception NO_DATA_FOUND0 row(s) selected.PL/SQL procedure successfully completed.

9. Creation of Procedures.

Creating the table ititems and displaying the contents

SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid number(4));Table created.SQL> insert into ititems values(101, 2000, 500, 201);1 row created.SQL> insert into ititems values(102, 3000, 1600, 202);1 row created.SQL> insert into ititems values(103, 4000, 600, 202);1 row created.SQL> select * from ititems;ITEMID ACTUALPRICE ORDID PRODID--------- ----------- -------- --------- 101 2000 500 201 102 3000 1600 202 103 4000 600 202

Program For General Procedure Selected Records Price Is Incremented By 500 , Executing The Procedure Created And Displaying The Updated Table

SQL> create procedure itsum(identity number, total number) is price number; 2 null_price exception; 3 begin 4 select actualprice into price from ititems where itemid=identity; 5 if price is null then 6 raise null_price; 7 else 8 update ititems set actualprice=actualprice+total where itemid=identity; 9 end if; 10 exception 11 when null_price then 12 dbms_output.put_line('price is null'); 13 end; 14 /

Procedure created.

SQL> exec itsum(101, 500);PL/SQL procedure successfully completed.

SQL> select * from ititems; ITEMID ACTUALPRICE ORDID PRODID--------- ----------- --------- --------- 101 2500 500 201 102 3000 1600 202 103 4000 600 202

Procedure For In Parameter Creation, Execution

SQL> set serveroutput on;SQL> create procedure yyy (a IN number) is price number; 2 begin 3 select actualprice into price from ititems where itemid=a; 4 dbms_output.put_line('Actual price is ' || price); 5 if price is null then 6 dbms_output.put_line('price is null'); 7 end if; 8 end; 9 /Procedure created.

SQL> exec yyy(103);Actual price is 4000PL/SQL procedure successfully completed.

Procedure For Out Parameter Creation, Execution

SQL> set serveroutput on;SQL> create procedure zzz (a in number, b out number) is identity number; 2 begin 3 select ordid into identity from ititems where itemid=a; 4 if identity declare 2 a number; 3 b number; 4 begin 5 zzz(101,b); 6 dbms_output.put_line('The value of b is '|| b); 7 end; 8 /The value of b is 100

PL/SQL procedure successfully completed.

Procedure For Inout Parameter Creation, Execution

SQL> create procedure itit ( a in out number) is 2 begin 3 a:=a+1; 4 end; 5 / Procedure created.

SQL> declare 2 a number:=7; 3 begin 4 itit(a); 5 dbms_output.put_line(The updated value is ||a); 6 end; 7 /The updated value is 8

PL/SQL procedure successfully completed.

36