DBMS LAB RECORD

27
NAME : MEHUL ASHARA COURSE: BCA Y2K8 SCHEME SUBJECT: 2 ND SEM BCA DBMS PRACTICAL RECORD

Transcript of DBMS LAB RECORD

Page 1: DBMS LAB RECORD

NAME : MEHUL ASHARA

COURSE: BCA Y2K8 SCHEME

SUBJECT: 2ND SEM BCA DBMS

PRACTICAL RECORD

Page 2: DBMS LAB RECORD

INDEX:

S.L. N.O. TOPIC DATE PAGE

1 STUDENT DETAILS DATABASE 08-03-2011 3-4

2 LIBRARY DATABASE 08-03-2011 5-6

3 EMPLOYEE SALARY DATABASE 11-30-2011 7-8

4 INVENTORY DATABASE 15-03-2011 9-10

5 BANK CUSTOMER DATABASE 18-03-2011 11-12

6 INSURANCE DATABASE 22-03-2011 13-16

7 ORDER PROCESSING DATABASE 05-04-2011 17-19

8 STUDENT ENROLLMENT DATABASE

08-04-2011 20-23

1.STUDENT DETAILS DATABASE:

2

Page 3: DBMS LAB RECORD

Create a student details database that has a table with the following attributes. The primary keys are underlined and the data types are specified:

STUDENT (regno: number, name: text, DOB: date, marks: number)

SOL:create table student(regno int primary key,name varchar(10),dob datetime,marks int);exec sp_columns student;

a) Remove the existing attribute marks from the table.

SOL:alter table student drop column marks;exec sp_columns student;

b) Change the data type of dob from integer to string.

SOL:alter table student alter column dob varchar(11);exec sp_columns student;

c) Add a new attribute phoneno to the existing table.

3

Page 4: DBMS LAB RECORD

SOL:alter table student add phonono int;exec sp_columns student;

d) Enter 5 tuples into the table.

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);

e) Display all the tuples in the student table.

SOL:select * from student;

2.LIBRARY DATABASE:

4

Page 5: DBMS LAB RECORD

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;

a) Enter 5 tuples into the table.

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);

b) Display the different publishers from the list.select distinct publisher from library;

c) Arrange the tuples in the alphabetical order of book titles.

5

Page 6: DBMS LAB RECORD

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;

3.EMPLOYEE SALARY DATABASE:

6

Page 7: DBMS LAB RECORD

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;

a) Enter 5 tuples into the table.

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);

b) Display the number of employees working in each department.

SOL:select distinct dept,count(empcode) as "Total employees" from empsalary group by dept;

c) Find the sum of salaries of all employees.

SOL:select sum(salary) sumofsalary from empsalary;

d) Find the sum and average of the salaries of employees of a particular department

7

Page 8: DBMS LAB RECORD

SOL:select dept,sum(salary) sumofsalary,avg(salary) avgsalary from empsalary group by dept having dept='COMPUTER';

e) Find the highest salary that an employee draws.

SOL:select max(salary) as "Maximum salary" from empsalary;

f) Find the least salary that an employee draws.

SOL:select min(salary) as "Minimum salary" from empsalary;

g) Find the total salary for each department.

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';

8

Page 9: DBMS LAB RECORD

4.INVENTORY DATABASE:

The inventory database has the following tables. The primary keys are underlined and the data types are specified:

ITEM (itemcode: number, itemname: text, price: number )PURCHASE (itemcode: number, quantity: number )

a) Create the tables with the above attributes.

SOL:create table item(itemcode int primary key,itemname varchar(10),price float);exec sp_columns items;

create table purchase(itemcode int,quantity int);exec sp_columns purchase;

b) Enter 5 tuples into the tables.

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);

insert into purchase values(1001,7);insert into purchase values(1002,17);insert into purchase values(1003,27);insert into purchase values(1006,29);

c) List the items purchased.

9

Page 10: DBMS LAB RECORD

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;

e) List the items which are not purchased by anyone.

SOL:select itemname from item where item.itemcode not in (select itemcode from purchase);

5.BANK CUSTOMER DATABASE:

10

Page 11: DBMS LAB RECORD

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;

