Dbms Lab Manual SQL Command _021939
Dbms Lab Manual SQL Command _021939
a) Create view sleeper to display train no, start place, destination which
have sleeper class and perform the
following
• insert new record
• update destination=’Manglore’ where train no=’RJD16’
• delete a record which have train no=’KKE55’
b) Create view details to display train no, train name, class
c) Create view total_seats to display train number, start place, use count
function to no of seats , group by
start place and perform the following
• insert new record
• update start place=’Hubli’ where train no=’JNS8’
• delete last row of the view
d) Rename view sleeper to class
e) Delete view details
7. Write PL/SQL procedure to compute factorial of a number using
recursion
8. Given the table EMPLOYEE (EmpNo, Name, Salary, Designation,
DeptID) write a cursor in PL/SQL to
select the five highest paid employees from the table.
9. Given the table MOVIE(MID,MTitle, Language,Director,Year) write a
function in PL/SQL to find the
total number of Movies in the table.
10. Given the Table CUSTOMERS(CID,CName, Address) write a PL/SQL
program which asks for
customer ID, if the user enters invalid ID then the exception invalid_id
is raised
1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc22','ram','13jan2001'
,2234567890);
1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc33','sun','14jan2002'
,3234567890);
1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc44','moon','14feb2003'
,4234567890);
1 row created.
SQL> insert into student(regno,name,dob,phno)values('u11sc55','ravi','02oct2004'
,5234567890);
1 row created.
f) Display the tuples in table
SQL> SELECT * FROM STUDENT;
REGNO NAME DOB PHNO
---------- ---------- --------- ----------
u11sc11 john 12-JAN-00 1234567890
u11sc22 ram 13-JAN-01 2234567890
u11sc33 sun 14-JAN-02 3234567890
u11sc44 moon 14-FEB-03 4234567890
u11sc55 ravi 02-OCT-04 5234567890
pocket books
grove press
e) Update price of all books with 5% GST amount.
SQL> UPDATE library SET price = price +(price*5/100);
5 rows updated.
SQL> SELECT * FROM library;
DOB DATE
BRANCH VARCHAR2(20)
b) Insert 5 tuples into the table
SQL> insert into employee values(10,'ravi','biotech',20000,'9-dec-2000','bsc');
1 row created.
SQL> insert into employee values(11,'ramesh','cs',26000,'3-jan-2003','bsc');
1 row created.
SQL> insert into employee values(13,'ramya','Acc',30000,'11-oct-2005','bcom');
1 row created.
SQL> insert into employee values(15,'rahul','cs',50000,'31-oct-2001','bca');
1 row created.
SQL> insert into employee values(14,'someshwar','biotech',55000,'9-dec-
2000','bsc');
1 row created.
SQL> SELECT * FROM employee;
EMP_NO EMP_NAME DEPT SALARY DOB BRANCH
---------- ---------- ---------- ---------- --------- --------------------
10 ravi biotech 20000 09-DEC-00 bsc
11 ramesh cs 26000 03-JAN-03 bsc
13 ramya Acc 30000 11-OCT-05 bcom
15 rahul cs 50000 31-OCT-01 bca
14 someshwar biotech 55000 09-DEC-00 bsc
c) Retrieve average salary of all employee
SQL> SELECT avg(salary)Avg_of_salary FROM employee;
AVG_OF_SALARY
-------------
36200
d) Retrieve number of employees
120 50
198 30
420 5
570 27
c) List the items purchased.
SQL> SELECT * FROM items WHERE item_code in (select item_code from
purchase);
ITEM_CODE NAME PRICE
---------- ---------- -------
120 mouse 400
198 keyboard 800
420 monitor 1000
570 router 500
d) List the items which are not purchased by anyone.
SQL> SELECT * FROM items WHERE item_code not in (select item_code from
purchase);
ITEM_CODE NAME PRICE
---------- ---------- ----------
528 cpu 5000
5. Implement Join operations in SQL The COMPANY database consists of the
tables:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo) DEPARTMENT
(DNo, DName, MgrSSN, MgrStartDate)
Create tables, Insert 5 tuples each and perform the following
SQL> CREATE TABLE employee(ssn number(10),Name varchar2(10),Address
varchar2(10),Sex varchar2(7),Salary number(5),SuperSSN number(10),DNo
number(5));
Table created.
1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: research
Enter value for mgrssn: 850
Enter value for mgrstartdate: 02-mar-2009
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: cs
Enter value for mgrssn: 950
Enter value for mgrstartdate: 05-feb-2015
1 row created.
SQL> /
Enter value for dno: 4
Enter value for dname: bio-tech
Enter value for mgrssn: 650
Enter value for mgrstartdate: 02-dec-2018
1 row created.
SQL> /
Enter value for dno: 6
Enter value for dname: cs
Enter value for mgrssn: 550
Enter value for mgrstartdate: 08-jun-2023
1 row created.
SQL> select * from department;
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e,department d where e.DNo <> d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD
---------- ---------- --------- ------- ---------- ---------- ---------- --------------- ---------- ---------
---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
1436 shalini tumkur female 50000 580 null null null null
RIGHT JOIN :-
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e RIGHT JOIN department d ON e.DNo =
d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD
---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
null null null null null null 6 cs 550 08-JUN-23
FULL JOIN :-
SQL> select
e.ssn,e.Name,e.Address,e.sex,e.salary,e.SuperSSN,d.DNo,d.DName,d.MgrSSN,d
.MgrStartDate FROM employee e FULL JOIN department d ON e.DNo = d.DNo;
SSN NAME ADDRESS SEX SALARY SUPERSSN DNO DNAME MGRSSN MGRSTARTD
---------- ---------- ---------- ------- ---------- ---------- --- ------- ---------- ---------
5432 vijay tumkur male 66000 780 1 research 750 01-JAN-12
5462 geetha banglore female 40700 797 2 research 850 02-MAR-09
6852 suhas hassan male 25000 970 3 cs 950 05-FEB-15
5490 rahul arsikere male 80000 148 4 bio-tech 650 02-DEC-18
null null null null null null 6 cs 550 08-JUN-23
1436 shalini tumkur female 50000 580 null null null null
4 rows selected.
6. Create views for a particular table
The RAILWAY RESERVATION SYSTEM database consists of the tables:
TRAIN(TrainNo, TrainName, StartPlace, Destination)
AVAILABILITY(TrainNo, Class,StartPlace,Destination,No_of_seats)
SQL> create table train(trainno varchar2(7),train_name varchar2(25),startplace
varchar2(25),destination varchar2(25));
Table created.
SQL> desc train;
Name Null? Type
----------------------------------------- -------- ----------------------------
TRAINNO VARCHAR2(7)
TRAIN_NAME VARCHAR2(25)
STARTPLACE VARCHAR2(25)
DESTINATION VARCHAR2(25)
SQL> create table availability(trainno varchar2(7),class varchar2(10),startplace
varchar2(25),destination varchar2(25),no_of_seats number(5));
Table created.
SQL> desc availability;
Name Null? Type
SQL> declare
2 num number;
3 factorial number;
4 function calculateFact(x number)
5 return number
6 is
7 f number;
8 begin
9 if x=0 then
10 f:=1;
11 else
12 f:=x*calculateFact(x-1);
13 end if;
14 return f;
15 end;
16 begin
17 num:=2;
18 factorial:=calculateFact(num);
19 DBMS_OUTPUT.PUT_LINE('factorial'||num||'is'||factorial);
20 end;
21 /
factorial2is2
PL/SQL procedure successfully completed.
8. Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID)
write a cursor in PL/SQL to
select the five highest paid employees from the table.
SQL> CREATE table employee12(empno number(6),name varchar2(20),salary
number(10),designation varchar2(20),deptID number(10));
Table created.
SQL> /
Enter value for empno: 102
Enter value for name: lekhana
Enter value for salary: 10000
Enter value for designation: manager
Enter value for deptid: 112
1 row created.
SQL> /
Enter value for empno: 103
Enter value for name: kavya
Enter value for salary: 30000
Enter value for designation: teacher
Enter value for deptid: 113
1 row created.
SQL> /
Enter value for empno: 104
Enter value for name: shree
Enter value for salary: 25000
Enter value for designation: lecturer
Enter value for deptid: 114
row created.
SQL> /
Enter value for empno: 105
Enter value for name: latha
Enter value for salary: 15000
Enter value for designation: hr mnager
Enter value for deptid: 115
1 row created.
SQL> /
Enter value for empno: 106
SQL> /
Enter value for empno: 107
Enter value for name: rachna
Enter value for salary: 33000
Enter value for designation: engineer
Enter value for deptid: 117
1 row created.
SQL> /
Enter value for empno: 108
Enter value for name: sinchana
Enter value for salary: 12000
Enter value for designation: owner
Enter value for deptid: 118
1 row created.
SQL> /
Enter value for empno: 109
Enter value for name: jaya
Enter value for salary: 23000
Enter value for designation: home worker
Enter value for deptid: 119
1 row created.
SQL> /
Enter value for empno: 110
Enter value for name: teju
Enter value for salary: 22000
Enter value for designation: professor
Enter value for deptid: 120
1 row created.
10 rows selected.
103 30000
104 25000
109 23000
110 22000
PL/SQL procedure successfully completed.
9. Given the table MOVIE(MID,MTitle, Language,Director,Year) write a
function in PL/SQL to find the
total number of Movies in the table.
SQL> create table movie(mid varchar2(10) primary key,mtitle varchar2(20) not
null, language varchar2(15) not null,director varchar2(25) not null, year
number(4) not null);
Table created.
SQL> desc movie;
Name Null? Type
----------------------------------------- -------- ----------------------------
MID NOT NULL VARCHAR2(10)
MTITLE NOT NULL VARCHAR2(20)
LANGUAGE NOT NULL VARCHAR2(15)
DIRECTOR NOT NULL VARCHAR2(25)
YEAR NOT NULL NUMBER(4)
SQL> insert into movie4 values ('&mid','&mtitle',' &language',' &director','
&year');
Enter value for mid: m123
Enter value for mtitle: taare
Enter value forlanguage: hindi
Enter value for director: kapoor
Enter value for year: 2000
1 row created.
SQL> /
1 row created.
SQL> select*from movie4;
MID MTITLE LANGUAGE DIRECTOR YEAR
---------- -------------------- --------------- ------------------------- ----------
m123 taare hindi Kapoor 2000
m342 kranti kannada yogarajbhat 2023
m126 kgf kannada rakshithshetty 2020
m129 kantara kannada sudeep 2000
m421 rajkumara kannada pavan 2021
customer ID, if the user enters invalid ID then the exception invalid_id is
raised
SQL>create table customer(CID number(10),CName varchar2(20),Address
varchar2(20));
Table created.
SQL> desc customer;
Name Null? Type
---------------------------------------- -------- ----------------------------
CID NUMBER(10)
CNAME VARCHAR2(20)
ADDRESS VARCHAR2(20)
SQL> insert into customer values(12,'Arjun','Tiptur');
1 row created.
SQL> insert into customer values(13,'Aryan','Tumkur');
1 row created.
SQL> insert into customer values(14,'Srujan','banglore');
1 row created.
SQL> insert into customer values(14,'Raju','Hassan');
1 row created.
SQL> insert into customer values(15,'Rama','Hubballi');
1 row created.
SQL> select *from customer;
CID CNAME ADDRESS
---------- -------------------- --------------------
12 Arjun Tiptur
13 Aryan Tumkur
14 Srujan banglore
14 Raju Hassan
15 Rama Hubballi
SQL> SET SERVEROUTPUT ON
SQL> declare
2 cname varchar2(20);
3 cid number(10);
4 begin
5 select cname,cid
6 into cname,cid
7 from customer
8 where cid='18';
9 dbms_output.put_line(cname||' '||cid);
10 exception
11 when no_data_found then
12 dbms_output.put_line('Invalid id');
13 when others then
14 dbms_output.put_line('no records');
15 end;
16 /
Invalid id
PL/SQL procedure successfully completed.