You are on page 1of 51

DBMS Lab Manual

Question #1: STUDENT DETAILS DATABASE:


The student details database has a table with the following attributes. The
primary key is underlined.

STUDENT( Regno: number, name: Text, DOB: date, marks: number)


1. Create the above table by specifying primary key.
2. Remove the existing attribute marks from the table.
3. Change the data type of regno from integer to string.
4. Add a new attribute PhoneNo to the existing table.
5. Enter 5 tuples into the table.
6. Display all the tuples in student table.

Question #2: Library database has a table with the following attributes

LIBRARY (BOOKID: Number, Title: Text, Author: Text, Publication: Text, Year_pub:
number, Price:number (6,2)).

1. Create the above table by specifying primary key.


2. Enter 5 tuples into tables.
3. Display all the tuples from the library database.
4. Display different publisher from the list
5. Arrange the tuple in the alphabetical order of the book titles.
6. List the details of all books whose price ranges between Rs. 100 and Rs 300.

Question #3: EMPLOYEE SALARY DATABASE


The salary database of an organization has a table with the following attributes. The
primary key is underlined.

EMPSALARY(empcode: number, empname: text, dob: date, dept:text, salary:


number(10,2)

1. Create the above table by specifying primary key.


2. Enter 5 tuples into the table
3. Display the number of employees working in each department.
4. Find the sum of the salaries of all employees
5. Find the sum and average of the salaries of employees of a particular department.
6. Find the least and highest salary that an employee draws

Question #4: INSURANCE DATABASE

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: number, AccDate: date, Location: text)
PARTICIPATED(DriverId: text, Regno: text, Reportno: number, Dmg_amt:
number(10,2))

1. Create the above tables by specifying primary key and foreign keys.
2. Enter at least 5 tuples in each relation.

Department of BCA -AIMS Page 1


DBMS Lab Manual

i. Update the damage amount of the car with a specific regno in accident with
report number 12 to 25000.
ii. Add a new accident to the database
3. Find the total number of people who owned cars that were involved in accident in 2002
4. Find the number of accident in which car belonging to a specific model were involved.

Question #5:Student Enrollment

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

STUDENT (regno: string, name: string, major: string, bdate:date)


COURSE (courseno: int, cname: string, dept: string)
ENROLL (regno: string, courseno: int, marks: int)
TEXT (book_ISBN: int, book-title: string, publisher: string, author: string)
BOOK_ADOPTION (courseno: int, sem:int, book_ISBN:int)

1) create the above tables by properly specifying the primary keys and foreign keys
2) Enter five tuples for each relation
3) Demonstrate how you add a new text book to the database and make this book be
adopted by some department
4) Produce a list of text books (Include Course_no, Book_ISBN, Book_Title) in the
Alphabetical order for Course offered by ‘BCA’ Department that use more than two books
5) List any department that has all its adopted books published by a specific publisher

Question #6: BANK DATABASE Consider the following database for BANK
BRANCH(Bname: text, Bcity text, Asset number)
ACCOUNT( Accno: int, Bname String, balance real)
CUSTOMER(CName: string, Cstreet: string, Ccity string)
DEPOSITOR(Cname: string, Accno int)
LOAN(loan_no int: Bname: string, amount real)
BORROWER(Cname string, Loan_no int)

1. Create above tables by properly specifying the primary keys and the foreign keys
2. Enter atleast five tuples for each realtions
3. Find all the customer who have atleat two accounts at the main Branch (Ex: KG
Road)
4. Find all Customer who have an account at all the Branches located in a specific
city (Bangalore)
5. Demonstrate how you delete all Account tuples at every branch located in a
specific city (Ex: Bangalore)

Question #7:consider the following database for Ordering Processing

CUSTOMER(cust_no: int, Cname: string, City: String)


ORDER(Orderno: int, cust_no int, odate: date,ord_amt real);
ITEM(Itemno:int, unit_price: real)
ORDER_ITEM(Orderno: int, itemno: int, qty:int)
WAREHOUSE(Warehouseno:int, city:string)
SHIPMENT(orderno:int, warehouseno:int, ship_date: date)

1. create above tables by properly specifying primary keys and foreign keys.
2. Enter atleast five tuples for each realtions
3. List the Order number and Shipdate for all orders shipped from a particular
warehouse.
Department of BCA -AIMS Page 2
DBMS Lab Manual

4. Produce a listing Customer Name, No.of Orders,Avg Oreder AMount where the
middle column is the total average Order Amount for that customer.
5. List the orders that were not shipted within 30 days of ordering.
Part B
1. Consider the following table with the mapping given below.
RETAILOR(Cust_id: int, Cust_name: string , place: string, ph_no integer, pan_no: string).
a) Create the above table by specifying primary key.
b) Alter the table to add a column pin_no.
c) Enter 5 tuples into the table.
d) Display the details of customer who are all living in Bangalore.
e) Display the customer name whose first letter is ‘S’.

2. Consider the following employee and department tables.


DEPARTMENT(deptno: integer, dname:string, location: string)
EMPLOYEE(empno: integer, ename: string, designation: string, manager: string, hiredate:
date, salary: number(8,2), deptno: integer)

a) Create the above tables by properly specifying the primary keys and foreign keys.
b) Enter at least five tuples for each relation.
c) List the employees of employee table in descending order of their salaries.
d) List the details of highest paid employee in ‘SALES’ department.
e) List the employee number, employee name and department name for all employee of
departments in 50 and 60.

3. Create the following tables with the mapping given below.


STU_DETAILS (reg_no: integer, stu_name: string, DOB: date, address: string, city: string)
MARK_DETAILS (reg_no: integer, marks1: integer, mark2: integer, mark3: integer, total:
integer)
a) Create the above table by specifying primary key.
b) Enter 5 tuples into the table.
c) Display only those rows whose total ranges between 250 and 300.
d) Drop the table mark_details.
e) Delete the row whose reg_no=161.
f) List all the student detail that who are all located in Tumkur.

4. An inventory database has the following tables. The primary keys are underlined.
ITEM(itemno: number, ItemName:Text, Price: Number(10,2))
PURCHASE(itemno: number, Quantity: number)
a) Create the above tables by specifying primary key and foreign keys.
b) Enter 5 tuples into the tables
c) List the item purchased.
d) Display the total items purchased (listing must have the columns; itemcode,
itemname, totalQuantity)
e) List the items which are not purchased by any one.
5. A bank customer database has two tables CUSTOMER and ACCOUNT
ACCOUNT(Accno: number, Acctype: text, Branch: text, AccStatus: text,
ChequeFacility: Text)
CUSTOMER (Custno: number, Custname: Text, City :text, AccNo: Number, Balance:
Number(10,2))

a) Create the above tables by specifying primary key and foreign keys.
b) Enter 5-8 tuples into the tables
c) List the customer from “Bangalore” who have cheque facility.
d) List all the customer whose balance is grater than 3000 and have an active account.
e) Find the current outstanding balance amount of branch “Peenya”.

6. Write a PL/SQL Program to find factorial of a number using for-loop.


Department of BCA -AIMS Page 3
DBMS Lab Manual

7. Write a PL/SQL program to find result of the student.


8. Write a PL/SQL Program to build Mathematical table using while Loop.
9. Write a PL/SQL Program to generate Fibonacci Series up to N
10. Write a PL/SQL program to check whether given number is palindrome or not.

Department of BCA -AIMS Page 4


DBMS Lab Manual

Question #1: STUDENT DETAILS DATABASE:


The student details database has a table with the
following attributes. The primary key is underlined.

STUDENT( Regno: number, name: Text, DOB: date, marks:


number)
1. Create the above table by specifying primary key.
2. Remove the existing attribute marks from the table.
3. Change the data type of regno from integer to string.
4. Add a new attribute PhoneNo to the existing table.
5. Enter 5 tuples into the table.
6. Display all the tuples in student table.

1. Create the above table by specifying primary key.


create table student (Regno numeric(10) primary key,
Name varchar(15) not null,
DateofBirth datetime not null,
marks numeric(3));

Display Schema
sp_help student

Column Name Data Type Size constraint


Regno numeric 10 Primary Key
Name varchar 15 not null
DateofBirth datetime 8 not null
marks numeric 3

2. Remove the existing attribute marks from the table.


alter table student drop column marks

sp_help student
Column Name Data Type Size constraint
Regno numeric 10 Primary Key
Name varchar 15 not null
DateofBirth datetime 8 not null

3. Change the datatype of regno from integer to string.


alter table student drop constraint PK__student__78D3EB5B

alter table student alter column regno varchar(10)

sp_help student
Column Name Data Type Size constraint
Regno varchar 10
Name varchar 15 not null
DateofBirth datetime 8 not null

