Rdbms LC Labguide
Rdbms LC Labguide
Rdbms LC Labguide
RDBMS
Exercises for Hands-on
Background
This document contains the assignments to be completed as part of the hands on for the
subject RDBMS (Course code: DB07).
Day 1
Assignment 1
Draw an ER diagram to capture the requirements as stated below:
A Company has several business units. Each business unit has multiple projects. Employees
must be assigned to one business unit. One or more employees are assigned to a project, but
an employee may be on vacation and not assigned to any projects. One of the assigned
employees will be project manager for that project.
Assignment 2
Draw an ER diagram to capture the requirements as stated below:
In a hospital there are different departments. Patients are treated in these departments by
the doctors assigned to patients. Usually each patient is treated by a single doctor, but in
rare cases they will have two or three. Healthcare assistants will also attend to patients;
every department has many healthcare assistants. Each patient is required to take a variety
of drugs during different parts of the day such as morning, afternoon and night.
Assignment 3
Draw an ER diagram to capture the requirements as stated below:
A toy manufacturing company manufactures different types of toys. The company has several
manufacturing plants. Each plant manufactures different types of toys. A customer can place
the order for these toys. Each order may contain one or more toys. Each customer has
multiple ship-to addresses. To promote the business, the company offers different schemes
based on the order value.
Day 2
Assignment 4
Assignment 5
Let us find out the Primany Key and highest normal form for this relation:
Solution:
Determining primary key : From above stated functional dependencies, it is evident that a,e
determines c and d . d determines e and hence a,e determines all the non-key attributes
(b,c,d) of the relation. Thus a,e is the primary key for relation R.
Highest normal form: All the attributes are atomic in nature therefore Relation R is in 1NF.
All non-key attributes (b,c,d) are fully functionally dependent on the primary key (a,e).
Therefore the relation in is 2NF.
All the non-key attributes (b,c,d) are non-transitively depending on primary key (a,e).
Therefore the relation is in 3NF.
e is a part of the primary key, therefore its dependence on d does not violate 3NF definition.
Assignment 6
Find the primary key and the highest normal form of following relations
Assignment 7
Convert following table to 3NF table. Please add appropriate columns in normalized tables to make 3NF compliant.
Sell
CustomerID Customer_Name Customer_Address Product_Name Product_Brand Product_Category Price Date_of_Transaction Qty
21, Lion Blvd, St Detergent and
101 Lawrence Peter Excel Surf Powder 43 11-Feb-05 2
29, Tiger Blvd, Cookies and
102 Jagan Burbank GlucoBis Goodday Confectionaries 8 22-Mar-05 12
213,Parliament
103 Bobby Blvd, Petas Rotomax KingTime Watch and clocks 120 27-Mar-05 8
401,Gazipura
104 Ramsey Blvd,Gulbarga SouthernStar CoolPlus Garments 63 5-Apr-05 3
101,Marshal
105 Chang Hu Blvd,Kingston Winner Fusion Stationary 3 22-Apr-05 27
Assignment 8
Draw the E-R diagram for above normalized Relational schema.
Day 3
Assignment 9
Configuring ORACLE net service on your machine
Objective: To learn how to create host string using net service to connect oracle client to the
oracle server.
Background: Although on your machine net service is pre configured but please follow these
steps to get a feel of how actually this has been configured.
Step 1:
Step2:
Step 3:
Enter Net Service Name as training (If it already exists chose any other name)
Step 4:
Step 5:
Enter Host Name given to you and don’t change port number (default is 1521)
Step 6:
Click next
Step 7:
Default it will test with the userid as “scott” and password as “tiger”,but you give your
UserID and password and test that also
Click on change login button and give your userid and password
Once again test the connection after giving username and password
Step 8:
After that login into SQL plus by following the set of links as given below
Enter your username ex trng180 password is Infosys and host string is training
Assignment 10
Solving simple queries
EMP
Column name Data type Description
DEPT
Column name Data type Description
Assignment 11
d) List the names of employees whose employee numbers are 7369, 7521, 7839, 7934,
7788.
f) List employee names for those who have joined between 30 June and 31 Dec. ‘81.
h) List the names of employees who are not eligible for commission.
i) List the name and designation of the employee who does not report to anybody.
m) List names of employees whose names have “i” as the second character.
Assignment 12
Understanding a simple query
Problem Statement: Let us try to understand how the following query works:
SELECT ename FROM emp WHERE deptno NOT IN (30, 40, 10);
Step 1: The query filters those tuples which do not belong to deptno 30, 40 or 50, because of
the operator NOT IN, from the emp table.
Step 2: Finally it displays only the employee names of the filtered tuples because we specify
only ename in the SELECT clause.
Day 4
Assignment 13
Objective: To visualize how group by and having works.
Problem Statement: Let us try to understand how the following query works:
“List the total salary, maximum and minimum salary and average salary of the employees
jobwise, for department 20 and display only those rows having an average salary > 1000”
SELECT job, avg(sal) from emp where deptno=20 group by job having avg(sal) > 1000
order by job;
Let us explore how this query is working.
Note: All columns in the column clause of your select statement that are not in
a group function must be listed in the group by clause. However a column
listed in the group by clause needn’t appear in the column clause.
Step 1: First thing first we have to select (filter) data only for dept 20. For this we have to
put
WHERE deptno=20
Step 2: Now, we have to find average salary of the employees job wise. Therefore we have
to group our selected (filtered) data using group by clause.
GROUP BY job
Step 3: Now, we have to display only those rows having an average salary > 1000. Therefore
we have to put
Step 4: For getting the final output in ascending order of job we have to put order by clause.
ORDER BY job
Note: Order by clause should be the last clause in the query otherwise you will
get the error as follows (In oracle): ORA-00933: SQL command not properly
ended.
Assignment 14
14. Perform the following queries against EMP and DEPT tables:
a) List the number of employees and average salary for employees in department 20.
b) List name, salary and PF amount of all employees. (PF is calculated as 10% of basic
salary)
c) List names of employees who are more than 2 years old in the company.
d) List the employee details in the ascending order of their basic salary.
e) List the employee name and hire date in the descending order of the hire date.
f) List employee name, salary, PF, HRA, DA and gross; order the results in the ascending
order of gross. HRA is 50% of the salary and DA is 30% of the salary.
g) List the department numbers and number of employees in each department.
h) List the department number and total salary payable in each department.
i) List the jobs and number of employees in each job. The result should be in the
descending order of the number of employees.
j) List the total salary, maximum and minimum salary and average salary of the
employees jobwise.
k) List the total salary, maximum and minimum salary and average salary of the
employees, for department 20.
l) List the average salary of the employees job wise, for department 20 and display only
those rows having an average salary > 1000
Assignment 15
Parts
P# PName Color Weight City
P1 Nut Red 12.0 London
P2 Bolt Green 17.0 Paris
P3 Screw Blue 17.0 Rome
P4 Screw Red 14.0 London
P5 Cam Blue 12.0 Paris
P6 Cog Red 19.0 London
Projects
J# Jname City
J1 Montago London
J2 Cat Paris
J3 Box London
J4 Montago Rome
J5 Eagles Athens
Shipment
S# P# J# Qty
S1 P1 J1 350
S1 P3 J3 120
S2 P1 J1 620
S3 P2 J3 700
S2 P2 J4 250
S4 P3 J2 125
S5 P4 J2 325
15. The significance of an SPJ record is that the specified supplier supplies the specified
part to the specified project in the specified quantity (and the combination S#-P#-J#
uniquely identifies such a record).
a) Get full details of all projects in London.
b) Get S# for suppliers who supply project J1.
c) Get all part-color/part-city combinations.
d) Get all S#/P#/J# triples such that all are co-located.
e) Get al S#, P#, J# triples such that they are not all co-located.
f) Get P# for parts supplied by a supplier in London.
g) Get all pairs of cities such that a supplier in the first city supplies to a Project in
the second city.
h) Get J# for projects supplied by at least one supplier not in the same city.
i) Get all pairs of part numbers such that some supplier supplies both the indicated
parts.
j) Get the total quantity of part P1 supplied by S1.
k) For each part supplied to a project, get the P#, J# and corresponding total
quantity.
l) Get P# of parts supplied to some project in an average quantity > 320.
m) Get project names for projects supplied by supplier S1.
n) Get colors of parts supplied by S1.
o) Get J# for projects using at least one part available from supplier S1.
p) Get supplier numbers for suppliers supplying at least one part supplied by at least
one supplier who supplies at least one red part.
q) Get supplier numbers for suppliers with a status lower than that of supplier S1.
r) Get project numbers for projects not supplied with any red part by any London
supplier.
Assignment 16
Objective: To visualize how an inner query works.
“Get supplier numbers for suppliers with a status lower than that of supplier S1”
SELECT S# FROM Suppliers WHERE Status < (SELECT Status FROM Suppliers WHERE
S#=’S1’);
Step 1: The inner query is executed first which selects the status of supplier s1 from suppliers
table.
Step 3: Above query is again executed as a simple query i.e. tuples are selected for those
suppliers whose status is less than 5 and finally there s# is being displayed.
Day 5
Assignment 17
Objective: To visualize how correlated query works.
Step 1: The outermost query is executed first and for the first driver_name in truck relation
NOT EXISTS condition is being verified.
Step 2: Against the first driver_name all the cities are selected and again one by one for each
city shipment destination is being checked with the city_name in the innermost query.
NOTE: This is a correlated query because the innermost query is referring to a column in the
next outer query.
Step 3: If the condition c.destination=b.city_name become false for any of the tuple than
NOT EXISTS returns false and hence that city and henceforth that driver is not being
considered in the final output.
Step 4: Step 1 to step3 will be repeated for all the turck drivers in the truck relation.
Assignment 18
18. Now solve the following set of queries according to given schema:
Write the SQL commands to create a database schema for the following relational schema:
TRUCK_# DRIVER_NAME
100 Jensen
101 Sasi
102 Hrithik
103 Jake Stinson
CITY_NAME POPULATION
London 100000000
Paris 120000000
Rome 200000000
Panama City 1230000000
San Francisco 20000000
Sioux City 5000000000
Manhattan 10000000
Los Angeles 7000
Baltimore 2000
Denver 1000
St. Louis 5000
a) What are the names of customers who have sent packages (shipments) to Sioux City?
b) To what destinations have companies with revenue less than $1 million sent packages?
c) What are the names and populations of cities that have received shipments weighing
over 100 pounds?
d) Who are the customers having over $5 million in annual revenue who have sent
shipments weighing less than 1 pound?
e) Who are the customers having over $5 million in annual revenue who have sent
shipments weighing less than 1 pound or have sent a shipment to San Francisco?
f) Who are the drivers who have delivered shipments for customers with annual revenue
over $20 million to cities with populations over 1 million?
g) List the cities that have received shipments from customers having over $15 million in
annual revenue.
h) List the names of drivers who have delivered shipments weighing over 100 pounds.
i) List the name and annual revenue of customers who have sent shipments weighing
over 100 pounds.
j) List the name and annual revenue of customers whose shipments have been delivered
by truck driver Jensen.
k) List customers who had shipments delivered by every truck. ( use NOT EXISTS)
l) List cities that have received shipments from every customer. ( use NOT EXISTS)
m) List drivers who have delivered shipments to every city. (use NOT EXISTS)
n) Customers who are manufacturers or have sent a package to St. Louis.
o) Cities of population over 1 million which have received a 100-pound package
From customer 311.
p) Trucks driven by Jake Stinson which have never delivered a shipment to Denver.
q) Customers with annual revenue over $10 million which have sent packages under
1 pound to cities with population less than 10,000.
r) Create views for each of the following:
a. Customers with annual revenue under $1 million.
b. Customers with annual revenue between $1 million and $5 million.
c. Customers with annual revenue over $5 million.
Day 6
Assignment 19
Objective: To learn about the exclusive lock.
Background: The different locking mechanisms have been explained to you in the class. For
practice, we will use EMP table (which you have used in Assignments for Day3).
Step 1: Open two instances of SQLPLUS on your machine and login with your Oracle ID in both
the instances.
Assignment 20
Objective: To visualize how deadlock occurs.
Step 1: Open two instances of SQLPLUS (If you have closed the previous ones) on your
machine and login with your Oracle ID in both the instances.