0% found this document useful (0 votes)
53 views69 pages

Dbms Lab Manual 18CSL58

Uploaded by

sreeshraddha2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views69 pages

Dbms Lab Manual 18CSL58

Uploaded by

sreeshraddha2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELGAUM

DBMS LABORATORY WITH MINI PROJECT


(Subject Code: 18CSL58)

MASTER MANUAL
V-SEMESTER

Mrs Shilpa Shetty A Mrs Sharanya PS


Assistant Professor Assistant Professor
Department of Information Science & Engineering Department of Information Science & Engineering

A J INSTITUTE OF ENGINEERING & TECHNOLOGY


DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING
(A unit of Laxmi Memorial Education Trust. (R))
NH - 66, Kottara Chowki, Kodical Cross, Mangaluru- 575 006
COURSE OBJECTIVE

This course will enable students to:


 Design and implement various algorithms in JAVA
 Employ various design strategies for problem solving.

 Measure and compare the performance of different algorithms.

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.

1 VTU Syllabus For DBMS LAB WITH MINI PROJECT ii -iv

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. 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 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:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 1


DBMS LAB MANUAL – 18CSL58 2020-21

Schema Diagram:

Table Creation:

create table PUBLISHER (


name varchar(15),
address varchar(15),
phone varchar(10),
primary key (name)
);

create table BOOK (


book_id varchar(4),
title varchar(10),
publisher_name varchar(10),
pub_year int,
primary key (book_id),
foreign key(publisher_name) references PUBLISHER(name) on delete cascade
);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 2


DBMS LAB MANUAL – 18CSL58 2020-21

create table BOOK_AUTHORS (


book_id varchar(4),
author_name varchar(10),
primary key (book_id),
foreign key(book_id) references BOOK(book_id) on delete cascade
);

create table LIBRARY_PROGRAMME


(
programme_id varchar(4),
programme_name varchar(15),
address varchar(15),
primary key (programme_id)
);

create table BOOK_COPIES (


book_id varchar(4),
programme_id varchar(4),
no_of_copies int,
primary key (book_id, programme_id),
foreign key(book_id) references BOOK(book_id) on delete cascade,
foreign key(programme_id) references LIBRARY_PROGRAMME(programme_id) on delete
cascade
);

create table BOOK_LENDING


(
book_id varchar(4),
programme_id varchar(4),
card_no int,
date_out date,
due_date date,
primary key (book_id, programme_id, card_no),
foreign key(book_id) references BOOK(book_id) on delete cascade,
foreign key(programme_id) references LIBRARY_PROGRAMME(programme_id) on delete
cascade
);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 3


DBMS LAB MANUAL – 18CSL58 2020-21

Table Descriptions:

Insertion of Values to Tables:


insert into PUBLISHER values (’TMH’, ’Mangalore’,’9876543897’);
insert into PUBLISHER values (’Prism’, ’chennai’,’8756444324’);
insert into PUBLISHER values (’Himalaya’, ’Kolkata’,’9876556785’);
insert into PUBLISHER values (’Pearson’,’Delhi’,’9878987675’);
insert into PUBLISHER values (’Elsevier’,’Bangalore’,’7659876785’);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 4


DBMS LAB MANUAL – 18CSL58 2020-21

insert into BOOK_AUTHORS values (’b1’,’navathe’);


insert into BOOK_AUTHORS values (’b2’,’dsc’);
insert into BOOK_AUTHORS values (’b3’,’david’);
insert into BOOK_AUTHORS values (’b4’,’stuart’);
insert into BOOK_AUTHORS values (’b5’,’galvin’);

insert into LIBRARY_PROGRAMME values (’bh1’,’Book Corner’, ’Bangalore’);


insert into LIBRARY_PROGRAMME values (’bh2’,’Book Point’, ’Mangalore’);
insert into LIBRARY_PROGRAMME values (’bh3’,’Book Cafe’, ’Mumbai’);

insert into BOOK_COPIES values (’b1’,’bh1’,10);


insert into BOOK_COPIES values (’b1’,’bh2’,15);
insert into BOOK_COPIES values (’b2’,’bh2’,30);
insert into BOOK_COPIES values (’b2’,’bh3’,28);
insert into BOOK_COPIES values (’b3’,’bh1’,35);
insert into BOOK_COPIES values (’b3’,’bh2’,22);
insert into BOOK_COPIES values (’b4’,’bh1’,8);
insert into BOOK_COPIES values (’b5’,’bh3’,17);

(Note: For Mysqluse Date Format: ’YYYY-MM-DD’. Eg: ’2017-01-02’.)

insert into BOOK_LENDING values (’b1’,’bh1’,1,’02-jan-17’,’10-jan-17’);


