Dbms Lab Manuals
Dbms Lab Manuals
Dbms Lab Manuals
PART - A
1. The STUDENT detail databases has a table with the following attributes. The primary
keys are underlined. STUDENT(regno: int, name: string, dob: date, marks: int)
Answer
1. Create table studentdb (regno number(10) primary key , name varchar(20), dob date, marks
number(10))
2. Alter table studentdb drop column marks
3. Alter table studentdb modify regno varchar2(20)
4. Alter table studentdb add phoneno number(10)
5. Insert into studentdb values(®no,&name,&dob,&marks,&phoneno)
6. Select * from studentdb
****************************************
vi) List the details of all the books whose price ranges between Rs. 100 and Rs. 300
1
Answer
1. Create table librarydb (bookid number(10) primary key, title varchar2(20),author varchar2(20),
publication varchar2(20), year number(5),price number(6,2))
2. Insert into librarydb values(&bookid,&title,&author,&publication,&year,&price)
3. Select * from librarydb;
4. Select distinct publication from librarydb
5. Select * from librarydb order by title asc
6. Select * from librarydb where price between 100 and 300
*****************************
3. The SALARY database of an organization has a table with the following attributes.
v) Find the sum and average of the salaries of employees of a particular department.
vi) Find the least and highest salaries that an employee draws.
Answers
1. Create table salarydb(empcode number(10) primary key, empname varchar2(20), dob date,
dept varchar2(15),salary number(10,2))
2. Insert into salarydb values(&empcode,&empname,&dob,&dept,&salary)
3. Select dept,count(*) from salarydb group by dept
4. Select sum(salary) from salarydb
5. Select sum(salary) , avg(salary) from salarydb where dept=”computer”
6. Select min(salary) from salarydb
7. Select max(salary) from salarydb
*****************************
2
4. Consider the insurance database given below. The primary keys are underlined and
i) Create the above tables by properly specifying the primary keys and the foreign keys
a) Update the damage amount for the car with a specific regno in the accident with
report no 12 to 25000.
iv) Find total number of people who owned cars that were involved in accidents in 2002
v) Find the number of accidents in which cars belonging to a specific model were involved
Answers
1. Create table persondb(driver_id varchar2(20) primary key,name varchar2(20) not null, address
varchar2(30))
2. Create table cardb(regno varchar2(20) primary key, model varchar2(20) not null,year
number(5))
3. Create table accidentdb(report_no number(10) primary key,accident_date date,location
varchar2(20))
4. Create table ownsdb (driver_id varchar2(20) references persondb,,regno varchar2(20)
references cardb)
5. Create table participateddb(driver_id varchar2(20) references persondb,regno varchar2(20)
references cardb,report_no number(10) references accidentdb,damage_amount number(10))
6. Insert into persondb values(&driver_id,&name,&address)
7. Insert into cardb values(®no,&model,&year)
8. Insert into accidentdb values(&report_no,&accident_date,&location)
9. Insert into ownsdb values(&driver_id,®_no)
3
10. Insert into participateddb values(&driver_id,®_no,&report_no,&damage_amount)
11. Update participateddb set damage_amount = 25000 where reg_no = 1001 and report_no = 12
12. Insert into participateddb values(1004,2004,10,3000)
13. Select count(*) from accidentdb where accident_date LIKE ‘%-%-02’
14. Select count(*) from cardb c ,participateddb p where c.regno=p.regno and c.model = ‘scoda’
*****************************
5. Consider the following database of students enrollment in courses and books adopted
i) Create the above tables by properly specifying the primary keys and the foreign keys
iii) Demonstrate how you add a new text book to the database and make this book be
iv) Produce a list of text books (include Course-no, book-isbn, book-title) in the
alphabetical order for courses offered by the ‘Compute Science’ department that use
v) List any department that has all its adopted books published by a specific publisher.
4
Answers
***********************
CATLOG(book-id: int, title : string, author-id: int, publisher-id: int, category: int, year:
5
ORDER-DETAILS(order-no: int, book-id: int, quantity: int)
i) Create above tables by properly specifying the primary keys and the foreign keys.
iii) Give the details of the authors who have 2 or more books in the catalog and the price
of the books is greater than the average price of the books in the catalog and the year
iv) Find the author of the book which has maximum sales.
Answers
1. create table authordb (author_id number(5) primary key , name varchar2(15) not null,city
varchar2(10),country varchar2(10))
2. create table publisherdb(publisher_id number(5) primary key,name varchar2(15) not null,city
varchar2(15),country varchar2(15))
3. create table catalogdb(book_id number(5) primary key, title varchar2(15) not null,author_id
number(5) references authordb,publisher_id number(5) references publisherdb,year
number(5),price number(8))
4. create table categorydb(category_id number(5) primary key,description varchar2(15))
5. create table orderdb(order_id number(5) primary key,book_id number(5) references
catalogdb,quantity number(5))
6. insert into authordb values(&author_id,&name,&city,&country)
7. insert into publisherdb values(&publisher_id,&name,&city,&country)
8. insert into catalogdb values(&book_id,&title,&author_id,&publisher_id,&year,&price)
9. insert into categorydb values(&category_id,&description)
10. insert into orderdb values(&order_id,&book_id,&quantity)
11. select c.author_id,a.name from catalogdb c,authordb a where a.author_id = c.author_id and
c.price > (select avg(price) from catalogdb group by c.author_id , a.name having count(*) > 2)
12. update catalogdb set price = price*1.10 where publisher _id = 10;
***************************************
6
CUSTOMER(customer-name: string, customer-street: string, customer-city: string)
i) Create the above tables by properly specifying the primary keys and foreign keys.
iii) Find all the customers who have atleast two accounts at the main branch.
iv) Find all customer who have an account at all the branches located in a specific city.
v) Demonstrate how t0 delete all account tuples at every branch located in specific city.
Answers
1. Create table branchdb (bname varchar2(15) primary key,bcity varchar2(15) not null,asset
number(8,4))
2. Create table accountdb (accno number(10) primary key , bname varchar2(15) references
branchdb,balance number(10,2))
3. create table depositordb(cname varchar2(15) not null,accno number(5) references accountdb primary
key(cname,accno))
4.create table customerdb (cname varchar2(15) primary key, cstreet varchar2(15),ccity varchar(15))
5.create table loandb (loanno number(10) primary key,bname varchar2(15) references branchdb
,amount number(10,2))
6.create table borrowerdb (cname varchar2(20) not null,loanno number(10) references loandb,primary
key(cname,loanno)
7
13. create view decacc as (selelct b.bname,a.accno,d.cname from branchdb b,accountdb a,depositordb
d where a.accno = d.accno and a.bname = b.bname and b.bname = ‘jayanagar’)
14. select bname,accno,cname from decacc where cname in(select cname from decacc group by cname
having count(*)>-2)
16.delete from accountdb where bname in (select bname from branchdb where bcity=’bangalore’)
******************************************
i) Create the above tables by properly specifying the primary keys and the foreign keys
iii) List the order number and ship date for all orders shipped from particular warehouse
v) List the orders that were not shipped within 30 days of ordering
Answers
1. create table customerdb (cust_id number(6) primary key,cname varchar2(20) not null,ccity
varchar2(20))
2. create table custorderdb(orderno number(6) primary key,orderdate date,customer_no
number(6),order_amt number(6))
8
3. create table itemdb(item_no number(6) primary key , unitprice number(6))
4. create table warehousedb(warehouseno number(5) primary key,city varchar2(12))
5. create table shipmentdb(orderno number(6) references custorderdb,warehouse_no number(5)
references warehouse_db,shipdate date);
6. insert into customerdb values(&cust_id,&cname,&ccity)
7. insert into custorderdb values(&orderno,&orderdate,&customer_no,&order_amt)
8. insert into itemdb values(&item_no,&unitprice)
9. insert into warehousedb values(&warehouseno,&city)
10. insert into shipmentdb values(&orderno,&warehouse_no,&shipdate)
11. select orderno,shipdate from shipmentdb where warehouse_no=1002
12. select c.cname , count(co.orderno),avg(co.order_amt) from customerdb c,customer_db co
where c.cust_id = co.customer_no group by c.cname,co.customer_no
13. select c.cname ,co.orderno,co.orderdate,sh.shipdate from customerdb c,custorder
co,shipmentdb sh where c.cust_id =co.customer_no and co.orderno = sh.orderno and (to_date
(sh.shipdate)-to_date(co.orderdate))>30
*********************************************
PART – B
1. Write a PL/SQL program to find the largest of three numbers
declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then
dbms_output.put_line(‘a is largest’ || a);
elsif (b>a and b>c) then
dbms_output.put_line(‘b is largest’ || b);
else
dbms_output.put_line(‘c is the largest’||c);
endif;
end;
9
2. Write a PL/SQL program to generate reverse for given number
declare
n number(4) := &n;
s number(4) := 0;
r number(4);
begin
while n > 0
loop
r:= mod(n,10);
s:=(s*10)+r;
n:=trunc(n/10);
end loop;
dbms_output.put_line(‘the reverse number is’);
dbms_output.put_line(s);
end;
declare
i number(4) :=1;
n number(4) := &n;
f number(4) :=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(‘factorial of a number is’|| f);
end;
declare
num number;
i number := 1;
c number :=0;
begin
num := #
10
for i in 1..num
loop
if ((mod(num,i))=0)
then
c:=c+1;
end if;
end loop;
if (c>2)
then
dbms_output.put_line(num|| ‘not prime’);
else
dbms_output.put_line(num || ‘is prime’);
end if;
end;
declare
a number(3) := 1;
b number(3) := 1;
c number(3);
n number(3);
begin
n:=&n;
dbms_output.put_line(‘the Fibonacci series is:’)
while a<=n
loop
dbms_output.put_line(a);
c:=a+b;
a:=b;
b:=c;
end loop;
end;
11
Declare
begin
Insert into employee values(10,’chitra’,’hr’,40000);
End;
declare
n number(4);
d number(4);
begin
n:=&n;
d:=n/0;
exception
when zero_divide then
dbms_output.put_line(‘divide by error exception is caught’);
end;
8. Write a pl/sql program for creating a procedure for calculating sum of two numbers.
Execution
SQL>exec sum(10,20);
The sum is : 30
12
Dbms_output.put_line(‘the given year is not leap year’);
End if;
End;
13