RDBMS – Exercises for Hands on

RDBMS Exercises for Hands-on

© Infosys Technologies Ltd
No. 350, Hebbal Electronics City, Hootagalli Mysore – 571186

Author(s) Authorized by Creation Date Version

Anubhav Pradhan, Umesha Murthy, Hanumesh V.J, Seema Acharya Dr. M.P. Ravindra 1.00b

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

1 of 29

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). Note: All assignments in this document must be completed in the sequence in this document in order to complete the course.

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.
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

2 of 29

RDBMS – Exercises for Hands on

Day 2
Assignment 4
Convert the ER model into a relational Schema

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

3 of 29

RDBMS – Exercises for Hands on

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

4 of 29

RDBMS – Exercises for Hands on

Assignment 5
Let us find out the Primany Key and highest normal form for this relation: R{a,b,c,d,e,} a,e  c, d  e, a,eb, a,ed 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. Relation R is not in BCNF because d determines e and d, although a determinant is not a candidate key.

Assignment 6
Find the primary key and the highest normal form of following relations a. R{a,b,c} a,bc and ca b. R{a,b,c,d} ab, ad and bc c. R{a,b} ab and ba d. R{a,b,c,d,e} a,bc, a,bd and de

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

5 of 29

RDBMS – Exercises for Hands on

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 Blvd, 103 Bobby 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.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

6 of 29

RDBMS – Exercises for Hands on

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: Reach OracleNet8 Assistant via : Start->All programs->oracle-orahome81->networkadministration->net8assistant

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

7 of 29

RDBMS – Exercises for Hands on Step2: You will get an interface like Click on service Naming and click the “+” button to the left

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

8 of 29

RDBMS – Exercises for Hands on Step 3: After clicking + wizard appears Enter Net Service Name as training (If it already exists chose any other name)

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

9 of 29

RDBMS – Exercises for Hands on Step 4: Click the next button Choose TCP/IP as the protocol and click next

Step 5: Enter Host Name given to you and don’t change port number (default is 1521)

Step 6:
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

10 of 29

RDBMS – Exercises for Hands on

Click next Choose (oracle8i) and service name is “training”

Step 7: Test the connection by clicking the test button

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

11 of 29

RDBMS – Exercises for Hands on 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 For example userid is trng180 password is “Infosys”

Once again test the connection after giving username and password
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

12 of 29

RDBMS – Exercises for Hands on

Now configuration is tested Step 8: After that login into SQL plus by following the set of links as given below

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

13 of 29

RDBMS – Exercises for Hands on

Enter your username ex trng180 password is Infosys and host string is training

you will get SQL prompt and start working Summary of this exercise: You have just learnt • Net 8 is the service for communication between oracle client and server • How to configure Net service.
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

14 of 29

RDBMS – Exercises for Hands on

Assignment 10
Solving simple queries Create two tables - EMP & DEPT EMP Column name EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPT Column name DEPTNO DNAME LOC Data for EMP 7369 Smith 7499 Allen 7521 Ward 7566 Jones 7654 Martin 7698 Blake 7782 Clark 7788 Scott 7839 King 7844 Turner 7876 Adams 7900 James 7902 Ford 7934 Miller Data type Number Varchar Char Number Date Number Number Number Data type Number Varchar Varchar Clerk Salesman Salesman Manager Salesman Manager Manager Analyst President Salesman Clerk Clerk Analyst Clerk New York Dallas Chicago Boston Description Employee number Employee name Designation Manager’s Emp. number Date of joining Basic Salary Commission Department Number Description Department number Department name Location of department 7902 7698 7698 7839 7698 7839 7839 7566 7698 7788 7698 7566 7782 17/12/80 20/2/81 22/2/81 2/4/81 28/9/81 1/5/81 9/6/81 9/12/82 17/11/81 8/9/81 12/1/83 3/12/81 4/12/81 23/1/82 800 1600 1250 2975 1250 2850 2450 3000 5000 1500 1100 950 3000 1300 20 30 30 20 30 30 10 20 10 30 20 30 20 10

300 500 1400

0

Data for DEPT table 10 Accounting 20 Research 30 Sales 40 Operations

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

15 of 29

RDBMS – Exercises for Hands on