create table customer(custno int primary key,custname varchar(10),city varchar(1),accno int foreign key references account(accno),balance decimal(10,2));exec sp_columns customer;

b) Enter 4 tuples for each relation.

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');

insert into customer values(1,'MAC','BANGALORE',101,2000);insert into customer values(2,'ZAK','BANGALORE',102,4000);insert into customer values(3,'KAT','BANGALORE',103,5000);insert into customer values(4,'LAM','BANGALORE',104,6000);

11

Page 12: DBMS LAB RECORD

c) List the customer from Bangalore who have cheque facility.

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;

e) Find the current outstanding balance amount of branch “MG Road” .

SOL:select sum(balance) tot_balance from customer where accno in (select accno from account where branch='MG ROAD');

6.INSURANCE DATABASE:

12

Page 13: DBMS LAB RECORD

Consider the Insurance database given below. The primary keys are underlined and the data types are specified:

PERSON (driverid: text, name: text, address: text )CAR (regno: text, model: text, year: number )OWNS (driverid: text, regno: text )ACCIDENT (reportno: nimber, accdate: date, location: text )PARTICIPATED (driverid: text, regno: text, reportno: number, dmgamt: number )

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;

create table car(regno int primary key,model varchar(10),year int);exec sp_columns car;

create table owns(driverid int foreign key references person(driverid),regno int foreign key references car(regno));exec sp_columns owns;

create table accident(reportno int primary key,accdate datetime,location varchar(10));exec sp_columns accident;

13

Page 14: DBMS LAB RECORD

create table participated(driverid int foreign key references person(driverid),regno int foreign key references car(regno),reportno int foreign key references accident(reportno),damageamt int);exec sp_columns participated;

b) Enter 5 tuples for each relation.

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');

insert into car values(021001,'LANCER',1998);insert into car values(021002,'CRUZER',1990);insert into car values(021003,'RANGER',2005);insert into car values(021004,'FERRARI',2010);insert into car values(021005,'INDICA',1996);

insert into owns values(12,021001);insert into owns values(13,021002);insert into owns values(15,021003);insert into owns values(16,021004);insert into owns values(17,021005); insert into accident values(12,'12-may-2010','DOMLUR');insert into accident values(250,'19-june-2009','RAJA NAGAR');insert into accident values(562,'05-aug-2000','JC ROAD');insert into accident values(145,'28-mar-2008','NICE ROAD');insert into accident values(288,'11-nov-2003','G ROAD');

insert into participated values(12,021001,12,5000);insert into participated values(13,021002,250,9000);insert into participated values(15,021003,562,4000);insert into participated values(16,021004,145,50000);insert into participated values(17,021005,288,55000);

14

Page 15: DBMS LAB RECORD

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;

insert into accident values(950,'29-may-2010','ELEC CITY');insert into participated values(null,null,null,30000);

select * from accident;

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';

15

Page 16: DBMS LAB RECORD

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';

16

Page 17: DBMS LAB RECORD

7.ORDER PROCESSING DATABASE:

Consider the following relations for an order processing database application in a company.

CUSTOMER (custid: number, custname: text, city: text)CUSTORDER (orderno: number, orderdate: date, custid: number, orderamount: number)ITEMS (itemno: number, itemname: text, unitprice: number)ORDER_ITEM (orderno: number, itemno: number, orditemqty: number, orderamt: number)WAREHOUSE (warehouseno: number, city: text)SHIPMENT (orderno: number, warehouseno: number, shipdate: date)

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;

create table custorder(orderno int primary key,date datetime,custid int foreign key references customar(custid),amount float);exec sp_columns custorder;

create table items(orderno int primary key,itemname varchar(10),unitprice float);exec sp_columns items;

create table orderitem(orderno int primary key,itemno int foreign key references items(orderno),orditemqty int);

17

Page 18: DBMS LAB RECORD

exec sp_columns orderitem;

create table warehouse(warehouseno int primary key,city varchar(10));exec sp_columns warehouse;

create table shipment(orderno int foreign key references custorder(orderno),warehouseno int foreign key references warehouse(warehouseno),shipdate datetime);exec sp_columns shipment;

b) Enter at least 5 tuples for each relation.

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');