Department of BCA -AIMS Page 5


DBMS Lab Manual

4. Add a new attribute PhoneNo to the existing table.


alter table student add PhoneNo numeric(10);

sp_help student
Column Name Data Type Size constraint
Regno varchar 10
Name varchar 15 not null
DateofBirth datetime 8 not null
PhoneNo Numeric 10

5. Enter 5 tuples into the table.

insert into student values ('17SKSB7001','Abhijith','07-05-1999',9898989898)


insert into student values ('17SKSB7002','Ajay','12-03-1999', 9797979797)
insert into student values ('17SKSB7003', 'Anik Das ', '03-30-1997',9393939393)
insert into student values ('17SKSB7004', 'Ankith ', '10-22-1999', 9292929292)
insert into student values ('17SKSB7005', 'Deepak ', '11-02-2000', 9494949494)

6. Display all the tuples in student table.

SELECT * FROM STUDENT


Regno Name DateofBirth Phone Number
17SKSB7001 Abhijith 1999-07-05 9898989898
17SKSB7002 Ajay 1999-12-03 9797979797
17SKSB7003 Anik Das 1997-03-30 9393939393
17SKSB7004 Ankith 1999-10-22 9292929292
17SKSB7005 Deepak 2000-11-02 9494949494

Department of BCA -AIMS Page 6


DBMS Lab Manual

Question #2: Library database has a table with the following


attributes

Library(BOOKID: Number, Title: Text, Author: Text,


Publication: Text, Year_pub: number, Price number(6,2)).

1. Create the above table by specifying primary key.


2. Enter 5 tuples into tables.
3. Display all the tuples from the library database.
4. Display different publisher from the list
5. Arrange the tuple in the alphabetical order of the book
titles.
6. List the details of all books whose price ranges between
Rs. 100 and Rs 300.

1. Create the above table by specifying primary key.

create table library(Bookid numeric(10) primary key,


Title varchar(20) not null,
Author varchar(20) not null,
Publisher varchar(20) not null,
Year_of_publication numeric(5),
price numeric(6,2));

sp_help library

column name type size


Bookid numeric 10
Title varchar 20
Author varchar 20
Publisher varchar 20
Year_of_publication numeric 5
price numeric 6,2

2. Enter 5 tuples into the table

insert into library values


(1001, 'DBMS', 'Sreekanth', 'Sumuka', 1995,254.12)
insert into library values
(1003, 'JAVA', 'Sumanth', 'Pearson', 2010,35.812)
insert into library values
(1006, 'DCN', 'ujjwal pal', 'AIMS', 2009, 954.12)
insert into library values
(1008, 'VB', 'Revathi', 'PHI', 2005, 554.89)
insert into library values
(1010, 'DCN', 'Manjutha','Skyword',2005,171.12)
insert into library values
(1014,'C++','Manu','skyword',1985,54.18)

3. Display all the tuples in the library table

Department of BCA -AIMS Page 7


DBMS Lab Manual

select * from library

Bookid Title Author Publisher Year Price


1001 DBMS Sreekanth Sumuka 1995 254.12
1003 JAVA Sumanth Pearson 2010 35.81
1006 DCN ujjwal pal AIMS 2009 954.12
1008 VB Revathi PHI 2005 554.89
1010 DCN Manjutha Sky word 2005 171.12
1014 C++ Manu skyword 1985 54.18

4. Display the different publishers from the list

select Distinct(publisher) from library

Publisher
AIMS
Pearson
PHI
Sky word
Sumuka

5. Arrange the tuples in the alphabetical order of the book


titles
select * from library order by Title

Bookid Title Author Publisher Year Price


1014 C++ Manu skyword 1985 54.18
1001 DBMS Sreekanth Sumuka 1995 254.12
1006 DCN ujjwal pal AIMS 2009 954.12
1010 DCN Manjutha Sky word 2005 171.12
1003 JAVA Sumanth Pearson 2010 35.81
1008 VB Revathi PHI 2005 554.89

6. List details of all the books whose price ranges between


Rs. 100.00 and Rs. 300.00.

select Bookid, Title, price from library where price


between 100 and 300;

Bookid Title Price


1001 DBMS 254.12
1010 DCN 171.12

Department of BCA -AIMS Page 8


DBMS Lab Manual

Question #3: EMPLOYEE SALARY DATABASE


The salary database of an organization has a table with the
following attributes. The primary key is underlined.

EMPSALARY(empcode: number, empname: text, dob: date,


dept:text, salary: number(10,2)

1. Create the above table by specifying primary key.


2. Enter 5 tuples into the table
3. Display the number of employees working in each department.
4. Find the sum of the salaries of all employees
5. Find the sum and average of the salaries of employees of a
particular department.
6. Find the least and highest salary that an employee draws

1. Create the above table by specifying primary key.


create table empsalary(
empcode numeric(20),
empname varchar(20),
dob datetime,
dept varchar(20),
salary numeric(10,2),
primary key(empcode));

sp_help empsalary;

column name type size


empcode numeric 20 0
empname varchar 20
dob datetime 8
dept varchar 20
salary numeric 10 2

2. Enter 5 tuples into the table


insert into empsalary values
(1,'rakesh','06-12-1990','computer',15000);
insert into empsalary values
(2,'sourish','03-05-1992','library',10000);
insert into empsalary values
(3,'parth','02-12-1995','itsecurity',20000);
insert into empsalary values
(4,'rahul','03-06-1995','hr',250000);
insert into empsalary values
(5,'raj','08-04-1990','marketing',50000);
insert into empsalary values
(6,'ram','06-05-1981','hr',100);

select * from empsalary;

Department of BCA -AIMS Page 9


DBMS Lab Manual

EMPCODE EMPNAME DOB DEPT SALARY


1 rakesh 1990-06-12 computer 16000.00
2 sourish 1992-03-05 library 10000.00
3 parth 1995-02-12 it-security 20000.00
4 rahul 1995-03-06 hr 250000.00
5 raj 1990-08-04 marketing 50000.00
6 ram 1981-06-05 hr 100.00

3. Display the number of employees working in each department.

select dept,count(empcode)'number of emp' from empsalary group


by dept;

dept number of emp


computer 1
hr 2
itsecurity 1
library 1
marketing 1

4. Find the sum of the salaries of all employees

select sum(salary)'sum of salary'from empsalary;

sum of salary
346100.00

5. Find the sum and average of the salaries of employees of


a particular department.

select sum(salary)'sum of salary', avg(salary)'avg salary'


from empsalary where dept = 'hr';

sum of salary avg salary


250100.00 125050.00

6. Find the highest salary that an employee draws

select max(salary)'max salary' from empsalary;

max salary
250000.00

7. Find the least salary that an employee draws

select min(salary)'min salary' from empsalary;

min salary
100.00
Department of BCA -AIMS Page 10
DBMS Lab Manual

Question #4: INSURANCE DATABASE


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: number, AccDate: date, Location: text)
PARTICIPATED(DriverId: text, Regno: text, Reportno: number,
Dmg_amt: number(10,2))

5. Create the above tables by specifying primary key and


foreign keys.
6. Enter at least 5 tuples in each relation.
i. Update the damage amount of the car with a specific
regno in accident with report number 12 to 25000.
ii. Add a new accident to the database
7. Find the total number of people who owned cars that were
involved in accident in 2002
8. Find the number of accident in which car belonging to a
specific model were involved.

1. Create the above tables by specifying primary key and


foreign keys.

Create table PERSON(


DriverId varchar(10),
Name varchar(15),
Address varchar(20),
primary key (DriverId));

sp_help person;

Column name type size


DriverId varchar 10
Name varchar 15
Address varchar 20

Create table CAR(


Regno varchar(10),
Model varchar(15),
Year numeric(6),
primary key(Regno));

sp_help CAR;

Column name type size


Regno varchar 10
Model varchar 15
Year numeric 5

Department of BCA -AIMS Page 11


DBMS Lab Manual

Create table OWNS(


DriverId varchar(10),
Regno varchar(10),
foreign key (DriverId) references PERSON(DriverId),
foreign key (Regno) references CAR(Regno));

sp_help Owns;
Column name type size
DriverId varchar 10
Regno varchar 10

Create table ACCIDENT(


Reportno numeric(10),
AccDate datetime,
Location varchar(30),
primary key (Reportno));

sp_help Accident;

Column name type size


Reportno numeric 10
AccDate datetime 8
Location varchar no 30

Create table PARTICIPATED(


DriverId varchar(10),
Regno varchar(10),
Reportno numeric(10),
Dmg_amt numeric(10,2),
foreign key (DriverId) references PERSON(DriverId),
foreign key (Regno) references CAR(Regno),
foreign key (Reportno) references ACCIDENT(Reportno));