insert into BOOK_LENDING values (’b2’,’bh2’,2,’11-jan-17’,’11-mar-17’);
insert into BOOK_LENDING values (’b3’,’bh1’,1,’21-feb-17’,’21-apr-17’);
insert into BOOK_LENDING values (’b5’,’bh3’,1,’15-mar-17’,’15-jul-17’);
insert into BOOK_LENDING values (’b3’,’bh2’,4,’12-apr-17’,’12-may-17’);
insert into BOOK_LENDING values (’b4’,’bh1’,1,’21-feb-17’,’21-apr-17’);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 5


DBMS LAB MANUAL – 18CSL58 2020-21

Department of Information Science & Engineering, AJIET, Mangaluru. Page 6


DBMS LAB MANUAL – 18CSL58 2020-21

Queries:
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each PROGRAMME, etc.

select b.book_id, b.title, b.publisher_name, a.author_name, c.no_of_copies, lb.programme_id


from BOOK b, BOOK_AUTHORS a, BOOK_COPIES c, LIBRARY_PROGRAMMEl b
where a.book_id = b.book_id and b.book_id=c.book_id and c.programme_id=lb.programme_id;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 7


DBMS LAB MANUAL – 18CSL58 2020-21

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.

delete from BOOK where book_id=’b4’;

(Note: records corresponds to book_id =’b4’ is also deleted from BOOK_AUTHORS,


LIBRARY_PROGRAMME, BOOK_COPIES and BOOK_LENDING tables because of the use of ‘on
delete CASCADE’ constraint on foreign keys).

Department of Information Science & Engineering, AJIET, Mangaluru. Page 8


DBMS LAB MANUAL – 18CSL58 2020-21

4. Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.

create view V_PUBLICATION as


select pub_year
from BOOK;

select * from V_PUBLICATION;

5. Create a view of all books and its number of copies that are currently available in the
Library.

create view V_BOOKS as


select b.book_id, b.title, c.programme_id, c.no_of_copies
from BOOK b, BOOK_COPIES c, LIBRARY_PROGRAMME lb
where b.book_id=c.book_id and c.programme_id=lb.programme_id;

select * from V_BOOKS;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 9


DBMS LAB MANUAL – 18CSL58 2020-21

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.

4. What is a Database system?


The database and DBMS software together is called as Database system.

5. What are the advantages of DBMS?


 Redundancy is controlled.
 Unauthorized access is restricted.
 Providing multiple user interfaces.
 Enforcing integrity constraints.
 Providing backup and recovery.

6. What is relational database?

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 10


DBMS LAB MANUAL – 18CSL58 2020-21

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.

10. What is a query?


A query with respect to DBMS relates to user commands that are used to interact with a data base.

Department of Information Science & Engineering, AJIET, Mangaluru. Page 11


DBMS LAB MANUAL – 18CSL58 2020-21

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

Entity-Relationship Diagram

Department of Information Science & Engineering, AJIET, Mangaluru. Page 12


DBMS LAB MANUAL – 18CSL58 2020-21

Schema Diagram

Table Creation

create table SALESMAN (


Salesman_idint, Name varchar(10), City varchar(10), Commission int,
primary key(Salesman_id)
);

create table CUSTOMER (


Customer_idint, Cust_namevarchar(10), City varchar(10),
Grade int, Salesman_idint,
primary key (Customer_id),
foreign key (Salesman_id) references SALESMAN (Salesman_id) on delete set NULL
);

create table ORDERS (


Ord_no int,
Purchase_amt int,
Ord_date date,
Customer_id int,
Salesman_id int,
primary key (Ord_no),
foreign key (Customer_id) references CUSTOMER (customer_id) on delete cascade,
Department of Information Science & Engineering, AJIET, Mangaluru. Page 13
DBMS LAB MANUAL – 18CSL58 2020-21

foreign key (Salesman_id) references SALESMAN (salesman_id) on delete cascade


);

Table Descriptions:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 14


DBMS LAB MANUAL – 18CSL58 2020-21

Insertion Of Values To Tables:

Salesman Details:

insert into SALESMAN values (1000, ’joseph’,’mysore’,’13’);


insert into SALESMAN values (1001, ’girish’,’bangalore’,’22’);
insert into SALESMAN values (1002, ’mukund’,’mumbai’,’16’);
insert into SALESMAN values (1003, ’saurabh’,’delhi’,’19’);
insert into SALESMAN values (1004, ’srinivas’,’hydrabad’,’23’);
insert into SALESMAN values (1005, ’mohan’,’ranchi’,’23’);

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:

(NOTE: Use ‘YYYY-MM-DD’ date format for MySQL, Example:’2017-01-04’)


