Dbms Lab Manual 18CSL58
Dbms Lab Manual 18CSL58
BELGAUM
MASTER MANUAL
V-SEMESTER
COURSE DESCRIPTION
Design, develop, and implement the specified algorithms for the following problems using Java language
under LINUX /Windows environment. NetBeans /Eclipse IDE tool can be used for development and
demonstration.
COURSE OUTCOMES
After studying this course, students will be able to:
Design algorithms using appropriate design techniques (brute-force, greedy, dynamic programming, etc.)
Implement a variety of algorithms such assorting, graph related, combinatorial, etc., in a high level
language.
Analyze and compare the performance of algorithms using language features.
Apply and implement learned algorithm design techniques and data structures to solve real world
problems.
CONTENTS
SL. NO. DESCRIPTION PAGE NO.
2 LIBRARY DATABASE 01 - 08
3 ORDER DATABASE 09 - 18
4 MOVIE DATABASE 19 - 29
5 COLLEGE DATABASE 30 - 41
6 COMPANY DATABASE 42 - 52
i
DBMS LABORATORY WITH MINI PROJECT
(Effective from the academic year 2018 -2019)
SEMESTER – V
Course Code 18CSL58 CIE Marks 40
Number of Contact Hours/Week 0:2:2 SEE Marks 60
Total Number of Lab Contact Hours 36 Exam Hours 03
Credits – 2
Course Learning Objectives: This course (18CSL58) will enable students to:
• Foundation knowledge in database concepts, technology and practice to groom students
into well-informed database application developers.
• Strong practice in SQL programming through a variety of database problems.
• Develop database applications using front-end tools and back-end DBMS.
Descriptions (if any):
PART-A: SQL Programming (Max. Exam Mks. 50)
• Design, develop, and implement the specified queries for the following problems using
Oracle, MySQL, MS SQL Server, or any other DBMS under LINUX/Windows environment.
• Create Schema and insert at least 5 records for each table. Add appropriate database
constraints.
PART-B: Mini Project (Max. Exam Mks. 30)
• Use Java, C#, PHP, Python, or any other similar front-end tool. All applications must
be demonstrated on desktop/laptop as a stand-alone or web based application (Mobile apps on
Android/IOS are not permitted.)
.
Installation procedure of the required software must be demonstrated, carried out in groups and
documented in the journal.
Programs List:
PART A
1. Consider the following schema for a Library Database: BOOK(Book_id,
Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS(Book_id, Author_Name)
PUBLISHER(Name, Address, Phone)
BOOK_COPIES(Book_id, Programme_id, No-of_Copies)
BOOK_LENDING(Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME(Programme_id, Programme_Name, Address)
Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each Programme, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but
from Jan 2017 to Jun 2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect
this data manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its
working with a simple query.
5. Create a view of all books and its number of copies that are currently
available in the Library.
ii
2. Consider the following schema for Order Database:
SALESMAN(Salesman_id, Name, City, Commission)
CUSTOMER(Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS(Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id) Write
SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesman who had more than one
customer.
3. List all the salesman and indicate those who have and don’t have customers
in their cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest
order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000.
All his orders must also be deleted.
3. Consider the schema for Movie Database: ACTOR(Act_id,
Act_Name, Act_Gender)
DIRECTOR(Dir_id, Dir_Name, Dir_Phone)
MOVIES(Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)
MOVIE_CAST(Act_id, Mov_id, Role) RATING(Mov_id,
Rev_Stars)
Write SQL queries to
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more
movies.
3. List all actors who acted in a movie before 2000 and also in a movie after
2015 (use JOIN operation).
4. Find the title of movies and number of stars for each movie that has at least
one rating and find the highest number of stars that movie received. Sort the result by
movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
4. Consider the schema for College Database:
STUDENT(USN, SName, Address, Phone, Gender) SEMSEC(SSID,
Sem, Sec)
CLASS(USN, SSID)
COURSE(Subcode, Title, Sem, Credits)
IAMARKS(USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
Write SQL queries to
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and
in each section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all Courses.
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section students.
ii
i
5. Consider the schema for Company Database:
EMPLOYEE(SSN, Name, Address, Sex, Salary, SuperSSN, DNo) DEPARTMENT(DNo,
DName, MgrSSN, MgrStartDate)
DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation, DNo)
WORKS_ON(SSN, PNo, Hours) Write
SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls
the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project
is given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department,
as well as the maximum salary, the minimum salary, and the average salary in this
department
4. Retrieve the name of each employee who works on all the projects
controlledby department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the
department number and the number of its employees who are making more than
Rs. 6,00,000.
PART B: Mini Project
• For any problem selected
• Make sure that the application should have five or more tables
• Indicative areas include; health care
Laboratory Outcomes: The student should be able to:
• Create, Update and query on the database.
• Demonstrate the working of different concepts of DBMS
• Implement, analyze and evaluate the project developed for an application.
Conduct of Practical Examination:
• Experiment distribution o For laboratories having only one part: Students are allowed
to pick one experiment from the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal opportunity.
• Change of experiment is allowed only once and marks allotted for procedure to be made
zero of the changed part only. • Marks Distribution (Courseed to change in accoradance with
university regulations)
k) For laboratories having only one part – Procedure + Execution + Viva-Voce:
15+70+15 =
100 Marks
l) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks ii.
Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks
i
v
DBMS LAB MANUAL – 18CSL58 2020-21
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each branch,etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017to Jun 2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5. Create a view of all books and its number of copies that are currently available in the
Library.
Entity-Relationship Diagram:
Schema Diagram:
Table Creation:
Table Descriptions:
Queries:
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each PROGRAMME, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017
to Jun2017.
select card_no
from BOOK_LENDING
where date_out between ’01-jan-2017’ and ’01-jul-2017’ group by card_no
having count (*)>3;
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5. Create a view of all books and its number of copies that are currently available in the
Library.
Viva Questions
1. What is data?
Data is a collection of facts, such as numbers, words, measurements, observations or even just
descriptions of things.
2. What is database?
A database is a logically coherent collection of data with some inherent meaning, representing some
aspect of real world and which is designed, built and populated with data for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it is
general-purpose software that provides the users with the processes of defining, constructing and
manipulating the database for various applications.
RDBMS is used to manage Relational database. Relational database is a collection of organized set
of tables related to each other, and from which data can be accessed easily. Relational Database is the
most commonly used database these days.
7. What is Table?
Table is a collection of data elements organized in terms of rows and columns.
8. What is a Tuple?
A single entry in a table is called a Tuple or Record or Row. A tuple in a table represents a set of
related data. For example, the above Employee table has 4 tuples/records/rows.
Following is an example of single record or tuple.
1 Adam 34 13000
9. What is Columns?
The columns in a table are the set of facts that we keep track of about that type of object. A column is
also called an attribute.
Entity-Relationship Diagram
Schema Diagram
Table Creation
Table Descriptions:
Salesman Details:
Customer Details:
insert into CUSTOMER values (1, ’sharal’,’hydrabad’,40,1004);
insert into CUSTOMER values (2,’meenakshi’,’mangalore’,40,1000);
insert into CUSTOMER values (3,’vikky’,’mumbai’,35,1002);
insert into CUSTOMER values (4, ’john’,’mumbai’,20,1002);
insert into CUSTOMER values (5, ’george’,’bangalore’,10,1001);
insert into CUSTOMER values (6, ’hevin’,’bangalore’,50,1001);
insert into CUSTOMER values (7, ’roshan’,’delhi’,45,1003);
insert into CUSTOMER values (8, ’vimala’,’chennai’,35,1001);
insert into CUSTOMER values (9, ’nakul’,’ayodhya’,15,1005);
Order Details:
Queries:
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION
operation.)
OUTPUT OF UNION:
4. Create a view that finds the salesman who has the customer with the highest order of a day.
(NOTE: Execute Query-1 to create a view. Then execute Query-2 to display that on SQL Command Line
console.)
Query-1:
create view TOPSALESMAN as
select b.Ord_date, b.Purchase_amt, a.Salesman_id, a.Name
from SALESMAN a, ORDERS b
where a.Salesman_id = b.Salesman_id and b.Purchase_amt=(select MAX(c.Purchase_amt)
from ORDERS c
where b.Ord_date = c.Ord_date) ;
Query-2:
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also
bedeleted.
We can verify from the above snapshot that, a Salesman named ‘JOSEPH’ with ID 1000 has been
removed from the SALESMAN table.
We can verify from the above snapshot that, as we had assigned ON DELETE SET NULL constraint on
Salesman_id(which is a foreign key) in CUSTOMER table, only the deleted Salesman_id is replaced
by NULL.
We can verify from the above snapshot that, as we had assigned ON DELETE CASCADE constraint on
Salesman_id(which is a foreign key) in ORDERS table, the complete order details of Ord_ID222 and
992 are deleted from ORDERS table (which was related to Salesman ID 1000).
Viva Questions
1. What is an Attribute?
A table consists of several records(row), each record can be broken down into several smaller parts of
data known as Attributes. The above Employee table consist of four
attributes, ID, Name, Age and Salary.
An attribute, that has a single value for a particular entity. For example, age of a employee entity.
An attributes that may have multiple values for the same entity. For example colors of a car entity.
Attribute can be subdivided into two or more other Attribute. For Example, Name can be divided into
First name, Middle name and Last name.
The attributes which cannot be divided into smaller subparts are called simple or atomic attributes. For
example, age of employee entity
An attribute, which cannot be derived from other attribute, is known as stored attribute. For example,
BirthDate of employee.
Attributes derived from other stored attribute. For example age from Date of Birth and Today’s date.
If an attribute of an entity, is built using composite and multivalued attributes, then these attributes are
called complex attributes. For example, a person can have more than one residence and each residence
can have multiple phones, an addressphone for a person entity can be specified as – {Addressphone
(phone {(Area Code, Phone Number)}, Address(Sector Address (Sector Number,House Number), City,
State, Pin))}.Here {} are used to enclose multivalued attributes and () are used to enclose composite
attributes with comma separating individual attributes.
It represents primary key. It is an attribute, that has distinct value for each entity/element in an entity set.
For example, Roll number in a Student Entity Type.
These are attributes other than candidate key attributes in a table. For example Firstname is a non key
attribute as it does not represent the main characteristics of the entity.
Entity-Relationship Diagram:
Schema Diagram:
Table Creation :
Note: In RATINGS table Mov_id & Rev_starsis defined as composite key so that more
than one rating can be assigned for a movie (Needed for query-4).
Table Descriptions:
Queries:
select Mov_title
from MOVIES
where Dir_id IN (select Dir_id
from DIRECTOR
where Dir_name = ’hitchcock’);
2. Find the movie names where one or more actors acted in two or more movies.
select Mov_title
from MOVIES m, MOVIE_CAST mc
where m.Mov_id=mc.Mov_id and mc.Act_id IN (select Act_id
from MOVIE_CAST
group by Act_id
having COUNT (Act_id)>1)
group by Mov_title;
3. List all actors who acted in a movie before 2000 and also in a movie after 2015(use JOIN
operation).
4. Find the title of movies and number of stars for each movie that has at least one rating
and find the highest number of stars that movie received. Sort the result by movie title.
Alternate Query:
select Mov_title, MAX(Rev_stars) as Best_Rating
from MOVIES m, RATING r
where m.Mov_id= r.Mov_id and r.Rev_stars IS NOT NULL
group by Mov_title
order by Mov_title;
NOTE: If there are two ratings for a particular movie directed by ‘Steven Spielberg’ then
query can’t be executed, because we have defined a composite key (movie_id, Rev_stars)
update RATING
set Rev_stars=5
where Mov_id IN(select Mov_id
from MOVIES
where Dir_id IN( select Dir_id
from DIRECTOR
where Dir_name = ’stevenspielberg’));
Viva Questions
A primary key is a column whose values uniquely identify every row in a table.
When a "one" table's primary key field is added to a related "many" table in order to create the common
field which relates the two tables, it is called a foreign key in the "many" table.
For example, the salary of an employee is stored in salary table. The relation is established via foreign
key column “Employee_ID_Ref” which refers “Employee_ID” field in the Employee table.
A set of attributes (one or more) that collectively identifies an entity in an entity set.
A minimal super key is called a candidate key. An entity set may have more than one candidate key.
6. What is a query?
A query with respect to DBMS relates to user commands that are used to interact with a data base. The
query language can be classified into data definition language and data manipulation language.
SQL Update is used to update data in a row or set of rows specified in the filter condition.
SQL Delete is used to delete a row or set of rows specified in the filter condition.
Schema Diagram:
Table creation:
Description of Tables:
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al14is001', '10is81',
'ise8a',15,16,18);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al14is001', '10is82',
'ise8a',10,9,6);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al14is003', '10isl88',
'ise8b',15,5,9);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al14is004', '10is82',
'ise8c',20,15,17);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al15is011', '15is62',
'ise6a',17,10,10);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al16is022', '15is41',
'ise4c',10,9,6);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al16is023', '15is42',
'ise4c',12,11,13);
insert into IAMARKS (Usn, Subcode, Ssid, Test1, Test2, Test3) values ('4al17is042', '15pcd22',
'ise2a',9,14,13);
Queries:
2. Compute the total number of male and female students in each semester and ineach section.
select ss.Sem, ss.Sec, s.Gender, count(s.Gender) as count
from STUDENT s, SEMSEC ss, CLASS c
where s.Usn = c.Usn and ss.Ssid = c.Ssid
group by ss.Sem, ss.Sec, s.Gender
order by Sem;
4. Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.
c_a number; c_b number; c_c number; c_sm number; c_av number;
begin
openc_iamarks; loop
fetchc_iamarks into c_a, c_b, c_c;
exit when c_iamarks%notfound; if (c_a != c_b) then
c_sm:=c_a+c_b; else
c_sm:=c_a+c_c; end if;
c_av:=c_sm/2;
update IAMARKS set Finalia=c_av where current of c_iamarks; end loop;
closec_iamarks; end;
/
(Note: This procedure will not update the average values in IAMARKS table until it has been called
explicitly. So each time when a new entry is done to IAMARKS table, procedure AVGMARKS can be
called to calculate and update the average marks.)
Below SQL code is to invoke the PL/SQL stored procedure from the command line:
begin AVGMARKS;
end;
/
Viva Question
1. Define Normalization.
Organized data void of inconsistent dependency and redundancy within a database is called
normalization.
No duplicate entries
Saves storage space
Boasts the query performances.
3. What is Entity?
An entity can be a real-world object, either animate or inanimate, that can be easily identifiable. For
example, in a school database, students, teachers, classes, and courses offered can be considered as
entities.
Binary = degree 2
Ternary = degree 3
n-ary = degree n
1. Make a list of all project numbers for projects that involve an employee whose last name is ‘Scott’,
either as a worker or as a manager of the department that controls the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent
raise.
3. Find the sum of the salaries of all employees the ‘Accounts’ department, as well as the maximum
salary, the minimum salary, and the average salary in this department
4. Retrieve the name of each employee who works on all the projects controlled by department
number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department number and the
number of its employees who are making more than Rs.6,00,000.
Entity-Relationship Diagram
Schema Diagram
TABLE CREATION
// FOR MYSQL:
create table EMPLOYEE (
Ssn varchar(8),
Fname varchar(10),
Lname varchar(10),
Address varchar(10),
Sex varchar(1),
Salary int,
Sup_Ssnvarchar(8),
Dno int,
primary key(Ssn),
foreign key(Sup_Ssn) references EMPLOYEE(Ssn)
);
// FOR ORACLE:
create table EMPLOYEE (
Ssn varchar(8),
Fname varchar(10),
Lname varchar(10),
Address varchar(10),
Sex varchar(1),
Salary int,
Sup_Ssnvarchar(8),
primary key(Ssn),
foreign key(Sup_Ssn) references EMPLOYEE(Ssn)
);
NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter
EMPLOYEE table to add foreign key constraint to EMPLOYEE.Dno using sql
command.
// FOR MYSQL:
alter table EMPLOYEE add constraint foreign key (Dno) references DEPARTMENT(Dno);
//ORACLE:
Table Descriptions
// FOR MYSQL:
//FOR ORACLE:
QUERIES:
1. Make a list of all project numbers for projects that involve an employee whose last name is ‘Scott’,
either as a worker or as a manager of the department that controls the project.
( select p.Pno
from PROJECT p, DEPARTMENT d, EMPLOYEE e
where p.Dno=d.Dno and d.MgrSsn=e.Ssn and e.Lname=’scott’)
UNION
( select p1.Pno
from PROJECT p1, WORKS_ON w, EMPLOYEE e1
where p1.Pno=w.Pno and e1.Ssn=w.Ssn and e1.Lname=’scott’);
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in thisdepartment
4. Retrieve the name of each employee who works on all the projects Controlled by department
number 5 (use NOT EXISTS operator).
FOR MYSQL:
FOR ORACLE:
5. For each department that has more than five employees, retrieve the department number and the
number of its employees who are making more than Rs. 6,00,000.
Viva question
1. What is Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the number of
entities of other set via relationship set.
One to one
One to many
Many to one
Many to many
One entity from entity set A can be associated with at most one entity of entity set B and vice versa.
One entity from entity set A can be associated with more than one entities of entity set B however an
entity from entity set B, can be associated with at most one entity.
More than one entities from entity set A can be associated with at most one entity of entity set B,
however an entity from entity set B can be associated with more than one entity from entity set A.
7.What is DDL?
DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP and RENAME
come under this.
8.What is DML?
DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT and UPDATE come
under this.
9.What is DCL?
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this.
Department of Information Science & Engineering, AJIET, Mangaluru. Page 63