sp_help Participated;

Column name type size


DriverId varchar 10
Regno varchar 10
Reportno numeric 10
Dmg_amt numeric 10,2

2. Enter at least 5 tuples in each relation.


insert into person values('D101', 'Pradeep', '#21 Peenya');
insert into person values('D102', 'Mallika', '#14 Delhi');
insert into person values('D103', 'Vinay', '#27 Mysore');
insert into person values('D104', 'Rakesh', '#75 Tumkur');
insert into person values('D105', 'Vasanth', '#32 Hubli');

select * from Person;

DriverId Name Address


D101 Pradeep #21 Peenya
D102 Mallika #14 Delhi
Department of BCA -AIMS Page 12
DBMS Lab Manual

D103 Vinay #27 Mysore


D104 Rakesh #75 Tumkur
D105 Vasanth #32 Hubli

insert into Car values('KA06M6363', 'Maruthi', 2001);


insert into Car values('KA01N5454', 'BMW', 2005);
insert into Car values('KA09Z7272', 'BENZ', 2010);
insert into Car values('KA36M1818', 'Scorpio', 2008);
insert into Car values('KA54A8181', 'BMW', 2008);

select * from Car;

RegNO Model Year


KA01N5454 BMW 2005
KA06M6363 Maruthi 2001
KA09Z7272 BENZ 2010
KA36M1818 Scorpio 2008
KA54A8181 BMW 2008

insert into owns values('D101', 'KA54A8181');


insert into owns values('D104', 'KA09Z7272');
insert into owns values('D102', 'KA01N5454');
insert into owns values('D103', 'KA36M1818');
insert into owns values('D105', 'KA06M6363');

select * from owns;

DriverId RegNo
D101 KA54A8181
D104 KA09Z7272
D102 KA01N5454
D103 KA36M1818
D105 KA06M6363

insert into Accident values


(12, '02-Dec-2003', ' Jalahalli Cross');
insert into Accident values
(25, '25-Apr-2009', '8th Mail');
insert into Accident values
(100, '11-Mar-2008', ' Yeshwantpur');
insert into Accident values
(1001, '27-May-2009', ' Tumkur');
insert into Accident values
(2001, '29-Feb-2003', ' BEL Road');

select * from Accident;

ReportNo AccDate Location


12 2003-12-02 Jalahalli Cross
25 2009-04-25 8th Mail
100 2008-03-11 Yeshwantpur
1001 2009-05-27 Tumkur
2001 2003-02-27 BEL Road

Department of BCA -AIMS Page 13


DBMS Lab Manual

insert into Participated values('D101', 'KA54A8181', 25, 25000.36 );


insert into Participated values('D104', 'KA09Z7272', 12, 9999.99);
insert into Participated values('D102', 'KA01N5454',100, 788787.90);
insert into Participated values('D103', 'KA36M1818', 2001,5454.54 );
insert into Participated values('D105', 'KA06M6363', 1001, 3636.36);

select * from participated;

DriverId Regno ReportNo Dmg_amt


D101 KA54A8181 25 25000.36
D104 KA09Z7272 12 9999.99
D102 KA01N5454 100 788787.90
D103 KA36M1818 2001 5454.54
D105 KA06M6363 1001 3636.36

3.1 Update the damage amount of the car with a specific regno
in accident with report number 12 to 25000.

Before Updating:

select * from participated where reportno = 12;

DriverId Regno ReportNo Dmg_amt


D104 KA09Z7272 12 9999.99

update Participated set Dmg_amt = 25000 where reportno = 12;

After Updating
select * from participated where reportno = 12;
DriverId Regno ReportNo Dmg_amt
D104 KA09Z7272 12 25000.00

3.2 Add a new accident to the database


insert into person values
('D106', 'Himanshu', '#144 Dasarhali');
insert into Car values
('KA54A2727', 'Invova', 2009);
insert into Accident values
(3006, '18-Jun-2014', ' Peenya');
insert into owns values
('D106', ' KA54A2727');
insert into Participated values
('D106', ' KA54A2727', 3006, 9696.00);

select * from Accident where reportNo =3001

ReportNo AccDate Location


3001 2003-06-27 BEL Road

select * from participated where reportNo =3001

Department of BCA -AIMS Page 14


DBMS Lab Manual

DriverId Regno ReportNo Dmg_amt


D103 KA09Z7272 3001 6969.00

4 Find the total number of people who owned cars that were
involved in accident in 2009

select count(*) 'No of Accident in 2009'from Accident


where convert (varchar(10),AccDate,105) like '%2009%';

No of Accident in 2009
2

5 Find the number of accident in which car belonging to a


specific model were involved.

select count(*) 'No of Accident' from Car c,Participated p


where c.regno = p.regno and c.model = 'BMW';

'No of Accident'
2

Department of BCA -AIMS Page 15


DBMS Lab Manual

Question #5:Student Enrollment


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

STUDENT (regno: string, name: string, major: string,


bdate:date)
COURSE (courseno: int, cname: string, dept: string)
ENROLL (regno: string, courseno: int, marks: int)
TEXT (book_ISBN: int, book-title: string, publisher: string,
author: string)
BOOK_ADOPTION (courseno: int, sem:int, book_ISBN:int)

6) create the above tables by properly specifying the primary


keys and foreign keys
7) Enter five tuples for each relation
8) Demonstrate how you add a new text book to the database and
make this book be adopted by some department
9) Produce a list of text books (Include Course_no, Book_ISBN,
Book_Title) in the Alphabetical order for Course offered by
‘BCA’ Department that use more than two books
10) List any department that has all its adopted books
published by a specific publisher

1) create the above tables by properly specifying the primary


keys and foreign keys
create table Student
(Regno varchar(10),
Name varchar(10) not null,
Major varchar(10) not null,
Bdate datetime,
primary key (Regno));

sp_help Student;

Column name type size


Regno varchar 10
Name varchar 10
Major varchar 10
Bdate datetime 8

create table Course


(Courseno integer,
Cname varchar(10) not null,
Dept varchar(10) not null,
primary key (Courseno));

sp_help Course

Column name type size


Courseno int 10
Cname varchar 10
Dept varchar 10

Department of BCA -AIMS Page 16


DBMS Lab Manual

create table Enroll(


Regno varchar(10),
Courseno integer,
Sem integer not null,
Marks integer,
foreign key (Regno) references Student(Regno),
foreign key (Courseno) references Course(Courseno));

sp_help Enroll

Column name type size


Regno varchar 10
Courseno int 10
Sem int 10
Marks int 10

create table Text(


ISBN integer,
Booktitle varchar(20) not null,
Publisher varchar(20),
Author varchar(15),
primary key (ISBN));

sp_help Text

Column name type size


ISBN int 10
Booktitle varchar 20
Publisher varchar 20
Author varchar 15

create table Book_adoption(


Courseno integer,
Sem integer,
ISBN integer,
foreign key (Courseno) references Course(Courseno),
foreign key (ISBN) references Text(ISBN));

sp_help Book_adoption

Column name type size


Courseno int 10
Sem int 10
ISBN int 10

2) Enter five tuples for each relation


insert into Student values
('14SKSB7001','Divya','BBM','15-Apr-1996');
insert into Student values
('14SKSB7002','Mohan','MCA','02-Jan-1994');
insert into Student values
('14SKSB7003','Manjula','MBA','01-Dec-1995');
insert into student values
('14SKSB7004','Sachin','BCOM','18-Jul-1996');
Department of BCA -AIMS Page 17
DBMS Lab Manual

insert into student values


('14SKSB7005','Alok','BCA','18-Aug-1997');
insert into Student values
('14SKSB7006','Dilip','MCA','30-Apr-1995');
insert into Student values
('14SKSB7007','Karan','BCA','02-Jan-1995');

SELECT * FROM STUDENT;

REGNO NAME MAJOR BDATE


14SKSB7001 Divya BBM 1996-04-15
14SKSB7002 Manjula MBA 1995-12-01
14SKSB7003 Sachin BCOM 1996-07-18
14SKSB7004 Mohan MCA 1994-01-02
14SKSB7005 Dilip MCA 1995-04-30
14SKSB7006 Karan BCA 1995-01-02
14SKSB7007 Alok BCA 1997-08-18

insert into Course values(11, 'BCA','IT');


insert into Course values(22, 'BBM','MGT');
insert into Course values(33, 'BCOM','COM');
insert into Course values(44, 'MCA','IT');
insert into Course values(55, 'MBA','MGT');

Select * from Course;