insert into ORDERS values (111, 50000, ’04-jan-17’, 1, 1004);
insert into ORDERS values (222, 45000, ’04-jan-17’, 2, 1000);
insert into ORDERS values (333, 10000, ’05-feb-17’, 3, 1002);
insert into ORDERS values (444, 35000, ’13-mar-17’, 4, 1003);
insert into ORDERS values (555, 75000, ’14-mar-17’, 5, 1001);
insert into ORDERS values (666, 25000, ’14-mar-17’, 6, 1004);
insert into ORDERS values (777, 5000, ’27-jun-17’, 7, 1003);
insert into ORDERS values (888, 52000, ’25-aug-17’, 8, 1001);
insert into ORDERS values (991, 37000, ’25-aug-17’, 1, 1004);
insert into ORDERS values (992, 29000, ’09-sep-17’, 2, 1000);
insert into ORDERS values (993, 6000, ’09-sep-17’, 9, 1005);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 15


DBMS LAB MANUAL – 18CSL58 2020-21

Department of Information Science & Engineering, AJIET, Mangaluru. Page 16


DBMS LAB MANUAL – 18CSL58 2020-21

Queries:

1. Count the customers with grades above Bangalore’saverage.

select Grade, COUNT (distinct Customer_id) as Total_Customers


from CUSTOMER
group by Grade
having Grade > (select AVG(Grade)
from CUSTOMER
where City='bangalore');

2. Find the name and numbers of all salesmen who had more than one customer.

select s.Salesman_id, s.Name


from SALESMAN s
where (select COUNT (*)
from CUSTOMER c
where c.Salesman_id=s.Salesman_id) > 1;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 17


DBMS LAB MANUAL – 18CSL58 2020-21

3. List all salesmen and indicate those who have and don’t have customers in their cities (Use UNION
operation.)

(select a.Salesman_id, a.Name, b.Cust_name, a.Commission, a.City


from SALESMAN a, CUSTOMER b
where a.City = b.City)
UNION
(select Salesman_id, Name, 'No Match', Commission, City
from SALESMAN
where NOT City = ANY (select City
from CUSTOMER))
ORDER BY 2 DESC;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 18


DBMS LAB MANUAL – 18CSL58 2020-21

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 19


DBMS LAB MANUAL – 18CSL58 2020-21

Query-2:

select * from TOPSALESMAN;

5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also
bedeleted.

delete from SALESMAN


where Salesman_Id=1000;

We can verify from the above snapshot that, a Salesman named ‘JOSEPH’ with ID 1000 has been
removed from the SALESMAN table.

Department of Information Science & Engineering, AJIET, Mangaluru. Page 20


DBMS LAB MANUAL – 18CSL58 2020-21

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 21


DBMS LAB MANUAL – 18CSL58 2020-21

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.

2. What is Single valued Attributes ?

An attribute, that has a single value for a particular entity. For example, age of a employee entity.

3. What is Multi valued Attributes?

An attributes that may have multiple values for the same entity. For example colors of a car entity.

4. What is Compound /Composite Attribute?

Attribute can be subdivided into two or more other Attribute. For Example, Name can be divided into
First name, Middle name and Last name.

5. What is Simple/Atomic Attributes?

The attributes which cannot be divided into smaller subparts are called simple or atomic attributes. For
example, age of employee entity

6. What is Stored Attribute?

An attribute, which cannot be derived from other attribute, is known as stored attribute. For example,
BirthDate of employee.

7. What is Derived Attribute ?

Attributes derived from other stored attribute. For example age from Date of Birth and Today’s date.

8. What is Complex Attributes?

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.

9. What is Key Attribute ?

Department of Information Science & Engineering, AJIET, Mangaluru. Page 22


DBMS LAB MANUAL – 18CSL58 2020-21

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.

10. What is Non Key Attributes ?

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.

Department of Information Science & Engineering, AJIET, Mangaluru. Page 23


DBMS LAB MANUAL – 18CSL58 2020-21

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.

Entity-Relationship Diagram:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 24


DBMS LAB MANUAL – 18CSL58 2020-21

Schema Diagram:

Table Creation :

create table ACTOR (


Act_id varchar(3),
Act_name varchar(10),
Act_gender varchar(1),
primary key(Act_id)
);

create table DIRECTOR (


Dir_id varchar(3),
Dir_name varchar(20),
Dir_phone varchar(10),
primary key(Dir_id)
);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 25


DBMS LAB MANUAL – 18CSL58 2020-21

create table MOVIES (


Mov_id varchar(3),
Mov_title varchar(20),
Mov_year int,
Mov_lang varchar(10),
Dir_id varchar(3),
primary key(Mov_id),
foreign key(Dir_id) references DIRECTOR(Dir_id) on delete set NULL
);

create table MOVIE_CAST (


Act_id varchar(3),
Mov_id varchar(3),
Role varchar(10),
primary key (Act_id, Mov_id),
foreign key (Act_id) references actor (Act_id) on delete set NULL,
foreign key (Mov_id) references movies (Mov_id) on delete set NULL
);

create table RATING (


Mov_id varchar(3),
Rev_stars int,
primary key(Mov_id, Rev_stars),
foreign key(Mov_id) references MOVIES (Mov_id) on delete set NULL
);

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:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 26