insert into custorder values(10,'2-jan-2001',1,10000);insert into custorder values(20,'10-oct-2001',2,14000);insert into custorder values(35,'2-feb-2002',3,16000);insert into custorder values(100,'4-nov-2002',4,18000);insert into custorder values(165,'5-mar-2003',5,20000);

insert into items values(10,'CHIPS',500);insert into items values(12,'BURGER',300);insert into items values(14,'FRIES',200);insert into items values(16,'MAGGI',1000);insert into items values(18,'POP CORN',250);

insert into orderitem values(10,10,10);insert into orderitem values(20,14,5);insert into orderitem values(35,18,2);insert into orderitem values(100,12,4);insert into orderitem values(165,16,5);

insert into warehouse values(1,'PUNE');insert into warehouse values(2,'MUMBAI');insert into warehouse values(3,'COCHIN');

18

Page 19: DBMS LAB RECORD

insert into warehouse values(4,'BANGALORE');insert into warehouse values(5,'HUBLI');

insert into shipment values(10,1,'4-mar-2002');insert into shipment values(20,2,'7-dec-2002');insert into shipment values(35,3,'5-feb-2002');insert into shipment values(100,4,'8-may-2003');insert into shipment values(165,5,'11-aug-2003');

c) Produce a listing: custname, no_of_order, avgorder_amt where the middle attribute is the total average amount for the customer.

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;

19

Page 20: DBMS LAB RECORD

8.STUDENT ENROLLMENT DATABASE:

Consider the following database of student enrollment in courses and books adopted for each course.

STUDENT (RegNo: number, Name: text, major: text, BDate : date)COURSE (courseNo: number, courseName: text, Department: text)ENROLL (RegNo: number, courseNo : number, Semester: number, TotalMarks:number)TEXTBOOK (CourseNo: number, Bk_ISBN: Number, Semester:number)BOOK_ADOPTION (Course No: number, Bk_ISBN : number, number, semester:number)

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;

create table course(courseno int, cname varchar(10),dept varchar(10),primary key(courseno));exec sp_columns course;

create table enroll(regno varchar(10), courseno int, sem int not null,marks int, primary key(regno,courseno),foreign key(regno) references student(regno), foreign key(courseno) references course(courseno));exec sp_columns enroll;

20

Page 21: DBMS LAB RECORD

create table text(isbn int,title varchar(20),publisher varchar(20),author varchar(20),primary key(isbn));exec sp_columns text;

create table bookadoption(courseno int,sem int, isbn int,primary key(courseno,sem), foreign key(courseno) references course(courseno),foreign key(isbn) references text(isbn));exec sp_columns bookadoption;

b) Enter al least 5 tuples for each relation.

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');

insert into course values(10,'MODERNPHYS','PHYSICS');insert into course values(16,'NETWORKS','COMP SC');insert into course values(22,'ALGEBRA','MATHS');insert into course values(17,'DS','COMP SC');insert into course values(12,'OPTICS','PHYSICS');

insert into enroll values('07BSC1001',10,2,70);insert into enroll values('07BSC1002',16,1,80);insert into enroll values('07BSC1003',22,4,94);insert into enroll values('07BSC1004',17,3,65);insert into enroll values('07BSC1005',12,5,77);

insert into text values(7001,'MODERN PHYSICS','BPB','HALLIDAY');insert into text values(6050,'NETWORKS','MCGRAWHILL','STALLINGS');insert into text values(6900,'ALGEBRA','NOOTAN','ATRE JAYARAM');insert into text values(6255,'OPTICS','SUBHAS','RAJARAM');insert into text values(6179,'DS','SUBHAS','CHITRA RAVI');

insert into bookadoption values(10,1,7001);insert into bookadoption values(16,4,6050);insert into bookadoption values(22,3,6900);insert into bookadoption values(17,5,6255);insert into bookadoption values(12,3,6179);

21

Page 22: DBMS LAB RECORD

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');

d) List the name of the students who have been enrolled.

SOL: select name from student where regno in (select regno from enroll);

e) List the students who have registered but not enrolled.

SOL: select name from student1 where regno not in (select regno from enroll1);

f) List the books which have been adopted.

SOL: select title from text where isbn in( select isbn from bookadoption);

22

Page 23: DBMS LAB RECORD

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';

23