COURSENO CNAME DEPT


11 BCA IT
22 BBM MGT
33 BCOM COM
44 MCA IT
55 MBA MGT

insert into enroll values('14SKSB7002',55,3,75);


insert into enroll values('14SKSB7005',44,2,60);
insert into enroll values('14SKSB7001',22,4,55);
insert into enroll values('14SKSB7003',33,1,70);
insert into enroll values('14SKSB7007',11,2,92);

select * from enroll;

REGNO COURSENO SEM MARKS


13SKSB6002 55 3 75
13SKSB6005 44 2 60
13SKSB6001 22 4 55
13SKSB6003 33 1 70
13SKSB6007 11 2 92

insert into text values(7272, 'DBMS', 'Swapna', 'Sreenivasa');


insert into text values(7171, 'Banking', 'PHI', 'Harinath');
insert into text values(7373, 'C Prog', 'Subhas', 'Ujjwal');
insert into text values(7474, ' MIS', 'PHI', 'G R Pandith');
insert into text values(7575,'Visual Prog.','Swapna','Visalatchi');
Department of BCA -AIMS Page 18
DBMS Lab Manual

select * from text


ISBN BOOKTITLE PUBLISHER AUTHOR
7171 Banking PHI Harinath
7272 DBMS Swapna Sreenivasa
7373 C Prog Subhas Ujjwal
7474 MIS PHI G R Pandith
7575 Visual Prog. Swapna Visalatchi

insert into book_adoption values(11,3,7272);


insert into book_adoption values(22,2,7171);
insert into book_adoption values(11,1,7272);
insert into book_adoption values(44,4,7373);
insert into book_adoption values(22,3,7575);

select * from book_adoption;

COURSENO SEM ISBN


11 3 7272
22 2 7171
11 1 7272
44 4 7373
22 3 7575

3) Demonstrate how you add a new text book to the database and
make this book be adopted by some department

insert into text values(7878,'Management','Sapna','Giridhar');


(1 row(s) affected)

insert into book_adoption values(11,3,7878);


(1 row(s) affected)

select * from text ;

ISBN BOOKTITLE PUBLISHER AUTHOR


7171 Banking PHI Harinath
7272 DBMS Swapna Sreenivasa
7373 C Prog Subhas Ujjwal
7474 MIS PHI G R Pandith
7575 Visual Prog. Swapna Visalatchi
7878 Management Sapna Giridhar

select * from book_adoption;

COURSENO SEM ISBN


11 3 7272
22 2 7171
11 1 7272
44 4 7373
22 3 7575
11 3 7878

Department of BCA -AIMS Page 19


DBMS Lab Manual

4) Produce a list of text books (Include Course_no, Book_ISBN,


Book_Title) in the Alphabetical order for Course offered by
‘BCA’ Department that use more than two books

create view BCAdept as (select C.DEPT, C.COURSENO,


T.BOOKTITLE,T.ISBN from COURSE C, BOOK_ADOPTION BA, TEXT T
where BA.COURSENO = C.COURSENO and T.ISBN =BA.ISBN and DEPT
='IT' );

select * from BCAdept ORDER BY BOOKTITLE

Dept CourseNo BookTitle ISBN


IT 44 C.Prog 7373
IT 11 DBMS 7272

SELECT COURSENO,ISBN, BOOKTITLE FROM COMPDEPT7WHERE DEPT IN


(SELECT DEPT FROM BCAdept GROUP BY DEPT HAVING COUNT(*)>=2);

CourseNo ISBN BookTitle


44 7373 C.Prog
11 7272 DBMS

5) List any department that has all its adopted books published
by a specific publisher

select C.DEPT,C.CNAME from COURSE C, BOOK_ADOPTION BA, TEXT T


where T.ISBN =BA.ISBN and BA.COURSENO = C.COURSENO and
T.PUBLISHER ='Swapna';

DEPT CNAME
IT BCA
IT BCA
MGT BBM

Department of BCA -AIMS Page 20


DBMS Lab Manual

Question #06: The following tables are maintained by a book


dealer