DBMS LAB MANUAL – 18CSL58 2020-21

Insertion Of Values To the Tables:


insert into ACTOR values (’a1’,’robert d’,’m’);
insert into ACTOR values (’a2’,’scarlett’,’f’);
insert into ACTOR values (’a3’,’puneeth’,’m’);
insert into ACTOR values (’a4’,’meera’,’f’);
insert into ACTOR values (’a5’,’prabhas’,’m’);
insert into ACTOR values (’a6’,’anushka’,’f’);

insert into DIRECTOR values (’d1’,’hitchcock’, ’7690870681’);


insert into DIRECTOR values (’d2’,’steven spielberg’, ’7986554437’);
insert into DIRECTOR values (’d3’,’mahesh babu’, ’8765675304’);
insert into DIRECTOR values (’d4’,’rajamouli’, ’9651232245’);

insert into MOVIES values (’m1’,’iron Man-1’, 1990, ’english’,’d1’);


insert into MOVIES values (’m2’,’munna’, 1998, ’telugu’,’d3’);
insert into MOVIES values (’m3’,’iron Man-2’, 2001, ’english’,’d2’);
insert into MOVIES values (’m4’,’arasu’, 2007, ’kannada’,’d3’);
insert into MOVIES values (’m5’,’iron Man-3’, 2016, ’english’,’d2’);
insert into MOVIES values (’m6’,’bahubali-2’, 2017, ’telugu’,’d4’);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 27


DBMS LAB MANUAL – 18CSL58 2020-21

insert into MOVIE_CAST values (’a1’, ’m1’,’hero’);


insert into MOVIE_CAST values (’a5’, ’m2’, ’hero’);
insert into MOVIE_CAST values (’a1’, ’m3’, ’hero’);
insert into MOVIE_CAST values (’a2’, ’m3’, ’heroine’);
insert into MOVIE_CAST values (’a2’, ’m5’, ’guest’);
insert into MOVIE_CAST values (’a3’, ’m4’, ’hero’);
insert into MOVIE_CAST values (’a4’, ’m4’, ’heroine’);
insert into MOVIE_CAST values (’a1’, ’m5’, ’hero’);
insert into MOVIE_CAST values (’a5’, ’m6’, ’hero’);
insert into MOVIE_CAST values (’a6’, ’m6’, ’heroine’);

insert into RATING values (’m1’,8);


insert into RATING values (’m2’,4);
insert into RATING values (’m3’,6);
insert into RATING values (’m4’,8);
insert into RATING values (’m5’,7);
insert into RATING values (’m6’,9);
insert into RATING values (’m2’,9);
insert into RATING values (’m1’,4);

Initial Database With Valid Data:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 28


DBMS LAB MANUAL – 18CSL58 2020-21

Department of Information Science & Engineering, AJIET, Mangaluru. Page 29


DBMS LAB MANUAL – 18CSL58 2020-21

Queries:

1. List the titles of all movies directed by‘Hitchcock’.

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;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 30


DBMS LAB MANUAL – 18CSL58 2020-21

Alternate Query:(with actor’s name)

select Mov_title, a.Act_name


from MOVIES m, MOVIE_CAST mc, ACTOR a
where m.Mov_id=mc.Mov_id and mc.Act_id=a.Act_id and mc.Act_id IN (select Act_id
from MOVIE_CAST
group by Act_id
having COUNT (*)>1);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 31


DBMS LAB MANUAL – 18CSL58 2020-21

3. List all actors who acted in a movie before 2000 and also in a movie after 2015(use JOIN
operation).

select Act_name, Mov_title, Mov_year


from ACTOR a JOIN MOVIE_CAST c ON a.Act_id=c.Act_id JOIN MOVIES m ON
Mov_id=m.Mov_id
where m.Mov_year NOT BETWEEN 2000 and 2015;

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.

select Mov_title, MAX(Rev_stars)


from MOVIES INNER JOIN RATING using(Mov_id)
group by Mov_title
having MAX(Rev_stars)>0
order by Mov_title;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 32


DBMS LAB MANUAL – 18CSL58 2020-21

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;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 33


DBMS LAB MANUAL – 18CSL58 2020-21

5. Update rating of all movies directed by ‘Steven Spielberg’ to5.

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 34


DBMS LAB MANUAL – 18CSL58 2020-21

Viva Questions

1. What is a primary key?

A primary key is a column whose values uniquely identify every row in a table.

2. What are the conditions for a field to be a primary key?

 No two rows can have the same primary key value.


 Every row must have a primary key value.
 The primary key field cannot be null.
 Value in a primary key column can never be modified or updated, if any foreign key refers to that
primary key.

3. What is a Foreign Key ?

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.

4. What is Super Key?

A set of attributes (one or more) that collectively identifies an entity in an entity set.

5. What is Candidate Key

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.

7. Define SQL Insert Statement ?