Assignment 11
Perform the following queries on the EMP and DEPT table: a) List the names of analysts and salesmen. b) List details of employees who have joined before 30 Sep 81. c) List names of employees who are not managers. d) List the names of employees whose employee numbers are 7369, 7521, 7839, 7934, 7788. e) List employees not belonging to department 30, 40, or 10. f) List employee names for those who have joined between 30 June and 31 Dec. ‘81. g) List the different designations in the company. 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. j) List the employees not assigned to any department. k) List the employees who are eligible for commission. l) List employees whose names either start or end with “S”. m) List names of employees whose names have “i” as the second character. n) List the number of employees working with the company. o) List the number of designations available in the EMP table. p) List the total salaries paid to the employees. q) List the maximum, minimum and average salary in the company. r) List the maximum salary paid to a salesman.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

16 of 29

RDBMS – Exercises for Hands on

Assignment 12
Understanding a simple query Objective: To visualize how a simple query works. Problem Statement: Let us try to understand how the following query works: “List employees not belonging to department 30, 40, or 10” The solution is: SELECT ename FROM emp WHERE deptno NOT IN (30, 40, 10); Let us explore how this query is working. 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. Summary of this exercise: You have just learnt • How a simple query works.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

17 of 29

RDBMS – Exercises for Hands on

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” The solution is: 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 HAVING avg(sal) > 1000 Note: When you are using where, group by and having together than the order should be as follows: where, group by and having.

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. Summary of this exercise: You have learnt • Where to use where and having and there differences. • The order of group by, having and where. • The placement of order by.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

18 of 29

RDBMS – Exercises for Hands on

Assignment 14
14. Perform the following queries against EMP and DEPT tables: Note: Use sysdate to get the system’s date. For example : select sysdate from emp;

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

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

19 of 29

RDBMS – Exercises for Hands on

Assignment 15
The following questions pertain to a database with the following tables. Suppliers S# SName Status City S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens Parts P# P1 P2 P3 P4 P5 P6 Projects J# J1 J2 J3 J4 J5 PName Nut Bolt Screw Screw Cam Cog Color Red Green Blue Red Blue Red Weight 12.0 17.0 17.0 14.0 12.0 19.0 City London Paris Rome London Paris London

Jname Montago Cat Box Montago Eagles

City London Paris London Rome Athens

Shipment S# S1 S1 S2 S3 S2 S4 S5

P# P1 P3 P1 P2 P2 P3 P4

J# J1 J3 J1 J3 J4 J2 J2

Qty 350 120 620 700 250 125 325

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

20 of 29

RDBMS – Exercises for Hands on 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.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

21 of 29

RDBMS – Exercises for Hands on

Assignment 16
Objective: To visualize how an inner query works. Problem Statement: Let us consider the following query: “Get supplier numbers for suppliers with a status lower than that of supplier S1” The solution is: SELECT S# FROM Suppliers WHERE Status < (SELECT Status FROM Suppliers WHERE S#=’S1’); Let us explore how this query is working. Step 1: The inner query is executed first which selects the status of supplier s1 from suppliers table. Step 2: Now the query looks like this: SELECT S# FROM Suppliers WHERE Status < 5 (Considering the status of S1 is 5). 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. Summary of this exercise: You have just learnt • Inner query executes first and once only.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

22 of 29

RDBMS – Exercises for Hands on