Author(Author_id:int,name:string,city:string,country String)
Publisher(Publisher_id:int,Pname:String,city: String,
country:String)
Category(Category_id:int,Description varchar(30),
Catalog(Book_id:int,Title:String,Authour_id:int,Publisher_id:i
nt,Category_id:int,year:int,price real)
Order_details(Order_id:int,Book_id:int,Quantity:int)

1. Create above tables by properly specifying the primary keys


and the foreign keys.

create table Author(


Author_id int,
name varchar(20),
city varchar(20),
country varchar(20),
primary key(Author_id));

create table Publisher(


Publisher_id int,
Pname varchar(20),
city varchar(20),
country varchar(20),
primary key (Publisher_id));

create table Category(


Category_id int,
Description varchar(30),
primary key (Category_id));

create table Catalog(


Book_id int,
Title Varchar(20),
Authour_id int,
Publisher_id int,
Category_id int,
year int,
price numeric(8,2),
primary key(Book_id),
foreign key (Authour_id) references Author(Author_id),
foreign key(Publisher_id) references
Publisher(Publisher_id),
foreign key(Category_id) references Category(Category_id));

Department of BCA -AIMS Page 21


DBMS Lab Manual

create table Order_details(


Order_id int,
Book_id int,
Quantity int,
primary key(Order_id),
foreign key(Book_id) references Catalog(Book_id));

2. Enter atleast five tuples for each realtions

insert into Author values(101,'Malini','Tumkur','India');


insert into Author values(102,'Navathe','London','UK');
insert into Author values(103,'Swamy','Newdelhi','India');
insert into Author values(104,'Vidya','Newyark','USA');
insert into Author values(105,'Geetha','Mysore','India');

select * from author

Author_id Namea City Country


101 Malini Tumkur India
102 Navathe London UK
103 Swamy Newdelhi India
104 Vidya Newyark USA
105 Geetha Mysore India

insert into Publisher values(10,'PHI','London','UK');


insert into Publisher values(20,'Wiley','Newdelhi','India');
insert into Publisher values(30,'Pearson','Newyark','USA');
insert into Publisher values(40,'Oxford','London','UK');
insert into Publisher values(50,'Kalyani','Chennai','India');

select * from Publisher;

Publisher_id Name City Country


10 PHI London UK
20 Wiley Newdelhi India
30 Pearson Newyark USA
40 Oxford London UK
50 Kalyani Chennai India

insert into Category values(1001,'Database Mangement System');


insert into Category values(1002,'Software Engineering');
insert into Category values(1003,'Data Communication Network');
insert into Category values(1004, 'Data Structure Using C');
insert into Category values(1005, 'Operating System');

select * from Category

Department of BCA -AIMS Page 22


DBMS Lab Manual

Category_id Descriptiobbn
1001 Database Mangement System
1002 Software Engineering
1003 Data Communication Network
1004 Data Structure Using C
1005 Operating System

insert into catalog values(1111,'DBMS',101,10,1002,2014,250);


insert into catalog values(2222,'DS',103,10,1001,2014,150);
insert into catalog values(3333,'DCN',104,20,1003,2012,100);
insert into catalog values(4444,'OS',102,30,1004,2013,300);
insert into catalog values(5555,'SE',105,40,1003,2015,320);

select * from catalog;

Book_id Title Author_id Publisher_id Category_id Year Price


1111 DBMS 101 10 1002 2014 250.00
2222 DS 103 10 1001 2014 150.00
3333 DCN 104 20 1003 2012 100.00
4444 OS 102 30 1004 2013 300.00
5555 SE 105 40 1003 2015 220.00

insert into Order_details values(1,1111,20);


insert into Order_details values(2,2222,30);
insert into Order_details values(3,3333,40);
insert into Order_details values(4,4444,50);
insert into Order_details values(5,5555,60);

select * from Order_details

Order_no Book_id Quantity


1 1111 20
2 2222 30
3 3333 40
4 4444 50
5 5555 60

3. 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 of publicationis after 2010

select c.Author_id,a.name from catalog c, Author a where


a.Author_id=c.Author_id and c.Year >2010 and c.price
>(select avg(price)from catalog group by author_id having
count(*) >=2);

Department of BCA -AIMS Page 23


DBMS Lab Manual

Authour_ID Name
101 Malini
103 Swamy

4. Find the author of the book which has maximum sales

create view sales as (select od.book_id as book_no,


c.price as cost, sum(od.Quantity) as qty,
sum(od.Quantity*c.price) as sales
from Order_details od, catalog c, Author a
where od.book_id = c.book_id and c.author_id = a.author_id
group by od.book_id, c.price);

select * from sales

Book_no Cost Qty Sales


1111 250.00 20 5000.00
2222 150.00 30 4500.00
3333 100.00 40 4000.00
4444 300.00 50 15000.00
5555 320.00 60 19200.00

select a.author_id, a.name, s.Book_no, s.sales


from author a, catalog c, sales s
where a.author_id = c.author_id and s.book_no = c.book_id and
sales = (select max(sales) from sales);

Author_id Name Book_no Sales


104 Vidya 3333 4000.00

5. demonstrate how you increase the price of the books


published by a specific publisher by 10%

update catalog set price = price*1.10 where publisher_id =


10;

Book_ID Title Author_ID Publisher_Id Cateory_ID Year Price


1111 DBMS 101 10 1002 2014 275.00
2222 DS 103 10 1001 2014 165.00
3333 DCN 104 20 1003 2012 100.00
4444 OS 102 30 1004 2013 300.00
5555 SE 105 40 1003 2015 220.00
6666 SE 105 40 1003 2015 80.00
7777 SE 105 40 1003 2015 90.00

Department of BCA -AIMS Page 24


DBMS Lab Manual

Question #7: BANK DATABASE Consider the following database


for BANK
Branch(Bname: text, Bcity text, Asset number)
Account( Accno: int, Bname String, balance real)
Customer(CName: string, Cstreet: string, Ccity string)
Depositor(Cname: string, Accno int)
Loan(loan_no int: Bname: string, amount real)
Borrower(Cname string, Loan_no int)

6. Create above tables by properly specifying the primary


keys and the foreign keys

create table Branch (


Bname varchar(20),
Bcity varchar(20) not null,
Asst numeric(10,2),
primary key(Bname));

create table Account(


Accno int,
Bname varchar(20),
Balance Numeric(10,2),
primary key(accno),
foreign key(Bname) references Branch(Bname));

Customer(CName: string, Cstreet: string, Ccity string)


create table Customer1(
Cname varchar(10),
Cstreet Varchar(30),
Ccity varchar(20),
primary key (Cname));

create table Depositor(


Cname varchar(10) references Customer,
Accno int references Account);

create table Loan(


Loan_no int primary key,
Bname varchar(20) references Branch,
amount numeric(10,2));

create table Borrower(


Cname varchar(10) references Customer,
Loan_no int references Loan);

Department of BCA -AIMS Page 25


DBMS Lab Manual

7. Enter atleast five tuples for each realtions


insert into Branch values('KG Road', 'Bangalore',2000);
insert into Branch values('MG Road', 'Delhi',1000.20);
insert into Branch values('Jayanagar', 'Mysore',300.00);
insert into Branch values('Vijayanagar', 'Tumkur',50000.54);
insert into Branch values('Malleshwaram', 'Bangalore',2700.54);

select * from Branch;


Bname BCity Asset
Jayanagar Mysore 300.00
KG Road Bangalore 2000.00
Malleshwaram Bangalore 2700.54
MG Road Delhi 1000.20
Vijayanagar Tumkur 50000.54

insert into Account values(101,'KG Road',525.25)


insert into Account values(102,'Jayanagar',5215.05)
insert into Account values(103,'MG Road',5250.35)
insert into Account values(104,'Malleshwaram',2525.05)
insert into Account values(105,'Vijayanagar',5025.50)

select * from Account;


Accno BName Balance
101 KG Road 525.25
102 Jayanagar 5215.05
103 MG Road 5250.35
104 Malleshwaram 2525.05
105 Vijayanagar 5025.50

insert into Customer values


('Manveer','Hesaragatta','Bangalore');
insert into Customer values
('Rohit','Vasco', 'Goa');
insert into Customer values
('Shubham','Kasthuri Road','Bangalore');
insert into Customer values
('Shuvonabo','Krishan Nagar','giridih');
insert into Customer values
('Sadam','MG Road','Culcutta');

select * from Customer;


Cname CStreet CCity
Manveer Hesaragatta Bangalore
Rohit Vasco Goa
Sadam MG Road Culcutta
Shubham Kasthuri Road Bangalore
Shuvonabo Krishan Nagar giridih

Department of BCA -AIMS Page 26


DBMS Lab Manual

insert into Depositor values('Manveer',105);


insert into Depositor values('Rohit',104);
insert into Depositor values('Sadam',103);
insert into Depositor values('Shubham',101);
insert into Depositor values('Shuvonabo',102);

select * from Depositor;

Cname Accno
Manveer 105
Rohit 104
Sadam 103
Shubham 101
Shuvonabo 102

insert into Loan values(501,'Jayanagar',5000.25);


insert into Loan values(502,'KG Road',523.25);
insert into Loan values(503,'Malleshwaram',500.25);
insert into Loan values(504,'MG Road',4525.25);
insert into Loan values(505,'Vijayanagar',5255.25);

select * from loan;

Laon_no Bname Ammount


501 Jayanagar 5000.25
502 KG Road 523.25
503 Malleshwaram 500.25
504 MG Road 4525.25
505 Vijayanagar 5255.25

insert into Borrower values('Manveer',504);


insert into Borrower values('Shuvonabo',502);
insert into Borrower values('Shubham',501);
insert into Borrower values('Sadam',503);
insert into Borrower values('Rohit',505);

select * from Borrower;

Cname LoanNO
Manveer 504
Shuvonabo 502
Shubham 501
Sadam 503
Rohit 505

8. Find all the customer who have atleat two accounts at


the main Branch (KG Road)

Department of BCA -AIMS Page 27


DBMS Lab Manual

Insert 4 records to KG road Main Branch

insert into Account values(106,'KG Road',4525.25)


insert into Account values(107,'KG Road',3525.25)
insert into Account values(108,'KG Road',5225.25)
insert into Account values(109,'KG Road',1525.25)

select * from Account;

Accno BName Balance


106 KG Road 4525.25
107 KG Road 3525.25
108 KG Road 5225.25
109 KG Road 1525.25

insert into Depositor values('Shuvonabo',106);


insert into Depositor values('Shuvonabo',106);
insert into Depositor values('Shuvonabo',106);

select * from Depositor;

Cname Accno
Shuvonabo 106
Shuvonabo 106
Shuvonabo 106

create view twoAccounts as(select B.Bname,A.Accno,D.Cname


from Branch B, Account1 A, Depositor D
where A.Accno = D.Accno and A.BName = B.BName
and B.Bname='KG Road');

select * from twoAccounts;

select Bname, Accno, Cname from twoAccounts


where Cname in(select Cname from twoAccounts group by
Cname having count(*)>=2);

BName AccNo Cname


KG Road 106 Shuvonabo
KG Road 106 Shuvonabo
KG Road 106 Shuvonabo

9. Find all Customer who have an account at all the


Branches located in a specific city (Bangalore)

Department of BCA -AIMS Page 28


DBMS Lab Manual

select D.Cname, A.Accno,B.Bname,B.Bcity from Depositor D,


Account1 A, Branch B where B.Bcity ='Bangalore' and
D.Accno = A.Accno and A.Bname = B.Bname;

Cname Accno Bname Bcity


Rohit 104 Malleshwaram Bangalore
Shubham 101 KG Road Bangalore
Shuvonabo 106 KG Road Bangalore
Shuvonabo 106 KG Road Bangalore
Shuvonabo 106 KG Road Bangalore

10. Demonstrate how you delete all Account tuples at


every branch located in a specific city (Ex: Bangalore)

create view Bank as(select D.Cname, A.Accno, B.Bname from


Depositor D, Account1 A, Branch B
where D.Accno = A.Accno and A.Bname = B.Bname and B.Bcity
= 'Bangalore');

select * from Bank


Cname Accno Bname
Rohit 104 Malleshwaram
Shubham 101 KG Road
Shuvonabo 106 KG Road
Shuvonabo 106 KG Road
Shuvonabo 106 KG Road

delete from Depositor where Accno in (select Accno from


Bank);
(5 row(s) affected)
Select * from Depositor;
Cname Accno
Manveer 105
Sadam 103
Shuvonabo 102

delete from Account where BName in (select Bname from


Branch where Bcity = 'Bangalore');
(6 row(s) affected)

Select * from Account;


Accno Bname Balance
102 Jayanagar 5215.05
103 MG Road 5250.35
105 Vijayanagar 5025.50

Department of BCA -AIMS Page 29


DBMS Lab Manual

Question #8:consider the following database for Ordering


Processing

CUSTOMER(cust_no: int, Cname: string, City: String)


ORDER(Orderno: int, cust_no int, odate: date,ord_amt real);
ITEM(Itemno:int, unit_price: real)
ORDER_ITEM(Orderno: int, itemno: int, qty:int)
WAREHOUSE(Warehouseno:int, city:string)
SHIPMENT(orderno:int, warehouseno:int, ship_date: date)

1.create above tables by properly specifying primary keys and


foreign keys.

create table Customer(


cust_no int primary key,
cname varchar(20) not null,
city varchar(20) not null);

create table Order(


Orderno int primary key,
odate datetime not null,
cust_no int references Customer,
ord_amt numeric(10,2));

create table Item(


Itemno int primary key,
unit_price numeric(10,2));

create table Order_item(


Orderno int references order,
itemno int references Item,
qty int);

create table Warehouse(


Warehouseno int primary key,
city varchar(20) not null);

create table Shipment(


orderno int references order,
warehouseno int references warehouse,
ship_date datetime not null);

select * from Customer;


insert into Customer values(101, 'Himanshu', 'Ahmedabad');
insert into Customer values(102, 'Bilguun', 'Bangalore');
insert into Customer values(103, 'Deepak', 'Tumkur');
insert into Customer values(104, 'Prasad', 'Mysore');
insert into Customer values(105, 'Manisha', 'kolkatta');

select * from Customer

Department of BCA -AIMS Page 30


DBMS Lab Manual

Cust_no Cname City


101 Himanshu Ahmedabad
102 Bilguun Bangalore
103 Deepak Tumkur
104 Prasad Mysore
105 Manisha kolkatta

alter table order00 add cust_no int references Customer00;

insert into Order00 values(10,'10-Jan-15',104,15000)


insert into Order00 values(20,'15-Feb-15',103,15000)
insert into Order00 values(30,'31-Mar-12',101,15000)
insert into Order00 values(60,'30-Apr-15',105,15000)
insert into Order00 values(70,'20-Jun-15',102,15000)

select * from Order00;


Orderno Odate Ord_amt Cust_no
10 2015-01-10 15000.00 104
20 2015-02-15 15000.00 103
30 2012-03-31 15000.00 101
40 2013-04-30 15000.00 105
50 2014-06-20 15000.00 102

insert into item00 values(1, 100);


insert into item00 values(2, 200);
insert into item00 values(3, 300);
insert into item00 values(4, 150);
insert into item00 values(5, 250);

select * from Item00;


Itemno Unit_Price
1 100.00
2 200.00
3 300.00
4 150.00
5 250.00

insert into Order_item values(10, 1, 20);


insert into Order_item values(20, 5, 24);
insert into Order_item values(30, 4, 36);
insert into Order_item values(50, 3, 56);
insert into Order_item values(40, 1, 40);

select * from Order_Item00


Orderno itemno qty
10 1 20
20 5 24
30 4 36
50 3 56
40 1 40

insert into warehouse values(1001, 'Bangalore')


insert into warehouse values(1002, 'Hubli')
insert into warehouse values(1003, 'Mysore')
Department of BCA -AIMS Page 31
DBMS Lab Manual

insert into warehouse values(1004, 'Tumkur')


insert into warehouse values(1005, 'Kolar')

select * from warehouse;

warehouseno City
1001 Bangalore
1002 Hubli
1003 Mysore
1004 Tumkur
1005 Kolar

insert into shipment values(10, 1001, '28-Jan-2014');


insert into shipment values(20, 1002, '18-Mar-2013');
insert into shipment values(20, 1004, '28-Feb-2013');
insert into shipment values(30, 1002, '28-Dec-2014');
insert into shipment values(10, 1003, '28-Aug-2012');

select * from shipment;


Orderno Warehouseno Ship_date
10 1001 2014-01-28
20 1002 2013-03-18
20 1004 2013-02-28
30 1002 2014-12-28
10 1003 2012-08-28

3. List the Order number and Shipdate for all orders shipped
from a particular warehouse.

select Orderno, ship_date from shipment where warehouseno = 1002

4. Produce a listing Customer Name, No.of Orders,Avg Oreder


AMount where the middle column is the total average Order
Amount for that customer.

select C.Cname,count(O.orderno)'No of Order',


Avg(O.Ord_amt) 'Avg_Order_Amt'
from customer00 C,order00 O
where C.cust_no=O.cust_no
group by C.Cname,O.cust_no;
Cname No of Order Avg_Order_amt
Himanshu 1 2050.590000
Bilguun 2 10277.775000
Deepak 1 2000.090000
Prasad 1 15000.000000
Manisha 2 9720.220000

5. list the orders that were not shipted within 30 days of


ordering.

select c.cname, o.Orderno, o.odate, s.ship_date


from customer00 c, order00 o, shipment00 s
where c.Cust_no = o.Cust_no
Department of BCA -AIMS Page 32
DBMS Lab Manual

and o.orderno = s.orderno


and (s.ship_date) - (o.Orderdate))>30;