SQL INSERT statement is used to add rows to a table.

8. Define SQL Update Statement ?

SQL Update is used to update data in a row or set of rows specified in the filter condition.

Department of Information Science & Engineering, AJIET, Mangaluru. Page 35


DBMS LAB MANUAL – 18CSL58 2020-21

9. Define SQL Delete Statement ?

SQL Delete is used to delete a row or set of rows specified in the filter condition.

10. What is order by clause?


ORDER BY clause helps to sort the data in either ascending order to descending

Department of Information Science & Engineering, AJIET, Mangaluru. Page 36


DBMS LAB MANUAL – 18CSL58 2020-21

4. Consider the schema for College Database:

STUDENT (USN, SName, Address, Phone, Gender)


SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (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 subjects.
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.

Entity - Relationship Diagram

Department of Information Science & Engineering, AJIET, Mangaluru. Page 37


DBMS LAB MANUAL – 18CSL58 2020-21

Schema Diagram:

Table creation:

Create table STUDENT (


Usn varchar(10),
Sname varchar(10),
Address varchar(10),
Phone varchar(10),
Gender varchar(1),
primary key(Usn)
);

create table SEMSEC (


Ssid varchar(6),
Sem int,
Sec varchar(2),
primary key(Ssid)
);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 38


DBMS LAB MANUAL – 18CSL58 2020-21

create table CLASS (


Usn varchar(10),
Ssid varchar(6),
primary key(Usn,Ssid),
foreign key(Usn) references STUDENT(Usn),
foreign key(Ssid) references SEMSEC(Ssid)
);

create table SUBJECT (


Subcode varchar(7),
Title varchar(20),
Sem int,
Credits int,
primary key(Subcode)
);

create table IAMARKS (


Usn varchar(10),
Subcode varchar(7),
Ssid varchar(6),
Test1 int,
Test2 int,
Test3 int,
Finalia int,
primary key(Usn,Subcode,Ssid),
foreign key(Usn) references STUDENT(Usn),
foreign key(Ssid) references SEMSEC(Ssid),
foreign key(Subcode) references SUBJECT(Subcode)
);

Description of Tables:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 39


DBMS LAB MANUAL – 18CSL58 2020-21

Inserting initial values into tables:

insert into STUDENT values ('4al14is001','akshay','mangaluru', 8877881122,'m');


insert into STUDENT values ('4al14is002','sandhya','bengaluru', 7722829912,'f');
insert into STUDENT values ('4al14is003','trupti','bengaluru', 7712312312,'f');
insert into STUDENT values ('4al14is004','supriya','mangaluru', 8877881122,'f');
insert into STUDENT values ('4al15is010','abhay','bengaluru', 9900211201,'m');
insert into STUDENT values ('4al15is011','darshan','bengaluru', 9923211099,'m');

Department of Information Science & Engineering, AJIET, Mangaluru. Page 40


DBMS LAB MANUAL – 18CSL58 2020-21

insert into STUDENT values ('4al15is012','ashwitha','bengaluru', 7894737377,'f');


insert into STUDENT values ('4al16is020','ajay','tumkur', 9845091341,'m');
insert into STUDENT values ('4al16is021','sanjana','kundapura', 7696772121,'f');
insert into STUDENT values ('4al16is022','krishna','bellary', 9944850121,'m');
insert into STUDENT values ('4al16is023','santosh','mangaluru', 8812332201,'m');
insert into STUDENT values ('4al17is040','lokesh','kalburgi', 9900232201,'m');
insert into STUDENT values ('4al17is041','ashika','shimoga', 9905542212,'f');
insert into STUDENT values ('4al17is042','vinayaka','bijapura', 8800880011,'m');

insert into SEMSEC values ('ise8a',8,’a’);


insert into SEMSEC values (’ise8b’,8,'b’);
insert into SEMSEC values (’ise8c’,8,'c’);
insert into SEMSEC values ('ise6a',6,'a');
insert into SEMSEC values ('ise4a',4,'a');
insert into SEMSEC values (’ise4b',4,'b');
insert into SEMSEC values (’ise4c',4,'c');
insert into SEMSEC values (’ise2a’,2,’a’);

insert into CLASS values ('4al14is001',’ise8a’);


insert into CLASS values ('4al14is002','ise8a');
insert into CLASS values ('4al14is003',’ise8b’);
insert into CLASS values ('4al14is004',’ise8c’);
insert into CLASS values ('4al15is010',’ise6a’);
insert into CLASS values ('4al15is011',’ise6a’);
insert into CLASS values ('4al15is012',’ise6a’);
insert into CLASS values ('4al16is020',’ise4a’);
insert into CLASS values ('4al16is021',’ise4b’);
insert into CLASS values ('4al16is022',’ise4c’);
insert into CLASS values ('4al16is023',’ise4c’);
insert into CLASS values ('4al17is040',’ise2a');
insert into CLASS values ('4al17is041',’ise2a');
insert into CLASS values ('4al17is042',’ise2a’);

insert into SUBJECT values ('10is81','PW',8,4);


insert into SUBJECT values ('10is82','INS',8,4);
insert into SUBJECT values ('10isl88','PWL',8,2);
insert into SUBJECT values ('15is61','CN',6, 4);
insert into SUBJECT values ('15is62','DBMS',6,4);
insert into SUBJECT values ('15is41','DMS',4,4);
insert into SUBJECT values ('15is42','ADE',4,4);
insert into SUBJECT values ('15che21','Chemistry',2,4);
insert into SUBJECT values ('15pcd22','PCD',2,4);
Department of Information Science & Engineering, AJIET, Mangaluru. Page 41
DBMS LAB MANUAL – 18CSL58 2020-21

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

Tables with values:

Department of Information Science & Engineering, AJIET, Mangaluru. Page 42


DBMS LAB MANUAL – 18CSL58 2020-21

Department of Information Science & Engineering, AJIET, Mangaluru. Page 43


DBMS LAB MANUAL – 18CSL58 2020-21

Queries:

1. List all the student details studying in fourth semester ‘C’section.

select s.*, ss.Sem, ss.Sec


from STUDENT s, SEMSEC ss, CLASS c
where s.Usn = c.Usn and ss.Ssid = c.Ssid and ss.Sem = 4 and ss.Sec=’c’;

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;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 44


DBMS LAB MANUAL – 18CSL58 2020-21

3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in allsubjects.

create view Test1_view as


select Test1, Subcode
from IAMARKS
where Usn = '4al14is001';

Department of Information Science & Engineering, AJIET, Mangaluru. Page 45


DBMS LAB MANUAL – 18CSL58 2020-21

4. Calculate the FinalIA (average of best two test marks) and update the corresponding table
for all students.

create or replace procedure AVGMARKS is cursor c_iamarks is


select greatest(Test1,Test2) as a, greatest(Test1,Test3) as b, greatest(Test3,Test2) as c
from IAMARKS
where Finalia is null for update;

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 46


DBMS LAB MANUAL – 18CSL58 2020-21

Below SQL code is to invoke the PL/SQL stored procedure from the command line:
begin AVGMARKS;
end;
/

Department of Information Science & Engineering, AJIET, Mangaluru. Page 47


DBMS LAB MANUAL – 18CSL58 2020-21

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’
th
Give these details only for 8 semester A, B, and C section students.

Select s.Usn, s.Sname, s.Address, s.Phone, s.Gender, (


case when ia.finalia between 17 and 20 then 'outstanding'
when ia.finalia between 12 and 16 then 'average'
else 'weak'
end)
as cat
from STUDENT s, SEMSEC ss, IAMARKS ia, SUBJECT sub
where s.Usn = ia.Usn and ss.Ssid = ia.Ssid and sub.Subcode = ia.Subcode and sub.Sem = 8;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 48


DBMS LAB MANUAL – 18CSL58 2020-21

Viva Question
1. Define Normalization.
Organized data void of inconsistent dependency and redundancy within a database is called
normalization.

2. Enlist the advantages of normalizing database.


Advantages of normalizing database are:

 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.

4. What is entity set?


An entity set is a collection of similar types of entities. An entity set may contain entities with attribute
sharing similar values. For example, a Students set may contain all the students of a school; likewise a
Teachers set may contain all the teachers of a school from all faculties. Entity sets need not be disjoint.
5. What is Relationship?
The association among entities is called a relationship. For example, an employee works_at a
department, a student enrolls in a course. Here, Works_at and Enrolls are called relationships.

6. What is Relationship Set?


A set of relationships of similar type is called a relationship set.

7. What is Degree of Relationship?


The number of participating entities in a relationship defines the degree of the relationship.

8. Name the Degree of Relationship?

 Binary = degree 2
 Ternary = degree 3
 n-ary = degree n

Department of Information Science & Engineering, AJIET, Mangaluru. Page 49


DBMS LAB MANUAL – 18CSL58 2020-21

9. What is Data Model?


A collection of conceptual tools for describing data, data relationships data semantics and constraints.
10. What is E-R model?
This data model is based on real world that consists of basic objects called entities and of relationship
among these objects. Entities are described in a database by a set of attributes .

Department of Information Science & Engineering, AJIET, Mangaluru. Page 50


DBMS LAB MANUAL – 18CSL58 2020-21

5. Consider the schema for Company Database:

EMPLOYEE (SSN, Name, Address, Sex, Salary, Sup_Ssn, 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 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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 51


DBMS LAB MANUAL – 18CSL58 2020-21

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 52


DBMS LAB MANUAL – 18CSL58 2020-21

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

create table DEPARTMENT (


Dno int,
Dname varchar(20),
MgrSsn varchar(8),
Mgr_sdate date,
primary key(Dno),
foreign key(MgrSsn) 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:

ALTER TABLE EMPLOYEE ADD Dno INT REFERENCES DEPARTMENT(Dno);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 53


DBMS LAB MANUAL – 18CSL58 2020-21

create table DLOCATION (


Dno int,
Dloc varchar(20),
primary key (Dno,Dloc),
foreign key(Dno) references DEPARTMENT(Dno)
);

create table PROJECT (


Pno int,
Pname varchar(20),
Plocation varchar(20),
Dno int,
primary key(Pno),
foreign key(Dno) references DEPARTMENT (Dno)
);

create table WORKS_ON (


Pno int,
Ssn varchar(8),
Hours int,
primary key(Ssn,Pno),
foreign key(Ssn) references EMPLOYEE(Ssn),
foreign key(Pno) references PROJECT(Pno)
);

Table Descriptions

Department of Information Science & Engineering, AJIET, Mangaluru. Page 54


DBMS LAB MANUAL – 18CSL58 2020-21

Insertion Of Values To Tables:

inserting values into employee table:


insert into EMPLOYEE values ('alis01','john','scott','bangalore','m',2000000,NULL,NULL);
insert into EMPLOYEE values ('alis02','james','smith','kolar','m',1500000,'alis01',NULL);
insert into EMPLOYEE values ('alis03','william','baker','bangalore','m',1500000,'alis01', NULL);
insert into EMPLOYEE values ('alis04','elson','scott','mysore','m',1500000,'alis01',NULL);
insert into EMPLOYEE values ('alis05','pavan','hegde','mangalore','m',700000,'alis02', NULL);
insert into EMPLOYEE values ('alis06','girish','jain','mysore','m',1000000,'alis03',NULL);
insert into EMPLOYEE values ('alis07','neha','salian','bangalore','f',600500,'alis02',NULL);
insert into EMPLOYEE values ('alis08','ashika','hegde','mangalore','f',800000,'alis04', NULL);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 55


DBMS LAB MANUAL – 18CSL58 2020-21

insert into EMPLOYEE values ('alis09','santhosh','kumar','mumbai','m',500000,'alis02', NULL);


insert into EMPLOYEE values ('alis10','mythri','m','mysore','f',300000,'alis02',NULL);
insert into EMPLOYEE values ('alis11','nagesh','tantri','bangalore','m',900000,'alis04', NULL);
insert into EMPLOYEE values ('alis12','vignesh','g','bangalore','m',650000,'alis02',NULL);
insert into EMPLOYEE values ('alis13','kaveri','k','mangalore','f',750000,'alis01',NULL);

Inserting Values Into Department Table:

// FOR MYSQL:

insert into DEPARTMENT values(1,'accounts','alis02','2001-01-01');


insert into DEPARTMENT values(2,'marketing','alis03','2016-08-11');
insert into DEPARTMENT values(3,'it','2008-03-23','alis04');
insert into DEPARTMENT values(4,'production','alis08','2012-08-10');
insert into DEPARTMENT values(5,'support','alis01','2010-03-05');

//FOR ORACLE:

insert into DEPARTMENT values(1,'accounts','alis02','01-jan-01');


insert into DEPARTMENT values(2,'marketing','alis03','11-aug-16');
insert into DEPARTMENT values(3,'it','alis04','23-mar-08');
insert into DEPARTMENT values(4,'production','alis08','10-aug-12');
insert into DEPARTMENT values(5,'support','alis01','05-mar-10');

Update Entries Of Employee Table To Fill Missing DNO:

update EMPLOYEE set Dno=5 where Ssn='alis01';


update EMPLOYEE set Dno=1 where Ssn='alis02';
update EMPLOYEE set Dno=2 where Ssn='alis03';
update EMPLOYEE set Dno=3 where Ssn='alis04';
update EMPLOYEE set Dno=1 where Ssn='alis05';
update EMPLOYEE set Dno=2 where Ssn='alis06';
update EMPLOYEE set Dno=1 where Ssn='alis07';
update EMPLOYEE set Dno=4 where Ssn='alis08';
update EMPLOYEE set Dno=1 where Ssn='alis09';
update EMPLOYEE set Dno=1 where Ssn='alis10';
update EMPLOYEE set Dno=3 where Ssn='alis11';
update EMPLOYEE set Dno=1 where Ssn='alis12';
update EMPLOYEE set Dno=5 where Ssn='alis13';

Department of Information Science & Engineering, AJIET, Mangaluru. Page 56


DBMS LAB MANUAL – 18CSL58 2020-21

Inserting Values Into DLOCATION Table:

insert into DLOCATION values(1,'bangalore');


insert into DLOCATION values(2,'bangalore');
insert into DLOCATION values(3,'bangalore');
insert into DLOCATION values(1,'mangalore');
insert into DLOCATION values(3,'mangalore');
insert into DLOCATION values(4,'mysore');
insert into DLOCATION values(5,'hubli');

Inserting Values Into PROJECT Table:

insert into PROJECT values(100,'market_s','bangalore',1);


insert into PROJECT values(101,'stocks','bangalore',1);
insert into PROJECT values(102,'GST_b','bangalore',1);
insert into PROJECT values(103,'T_cards','bangalore',2);
insert into PROJECT values(104,'Jio_money','bangalore',2);
insert into PROJECT values(105,'iot','bangalore',3);
insert into PROJECT values(106,'Pro_xl','bangalore',4);
insert into PROJECT values(107,'project_j','bangalore',5);
insert into PROJECT values(108,'project_d','bangalore',5);

Inserting Values Into WORKS_ON Table:

insert into WORKS_ON(Pno,Ssn,Hours) values(100,'alis02',20);


insert into WORKS_ON(Pno,Ssn,Hours) values(100,'alis09',30);
insert into WORKS_ON(Pno,Ssn,Hours) values(101,'alis10',10);
insert into WORKS_ON(Pno,Ssn,Hours) values(101,'alis02',34);
insert into WORKS_ON(Pno,Ssn,Hours) values(102,'alis12',25);
insert into WORKS_ON(Pno,Ssn,Hours) values(102,'alis07',65);
insert into WORKS_ON(Pno,Ssn,Hours) values(103,'alis03',34);
insert into WORKS_ON(Pno,Ssn,Hours) values(104,'alis06',22);
insert into WORKS_ON(Pno,Ssn,Hours) values(105,'alis11',12);
insert into WORKS_ON(Pno,Ssn,Hours) values(107,'alis13',34);
insert into WORKS_ON(Pno,Ssn,Hours) values(107,'alis08',63);
insert into WORKS_ON(Pno,Ssn,Hours) values(107,'alis01',27);
insert into WORKS_ON(Pno,Ssn,Hours) values(108,'alis13',10);
insert into WORKS_ON(Pno,Ssn,Hours) values(108,'alis08',30);
insert into WORKS_ON(Pno,Ssn,Hours) values(108,'alis05',20);
insert into WORKS_ON(Pno,Ssn,Hours) values(105,'alis04',12);

Department of Information Science & Engineering, AJIET, Mangaluru. Page 57


DBMS LAB MANUAL – 18CSL58 2020-21

Department of Information Science & Engineering, AJIET, Mangaluru. Page 58


DBMS LAB MANUAL – 18CSL58 2020-21

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

Department of Information Science & Engineering, AJIET, Mangaluru. Page 59


DBMS LAB MANUAL – 18CSL58 2020-21

2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent raise.

select e.Fname, e.Lname, 1.1*e.Salary as incr_sal


from EMPLOYEE e, WORKS_ON w, PROJECT p
where e.Ssn=w.Ssn and w.Pno=p.Pno and p.Pname=’iot’;

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

select sum(e.Salary) as total_salary, max(e.Salary) as max_salary, min(e.Salary) as min_salary,


avg(e.Salary) as average_salary
from EMPLOYEE e, DEPARTMENT d
where e.Dno=d.Dno and d.dname='accounts';

4. Retrieve the name of each employee who works on all the projects Controlled by department
number 5 (use NOT EXISTS operator).

FOR MYSQL:

select e.Fname, e.Lname


from EMPLOYEE e
where NOT EXISTS( select *
from WORKS_ON w
where w.Pno IN (select p.Pno
from PROJECT p
where p.Dno=5)
and NOT EXISTS (select *
from WORKS_ON o

Department of Information Science & Engineering, AJIET, Mangaluru. Page 60


DBMS LAB MANUAL – 18CSL58 2020-21

where o.Ssn=e.Ssn and o.Pno=w.Pno));

FOR ORACLE:

select e.fname, e.lname


from employee e
where NOT EXISTS((select Pno
from project
where Dno=’5’)
minus
(select Pno
from works_on
where e.Ssn=Ssn));

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.

select d.Dno, count (*)


from DEPARTMENT d, EMPLOYEE e
where d.Dno=e.Dno and e.Salary>600000 and d.Dno in (select e1.Dno
from EMPLOYEE e1
group by e1.Dno
having count (*)>5)
group by d.Dno;

Department of Information Science & Engineering, AJIET, Mangaluru. Page 61


DBMS LAB MANUAL – 18CSL58 2020-21

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.

2. What are the different types of Mapping

 One to one

 One to many

 Many to one

 Many to many

3. What is One-to-one mapping?

One entity from entity set A can be associated with at most one entity of entity set B and vice versa.

4. What is One-to-many mapping?

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.

Department of Information Science & Engineering, AJIET, Mangaluru. Page 62


DBMS LAB MANUAL – 18CSL58 2020-21

5.What is Many-to-one mapping?

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.

6 .What is Many-to-many mapping?


One entity from A can be associated with more than one entity from B and vice versa.

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

You might also like