Day 5
Assignment 17
Objective: To visualize how correlated query works. Background: Let us consider the following query: “List drivers who have delivered shipments to every city” The solution is: SELECT a.driver_name FROM truck a WHERE NOT EXISTS ( SELECT * FROM city b WHERE NOT EXISTS( SELECT * FROM shipment c where c.destination=b.city_name and a.truck# = c.truck#)); Let us explore how this query is working. 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. Summary of this exercise: You have just learnt • Working of NOT EXISTS. • A sample correlated query.

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

23 of 29

RDBMS – Exercises for Hands on

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: CUSTOMER (CUST_ID, CUST_NAME, ANNUAL_REVENUE, CUST_TYPE) CUST_ID must be between 100 and 10,000 ANNUAL_REVENUE defaults to $20,000 CUST_TYPE must be manufacturer, wholesaler, or retailer Data for CUSTOMER table CUST_ID 100 101 102 103 104 311 CUST_NAME Revathi Richa Rishi Rijesh Kalyan Karthik ANNUAL_REVENUE 1000000 1800000 1000000 4000000 4800000 5500000 CUST_TYPE manufacturer wholesaler retailer wholesaler Wholesaler retailer

SHIPMENT (SHIPMENT_#, CUST_ID, WEIGHT, TRUCK_#, DESTINATION, SHIP_DATE) Foreign Key: CUST_ID REFERENCES CUSTOMER, on deletion cascade Foreign Key: TRUCK_# REFERENCES TRUCK, on deletion set to null Foreign Key: DESTINATION REFERENCES CITY, on deletion set to null WEIGHT must be under 1000 and defaults to 10 Data for SHIPMENT table SHIPMENT_# 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
ER/CORP/CRS/DB07/004

CUST_ID 100 101 101 101 101 102 100 104 103 104 102 103 104 104 104 100

WEIGHT 500 100 300 10 20 200 50 500 50 25 200 100 500 200 50 75

TRUCK_# 100 102 103 102 101 102 101 100 103 101 103 101 100 100 103 103

DESTINATION London Paris London Panama City Los Angeles Rome Sioux City Manhattan San Francisco San Francisco London London London London Manhattan Los Angeles

SHIP_DATE Null Null Null 12-Dec-03 Null Null 18-Sep-03 Null Null Null 11-Oct-98 09-Sep-99 18-Jun-88 11-Oct-98 29-May-03 17-Sep-02

Education & Research Department © Infosys Technologies Limited

24 of 29

RDBMS – Exercises for Hands on 116 117 118 119 120 121 122 123 124 125 101 103 103 103 104 100 101 102 311 311 55 45 45 45 45 150 500 250 0.5 100 102 101 100 102 102 102 102 102 102 102 Baltimore Paris Rome Los Angeles London Sioux City Manhattan San Francisco Denver St. Louis 01ul-02 Null Null Null Null Null Null 31-Jul-02 Null Null

TRUCK (TRUCK_#, DRIVER_NAME) Data for TRUCK table TRUCK_# 100 101 102 103 DRIVER_NAME Jensen Sasi Hrithik Jake Stinson

CITY (CITY_NAME, POPULATION) Data for CITY table CITY_NAME London Paris Rome Panama City San Francisco Sioux City Manhattan Los Angeles Baltimore Denver St. Louis POPULATION 100000000 120000000 200000000 1230000000 20000000 5000000000 10000000 7000 2000 1000 5000

Perform the following queries:
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

25 of 29

RDBMS – Exercises for Hands on

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. s) Use these views to answer the following queries: a. Which drivers have taken shipments to Los Angeles for customers with revenue over $5 million? b. What are the populations of cities which have received shipments from customers with revenue between $1 million and $5 million? c. Which drivers have taken shipments to cities for customers with revenue under $1 million, and what are the populations of those cities?

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

26 of 29

RDBMS – Exercises for Hands on

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). Note: In Oracle the default locking is row exclusive lock for Update/Delete/Insert

Step 1: Open two instances of SQLPLUS on your machine and login with your Oracle ID in both the instances. Step 2: Go to first instance. Write the following query: update emp set sal=9000 where empno=7369; Step 3: Go to the second instance. Write the following query: update emp set sal=6000 where empno=7369; Note: The second instance is hanged. The reason is the first transaction has issued the update statement for the empno 7369 and it acquires the exclusive(X) lock. The second transaction is also trying to update the same but because of the X lock acquired by the first transaction it has to wait. Step 4: Go to the first instance and issue COMMIT/ROLLBACK. Step 5: Go to the second instance. You get the SQL prompt. Note: As soon as you Commit/Rollback the transaction in second instance the X lock is released.

Step 6: Issue COMMIT/ROLLBACK in the second instance as well. Summary of this exercise: You have just learnt • Update/Delete/Insert DML statement acquire a row exclusive lock

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

27 of 29

RDBMS – Exercises for Hands on • The X lock is released only at the end of transaction which is marked by Commit/Rollback.

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. Step 2: Go to first instance. Write the following query: update emp set sal=7000 where empno=7900; Step 3: Go to the second instance. Write the following query: update emp set sal=6000 where empno=7902; Step 4: Go to first instance. Write the following query: update emp set sal=7000 where empno=7902; Note: The first instance is hanged. What is the reason? Do you remember the last hands on?

Step 5: Go to the second instance. Write the following query: update emp set sal=6000 where empno=7900; Note: This situation is deadlock. Oracle is smart enough and detect the same and it rollbacks the statement of Step 4. Check the Oracle message: ORA-00060: deadlock detected while waiting for resource

Step 6: Issue COMMIT/ROLLBACK in both the instances. Summary of this exercise: You have just learnt • How to visualize the deadlock.
ER/CORP/CRS/DB07/004 Education & Research Department © Infosys Technologies Limited

28 of 29

RDBMS – Exercises for Hands on • Oracle intelligently determines the deadlock situation

ER/CORP/CRS/DB07/004

Education & Research Department © Infosys Technologies Limited

29 of 29

Sign up to vote on this title
UsefulNot useful