select * from Customer


select * from Order1;
select * from Item
select * from Order_Item
select * from warehouse
select * from shipment;

Department of BCA -AIMS Page 33


DBMS Lab Manual

Part B
1. Consider the following table with the mapping given
below.
RETAILOR(Cust_id: int, Cust_name: string , place:
string, ph_no integer, pan_no: string).
a) Create the above table by specifying primary key.
b) Alter the table to add a column pin_no.
c) Enter 5 tuples into the table.
d) Display the details of customer who are all living in
Bangalore.
e) Display the customer name whose first letter is ‘S’.

a).Create the above table by specifying primary key.


create table RETAILOR(
Cust_id int primary key,
Cust_name varchar(20) not null,
place varchar(25),
ph_no int,
pan_no varchar(10));

Command(s) completed successfully.

sp_help Retailor

column name type size


Cust_id int 4
Cust_name varchar 20
place varchar 25
ph_no int 4
pan_no varchar 10

b)Alter the table to add a column pin_no.

alter table retailor add pin_no int not null;

Command(s) completed successfully.

c) Enter 5 tuples into the table.


insert into retailor values(111,'Geetha','Tumkur',
888999922,'934TI012',56001);
insert into retailor values(222,'Ranjith','Kolar',
878787878,'944GF014',56005);
insert into retailor values(333,'Krishna','Bangalore',
234343434,'768BN098',56055);
insert into retailor values(444,'Srihari','Mysore',

Department of BCA -AIMS Page 34


DBMS Lab Manual

232323234,'334RF324',560058);
insert into retailor values(555,'Manjunath','Hubali',
773344556,'224DE435',56005);

select * from retailor

Cust_id Cust_name Place ph_no pan_no pin_np


111 Geetha Tumkur 888999922 934TI012 56001
222 Ranjith Kolar 878787878 944GF014 56005
333 Krishna Bangalore 234343434 768BN098 56055
444 Srihari Mysore 232323234 334RF324 560058
555 Manjunath Hubali 773344556 224DE435 56005

d)Display the details of customer who are all living in


Bangalore.

select * from retailor where place = 'Bangalore';

Cust_id Cust_name Place ph_no pan_no pin_np


333 Krishna Bangalore 234343434 768BN098 56055

e) Display the customer name whose first letter is ‘S’.


select * from retailor where cust_name like 'S%';

Cust_id Cust_name Place ph_no pan_no pin_np


444 Srihari Mysore 232323234 334RF324 560058

Department of BCA -AIMS Page 35


DBMS Lab Manual

2. Consider the following employee and department tables.


DEPARTMENT(deptno: integer, dname:string, location: string)
EMPLOYEE(empno: integer, ename: string, designation: string,
manager: string, hiredate: date, salary: number(8,2), deptno:
integer)

a) Create the above tables by properly specifying the primary


keys and foreign keys.
b) Enter at least five tuples for each relation.
c) List the employees of employee table in descending order of
their salaries.
d) List the details of highest paid employee in ‘SALES’
department.
e) List the employee number, employee name and department
name for all employee of departments in 50 and 60.

a)Create the above tables by properly specifying the primary


keys and foreign keys.
Create table Department(
deptno int primary key,
dname varchar(20),
location varchar(20));

Command(s) completed successfully.

sp_help Department

column name type size


deptno int 4
dname varchar 20
location varchar 20

Create table Employee(


empno int primary key,
ename varchar(10),
designation varchar(20),
manager varchar(20),
hiredate datetime,
salary numeric(8,2),
deptno int references Department);

Command(s) completed successfully.

sp_help Employee

column name type size


empno int no 4

Department of BCA -AIMS Page 36


DBMS Lab Manual

ename varchar 10
designation varchar 20
manager varchar 20
hiredate datetime 8
salary numeric 5
deptno int 4

b) Enter at least five tuples for each relation.


insert into Department values(10,'Computer Science','Tumkur');
insert into Department values(20,'Commerce','Kolar');
insert into Department values(30,'Mangement','Hubali');
insert into Department values(40,'English','Bangalore');
insert into Department values(50,'Kannada','Mysore');

