DBMS Lab Record
DBMS Lab Record
COURSE: BCA
Y2K8 SCHEME
ND
SUBJECT: 2 SEM BCA
DBMS
PRACTICAL
RECORD
2
INDEX:
S.L.
N.O. TOPIC DATE PAGE
1 STUDENT DETAILS DATABASE 08-03-2011 3-4
08-04-2011 20-23
8 STUDENT ENROLLMENT
DATABASE
Create a student details database that has a table with the following attributes. The
primary keys are underlined and the data types are specified:
SOL:
create table student(regno int primary key,name varchar(10),dob
datetime,marks int);
exec sp_columns student;
SOL:
alter table student drop column marks;
exec sp_columns student;
SOL:
alter table student alter column dob varchar(11);
exec sp_columns student;
SOL:
alter table student add phonono int;
exec sp_columns student;
SOL:
insert into student values(101,'DIVYA','01-mar-1991',80);
insert into student values(102,'SOMYA','03-feb-1991',90);
insert into student values(103,'CHAYA','15-apr-1992',95);
insert into student values(104,'FUNYA','19-may-1993',70);
insert into student values(105,'DUMYA','15-jun-1992',65);
SOL:
select * from student;
2.LIBRARY DATABASE:
5
A library database has a table with the following attributes.The primary keys are
underlined and the data types are specified:
LIBRARAY (bookid: number, title: text, author: text, publisher: text, year_pub:
number, price: number)
SOL:
create table library(bookid int primary key,title varchar(10),author
varchar(10),publisher varchar(10),yearpub int,price float);
exec sp_columns library;
SOL:
insert into library values(111,'Maths','Umarani','Himalaya',2009,250.00);
insert into library values(112,'Physics','Ranganath','Subhash',2000,230.00);
insert into library values(113,'Chemistry','Deepa','MES',1990,360.00);
insert into library values(114,'Biology','Seema','S Chand',1992,220.00);
insert into library values(115,'Accounts','Arun','ML Kant',1996,550.00);
SOL:
select * from library order by title;
d) List details of all the books whose price ranges between Rs.100.00 and
RS.300.00.
SOL:
select * from Library where price between 100.00 and 300.00;
The salary database of an organization has a table with the following attributes. The
primary keys are underlined and the data types are specified:
EMPSALARY(empcode: number, empname: text, dob: date, dept :text, salary: number
)
SOL:
create table empsalary(empcode int,empname varchar(20),dob datetime,dept
varchar(15),salary float);
exec sp_columns empsalary;
SOL:
insert into empsalary values(101,'A','02-jan-1991','COMPUTER',10000);
insert into empsalary values(102,'B','04-dec-1996','COMPUTER',20000);
insert into empsalary values(103,'C','06-nov-1978','COMPUTER',25000);
insert into empsalary values(104,'D','02-oct-1991','COMPUTER',40000);
insert into empsalary values(105,'E','06-sep-1979','COMPUTER',50000);
SOL:
select distinct dept,count(empcode) as "Total employees" from empsalary
group by dept;
SOL:
select sum(salary) sumofsalary from empsalary;
d) Find the sum and average of the salaries of employees of a particular department
8
SOL:
select dept,sum(salary) sumofsalary,avg(salary) avgsalary from empsalary
group by dept having dept='COMPUTER';
SOL:
select max(salary) as "Maximum salary" from empsalary;
SOL:
select min(salary) as "Minimum salary" from empsalary;
SOL:
select distinct dept,sum(salary) as "Total salary" from empsalary group by
dept;
h) Increase the salary of those employees working for the computer department by
Rs.1000.00
SOL:
select empcode,empname,salary "old salary",salary+1000 "new salary" from
empsalary where dept like 'COMPUTER';
9
4.INVENTORY DATABASE:
The inventory database has the following tables. The primary keys are underlined and
the data types are specified:
SOL:
create table item(itemcode int primary key,itemname varchar(10),price
float);
exec sp_columns items;
SOL:
insert into item values(1001,'PEN',7.50);
insert into item values(1002,'PENCIL',3.75);
insert into item values(1003,'CALCULATOR',275.50);
insert into item values(1004,'ERASER',3.00);
insert into item values(1110,'MARKER',50);
SOL:
select * from purchase;
d) Display the total items purchased (listing must have columns: itemcode,
itemname, totalquantity)
SOL:
select item.itemcode,item.itemname,purchase.quantity from item,purchase
where item.itemcode=purchase.itemcode;
SOL:
select itemname from item where item.itemcode not in (select itemcode from
purchase);
A bank customer database has two tables CUSTOMER and ACCOUNT. The primary
keys are underlined and the data types are specified:
CSUTOMER (custno: number, custname: text, city: text, accno: number, balance:
number )
ACCOUNT (accno number, acctype: text, branch: text, accstatus: text, chequefacility:
text )
a) Create the above tables and specify the primary and the foreign keys.
SOL:
create table account(accno int primary key,acctype varchar(10),branch
varchar(10),accstatus varchar(10),cheque_face varchar(1));
exec sp_columns account;
SOL:
insert into account values(101,'SAVINGS','MG ROAD','ACTIVE','Y');
insert into account values(102,'CURRENT','JC ROAD','INACTIVE','N');
insert into account values(103,'CURRENT','KH ROAD','INACTIVE','Y');
insert into account values(104,'SAVINGS','KR PURAM','ACTIVE','N');
SOL:
select distinct custname,city,cheque_face from customer,account where
city='BANGALORE' and cheque_face='y' and customer.accno=account.accno;
d) List the customers whose balance is greater than 3000.00 and have an active
account.
SOL:
select customer.custname from customer,account where
customer.balance>=3000 and account.accstatus='ACTIVE' and
customer.accno=account.accno;
SOL:
select sum(balance) tot_balance from customer where accno in (select accno
from account where branch='MG ROAD');
6.INSURANCE DATABASE:
13
Consider the Insurance database given below. The primary keys are underlined and the
data types are specified:
a) Create the above tables and specify the primary and the foreign keys.
SOL:
create table person(driverid int primary key,name varchar(5),address
varchar(20));
exec sp_columns person;
SOL:
insert into person values(12,'VIMAL','MG ROAD');
insert into person values(13,'KAMAL','KG ROAD');
insert into person values(15,'AJMAL','NICE ROAD');
insert into person values(16,'SUMAL','HOSA ROAD');
insert into person values(17,'RAVAN','HOSUR ROAD');
c) Update the damage amount for each accident and add a new accident to the
database.
SOL:
update participated set damageamt=15000 where driverid=17 and reportno
between 200 and 300;
select * from participated;
d) Find the total number of people who owned cars that were involved in accidents
in the year 2009.
SOL:
select count(*) accin2009 from accident where accdate between '01-jan-
2009' and '31-dec-2009';
16
e) Find the number of accidents in which cars belonging to a specific model were
involved.
SOL:
select count(*)accINDICA from car c,participated p where c.regno=p.regno
and model='INDICA';
17
a) Create the above tables by properly specifying the primary keys and the
foreign keys.
SOL:
create table customar(custid int primary key,name varchar(10),city
varchar(10));
exec sp_columns customar;
SOL:
insert into customar values(1,'AMIT','BANGALORE');
insert into customar values(2,'RAKESH','MANGALORE');
insert into customar values(3,'VIJAY','HUBLI');
insert into customar values(4,'SUMANTH','BELGAM');
insert into customar values(5,'VARUN','MYSORE');
SOL:
select c.name,count(co.orderno) no_og_order,avg(co.amount) avgorder_amt
from customar c,custorder co where c.custid=co.custid group by
c.name,co.custid;
d) List the order_no for the orders which were shipped from all the warehouses
that the company has in a specific way.
SOL:
select orderno,warehouseno from shipment where warehouseno in (select
warehouseno from warehouse where city='BANGALORE');
e) Demonstrate the deletion of itemno 10 from the ITEM table and make that
field null in the ORDER_ITEM table.
SOL:
delete from items where orderno=10;
update orderitem set itemno=null where itemno=10;
select * from orderitem;
20
Consider the following database of student enrollment in courses and books adopted for
each course.
a) Create the above tables by specifying the primary keys and the foreign keys.
SOL:
create table student(regno varchar(10),name varchar(20),major
varchar(10),bdate datetime,primary key(regno));
exec sp_columns student;
SOL:
insert into student values('07BSC1001','AMRUTHA','PHYSICS','02-JUN-1989');
insert into student values('07BSC1002','VANI','CHEMISTRY','18-SEP-1989');
insert into student values('07BSC1003','MANJARI','COMP SC','17-OCT-2009');
insert into student values('07BSC1004','SEERNA','MATHS','05-MAY-1985');
insert into student values('07BSC1005','USHA','COMP SC','3-FEB-1987');
c) Insert a new text book to the database and make this book be adopted by some
department.
SOL:
insert into student values('07BSC1006','Arun','COMP SC','3-FEB-1987');
insert into course values(23,'c++','comp sc');
insert into text values(8005,'C++','SUBHASH','M A RAMA');
SOL:
select name from student where regno in (select regno from enroll);
SOL:
select name from student1 where regno not in (select regno from enroll1);
SOL:
select title from text where isbn in( select isbn from bookadoption);
23
g) List any department that has all its adopted books published by a specific
publisher.
SOL:
select c.dept, c.cname from course c,bookadoption ba, text t where t.isbn
= ba.isbn and ba.courseno=c.courseno and t.publisher ='MCGRAWHILL';