select * from department;


deptno dname Location
10 Computer Science Tumkur
20 Commerce Kolar
30 Mangement Hubali
40 English Bangalore
50 Kannada Mysore

insert into employee values(101,


'Anitha','Manager','Anitha','13-jan-2004',45000.00,20);
insert into employee values(102,
'Nirmala','Clerk','Anitha','25-feb-2003',25000.00,40);
insert into employee values(103,
'Lohith','Manager','Lohith','19-dec-2005',35000.00,10);
insert into employee values(104,
'Nithya','Profesor','Lohith','15-mar-2006',15000.00,30);
insert into employee values(105,
'Manish','Profesor','Anitha','01-apr-2009',5000.00,10);

select * from employee


empno ename designation Manger hiredate salary deptno
101 Anitha Manager Anitha 2004-01-13 45000.00 20
102 Nirmala Clerk Anitha 2003-02-25 25000.00 40
103 Lohith Manager Lohith 2005-12-19 35000.00 10
104 Nithya Profesor Lohith 2006-03-15 15000.00 30
105 Manish Profesor Anitha 2009-04-01 5000.00 10

c) List the employees of employee table in descending order


of their salaries.

select * from employee order by salary desc

empno ename designation Manger hiredate salary deptno


101 Anitha Manager Anitha 2004-01-13 45000.00 20
103 Lohith Manager Lohith 2005-12-19 35000.00 10
102 Nirmala Clerk Anitha 2003-02-25 25000.00 40
104 Nithya Profesor Lohith 2006-03-15 15000.00 30
105 Manish Profesor Anitha 2009-04-01 5000.00 10

Department of BCA -AIMS Page 37


DBMS Lab Manual

d)List the details of highest paid employee in ‘SALES’


department.

select max(salary)'Highest Paid salary'from employee,department


where employee.deptno = department.deptno and
dname = 'Computer Science';

Highest Paid salary


35000.00

e)List the employee number, employee name and department name


for all employee of departments in 20 and 40.

select e.empno, e.ename, d.dname from employee e,department d


where d.deptno = e.deptno and d.deptno in(20,40);

empno ename dname


101 Anitha Commerce
102 Nirmala English

Department of BCA -AIMS Page 38


DBMS Lab Manual

3. Create the following tables with the mapping given below.


STU_DETAILS (reg_no: integer, stu_name: string, DOB: date,
address: string, city: string)
MARK_DETAILS (reg_no: integer, marks1: integer, mark2:
integer, mark3: integer, total: integer)
a) Create the above tables by specifying primary key and
foreign key.
b) Enter 5 tuples into the table.
c) Display only those rows whose total ranges between 250
and 300.
d) Drop the table mark_details.
e) Delete the row whose reg_no=33.
f) List all the student detail that who are all located in
Tumkur.

a)Create the above tables by specifying primary key.

create table STU_DETAILS (


reg_no int primary key,
stu_name varchar(20) not null,
DOB datetime,
address varchar(20),
city varchar(20));

sp_help STU_DETAILS

column name type size


reg_no int no 4
stu_name varchar 20
DOB datetime 8
address varchar 20
city varchar 20

create table MARK_DETAILS (


reg_no int references STU_DETAILS,
marks1 int not null,
marks2 int not null,
marks3 int not null,
total int not null)

sp_help MARK_DETAILS
column name type size
reg_no int 4
marks1 int 4
marks2 int 4
marks3 int 4
total int 4

b)Enter 5 tuples into the table.

Department of BCA -AIMS Page 39


DBMS Lab Manual

insert into STU_DETAILS values


(11,'Rashmi','23-jan-1997','1st Main','Tumkur');
insert into STU_DETAILS values
(22,'Geetha','13-feb-1996','5th Cross','Bangalore');
insert into STU_DETAILS values
(33,'Bharathi','03-may-1987','25th Main','Mysore');
insert into STU_DETAILS values
(44,'Latha','25-dec-1994','3rd Main','Tumkur');
insert into STU_DETAILS values
(55,'Vidya','29-feb-1996','2nd Cross','Bangalore');

select * from Stu_Details

reg_no stu_name dob address city


11 Rashmi 1997-01-23 1st Main Tumkur
22 Geetha 1996-02-13 5th Cross Bangalore
33 Bharathi 1987-05-03 25th Main Mysore
44 Latha 1994-12-25 3rd Main Tumkur
55 Vidya 1996-02-29 2nd Cross Bangalore

insert into MARK_DETAILS VALUES(11,75,85,95,255);


insert into MARK_DETAILS VALUES(22,55,65,65,185);
insert into MARK_DETAILS VALUES(33,35,45,55,135);
insert into MARK_DETAILS VALUES(44,85,95,100,280);
insert into MARK_DETAILS VALUES(55,65,35,55,155);

select * from Mark_Details

reg_no marks1 marks2 marks3 total


11 75 85 95 255
22 55 65 65 185
33 35 45 55 135
44 85 95 100 280
55 65 35 55 155

c)Display only those rows whose total ranges between 250 and
300.
select s.reg_no, m.total from stu_details s, mark_details m
where s.reg_no = m.reg_no and total between 250 and 300;

reg_no total
11 255
44 280

d)Drop the table mark_details.

drop table mark_details

Command(s) completed successfully.

e)Delete the row whose reg_no=33


Department of BCA -AIMS Page 40
DBMS Lab Manual

delete stu_details where reg_no = 33;

(1 row(s) affected)

f)List all the student detail that who are all located in
Tumkur.

select * from stu_details where city = 'Tumkur';

reg_no stu_name dob address city


11 Rashmi 1997-01-23 1st Main Tumkur
44 Latha 1994-12-25 3rd Main Tumkur

Department of BCA -AIMS Page 41


DBMS Lab Manual

4. An inventory database has the following tables. The primary


keys are underlined.
ITEM(itemno: number, ItemName:Text, Price: Number(10,2))
PURCHASE(itemno: number, Quantity: number)
a) Create the above tables by specifying primary key and
foreign keys.
b) Enter 5 tuples into the tables
c) List the item purchased.
d) Display the total items purchased (listing must have the
columns; itemcode, itemname, totalQuantity)
e) List the items which are not purchased by any one.

a)Create the above tables by specifying primary key and


foreign keys.

create table ITEM(


itemno int primary key,
ItemName varchar(20) not null,
Price Numeric(10,2));

Command(s) completed successfully.

create table PURCHASE(


itemno int references ITEM,
Qty int not null);

Command(s) completed successfully.

b)Enter 5 tuples into the tables

Insert Into Item Values(101, 'Pencil', 10.25);


Insert Into Item Values(102, 'Pen', 20.52);
Insert Into Item Values(103, 'Note Book', 45.25);
Insert Into Item Values(104, 'Scale', 15.02);
Insert Into Item Values(105, 'Erraser', 9.12);
Insert Into Item Values(106, 'Pen Drive', 229.19);

select * from ITEM;

itemno itemname Price


101 Pencil 10.25
102 Pen 20.52
103 Note Book 45.25
104 Scale 15.02
105 Erraser 9.12
106 Pen Drive 229.19

Insert Into Purchase Values(101, 25);

Department of BCA -AIMS Page 42


DBMS Lab Manual

Insert Into Purchase Values(102, 17);


Insert Into Purchase Values(103, 30);
Insert Into Purchase Values(105, 12);
Insert Into Purchase Values(105, 13);
select * from Purchase;

itemno qty
101 25
102 17
103 30
105 12
105 13

c)List the item purchased.

Select I.Itemno, I.Itemname, Price, Qty From Item I, Purchase


P Where I.Itemno = P.Itemno;

itemno itemname Price QTY


101 Pencil 10.25 25
102 Pen 20.52 17
103 Note Book 45.25 30
105 Erraser 9.12 12
105 Erraser 9.12 13

d)Display the total items purchased (listing must have the


columns; itemcode, itemname, totalQuantity)

Select I.Itemno, I.Itemname, Sum(Qty)'Quantity' From Item I,


Purchase P Where I.Itemno = P.ItemNO Group By I.Itemno,
I.Itemname;

itemno itemname Quantity


101 Pencil 25
102 Pen 17
103 Note Book 30
105 Erraser 25

e)List the items which are not purchased by any one.

Select I.Itemno, I.Itemname From Item I Where Itemno not in


(Select P.Itemno From Purchase P);

itemno itemname
104 Scale
106 Pen Drive

Department of BCA -AIMS Page 43


DBMS Lab Manual

5. A bank customer database has two tables CUSTOMER and ACCOUNT


ACCOUNT(Accno: number, Acctype: text, Branch: text, AccStatus:
text, ChequeFacility: Text)
CUSTOMER (Custno: number, Custname: Text, City :text, AccNo:
Number, Balance: Number(10,2))

a) Create the above tables by specifying primary key and


foreign keys.
b) Enter 5-8 tuples into the tables
c) List the customer from “Bangalore” who have cheque
facility.
d) List all the customer whose balance is grater than 3000
and have an active account.
e) Find the current outstanding balance amount of branch
“Peenya”.

1.Create the above tables by specifying primary key and


foreign keys.

Create Table Account(


Accno Numeric(20),
Acctype Varchar(25),
Branch Varchar(25),
Accstatus Varchar(20),
Chequefacility Varchar(25),
Primary Key(Accno));

Sp_Help Account;

Column Name Type Size


Accno Numeric 20
Acctype Varchar 25
Branch Varchar 25
Accstatus Varchar 20
Chequefacility Varchar 25

Create Table Customer(


Custno Numeric(20),
Custname Varchar(24),
City Varchar(25),
Accno Numeric(20),
Balance Numeric(10,2),
Primary Key (Custno),
Foreign Key(Accno) References Account(Accno));

Sp_Help Customer;

Column Name Type Size


Custno Numeric 20
Custname Varchar 24

Department of BCA -AIMS Page 44


DBMS Lab Manual

City Varchar 25
Accno Numeric 20
Balance Numeric 10,2

2.Enter 5-8 Tuples Into The Tables

Insert Into Account Values


(1111,'CA','Malleshwaram','inactive','No');
Insert Into Account Values
(2222,'SB','Yeshwanthpur','Inactive','Yes');
Insert Into Account Values
(3333,'SB','Peenya','Active','Yes');
Insert Into Account Values
(4444,'CA','Jayanagar','Active','No');
Insert Into Account Values
(5555,'SB','Hebal','Active','No');
Insert Into Account Values
(6666,'SB','Peenya','Active','Yes');
Insert Into Account Values
(7777,'SB','Malleshwaram','inactive','Yes');

Select * From Account;

Accno Acctype Branch Accstatus Chequefacility


1111 CA Malleshwaram inactive No
2222 SB Yeshwanthpur Inactive Yes
3333 SB Peenya Active Yes
4444 CA Jayanagar Active No
5555 SB Hebal Active No
6666 SB Peenya Active Yes
7777 SB Malleshwaram inactive Yes

Insert Into Customer Values


(101,'Pavithra','Bangalore',1111,1500);
Insert Into Customer Values
(102,'Nandini','Mysore',3333,30001);
Insert Into Customer Values
(103,'Deepak','Tumkur',4444,22222);
Insert Into Customer Values
(104,'Jagadeesh','Bangalore',5555,50000);
Insert Into Customer Values
(105,'Rajesh','Bangalore',6666,1000);
Insert Into Customer Values
(106,'Vinutha','Mysore',2222,32000);
Insert Into Customer Values
(107,'Vimala','Bangalore',7777,10000);

Select * From Customer;


Custno Custname City Accno Balance
101 Pavithra Bangalore 1111 1500.00
102 Nandini Mysore 3333 30001.00
103 Deepak Tumkur 4444 22222.00
104 Jagadeesh Bangalore 5555 50000.00
105 Rajesh Bangalore 6666 1000.00
Department of BCA -AIMS Page 45
DBMS Lab Manual

106 Vinutha Mysore 2222 32000.00


107 Vimala Bangalore 7777 10000.00

3.List The Customer From “Bangalore” Who Have Cheque Facility.

Select C.Custno, C.Custname,A.Accno,A.Chequefacility From


Customer C, Account A Where C.Accno = A. Accno And
A.Chequefacility = 'Yes' And C.City = 'Bangalore';

Custno Custname Accno Chequefacility


105 Rajesh 6666 Yes
107 Vimala 7777 Yes

4.List All The Customer Whose Balance Is Grater Than 3000 And
Have An Active Account.

Select C.Custname,C.Balance From Customer C, Account A Where


C.Accno = A. Accno And C.Balance > 3000.00 And
Accstatus='Active' ;

Custname Balance
Nandini 30001.00
Deepak 22222.00
Jagadeesh 50000.00

5.Find The Current Outstanding Balance Amount Of Branch “


Peenya”.
Select Sum(C.Balance) 'Out Standing Balance'From Customer C,
Account A Where C.Accno = A. Accno And A.Branch='Peenya';

Out Standing Balance


31001.00

Department of BCA -AIMS Page 46


DBMS Lab Manual

 WRITE A PL/SQL PROGRAM TO FIND THE RESULT OF THE STUDENT

DECLARE @MARKS1 INT


DECLARE @MARKS2 INT
DECLARE @MARKS3 INT
DECLARE @AVG INT

SELECT @MARKS1=59
SELECT @MARKS2=55
SELECT @MARKS3=51
SELECT @AVG = (@MARKS1+ @MARKS2 + @MARKS3)/3
IF (@MARKS1 <50 OR @MARKS2<50 OR @MARKS3<50)
BEGIN
PRINT 'MARKS :' + CONVERT(CHAR(5),@AVG)
PRINT 'RESULT : FAILED'
END

ELSE
IF @AVG>=70
BEGIN
PRINT 'MARKS :' + CONVERT(CHAR(5),@AVG)
PRINT 'RESULT : PASSED WITH DISTINCTION'
END
ELSE IF @AVG>=60
BEGIN
PRINT 'MARKS :' + CONVERT(CHAR(5),@AVG)
PRINT 'RESULT : PASSED WITH FIRST CLASS'
END
ELSE
BEGIN
PRINT 'MARKS :' + CONVERT(CHAR(5),@AVG)
PRINT 'RESULT : PASSED WITH SECOND CLASS'
END

Department of BCA -AIMS Page 47


DBMS Lab Manual

 WRITE A PL/SQL PROGRAM TO GENERATE MATHEMATICAL TABLE USING


WHILE
DECLARE @NUM INT
SELECT @NUM=23
DECLARE @COUNT INT
DECLARE @PROD INT
SELECT @COUNT=1
WHILE @COUNT<=10
BEGIN
SELECT @PROD=@NUM * @COUNT
PRINT CONVERT(CHAR(3),@NUM)+'X '+CONVERT(CHAR(3),@COUNT)+' =
'+CONVERT(CHAR(3),@PROD)
SELECT @COUNT=@COUNT + 1
END

Department of BCA -AIMS Page 48


DBMS Lab Manual

 WRITE A PL/SQL PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER


USING WHILE LOOP

DECLARE @I INT
DECLARE @FACT INT

BEGIN
SELECT @I = 5
SELECT @FACT =1

WHILE @I>=1

BEGIN
SELECT @FACT =@FACT*@I;
SELECT @I=@I-1
END
PRINT 'FACTORIAL :' + CONVERT(CHAR(5),@FACT)
END;

Department of BCA -AIMS Page 49


DBMS Lab Manual

 WRITE A PL/SQL PROGRAM TO GENERATE FIBONACCI SERIES USING


WHILE LOOP

DECLARE @I INT
DECLARE @N INT
DECLARE @F1 INT
DECLARE @F2 INT
DECLARE @F3 INT

BEGIN
SELECT @I = 3
SELECT @N = 8
SELECT @F1 =0
SELECT @F2 =1
PRINT 'FIBONACCI SERIES IS :'
PRINT CONVERT(CHAR(5),@F1)
PRINT CONVERT(CHAR(5),@F2)

WHILE @I <=@N
BEGIN
SELECT @F3 = @F2 + @F1
PRINT + CONVERT(CHAR(5),@F3)
SELECT @F1 = @F2
SELECT @F2 = @F3
SELECT @I=@I+1
END
END;

Department of BCA -AIMS Page 50


DBMS Lab Manual

 WRITE A PL/SQL PROGRAM TO FIND GIVEN NUMBER IS PALINDROME OR


NOT USING WHILE LOOP

DECLARE @NUM INT


DECLARE @I INT
DECLARE @SUM INT
DECLARE @TEMP INT
DECLARE @REM INT

BEGIN

SELECT @NUM = 121


SELECT @SUM =0
SELECT @TEMP =@NUM

WHILE @NUM >0


BEGIN
SELECT @REM = @NUM%10
SELECT @SUM = @SUM*10+ @REM
SELECT @NUM = @NUM/10
END
END
IF((@SUM = @TEMP))
BEGIN
PRINT ' GIVEN NUMBER IS A POLINDROME'
END
ELSE
BEGIN
PRINT ' GIVEN NUMBER IS NOT A POLINDROME'
END

Department of BCA -AIMS Page 51

You might also like