DBMS Lab Manual-Student Copy[1]
DBMS Lab Manual-Student Copy[1]
MANAGEMENT SYSTEM
LAB GUIDE
KCS 551
Lab 1 – E R Diagram
Objective:To understand a given case study, notations used for ER Diagram and design the
ER Diagram using a tool of following scenarios.
Case Study 2:
An organization having a set of employees to execute a set of projects. Each employee may
be working on more than one project, each project is managed by a manager and a
manager is also one of the employees.
Case Study 3:
Preparation of time table of an Engineering College, catering for a number of Sections
(Year/Branch/Section),a number of courses, a number of faculty members teaching the
courses and a number of class rooms (ignorelabs).
Case Study 4:
Construct an E-R diagram for a car-insurance company whose customers own one or more
cars each. Each car has associated with it zero to any number of recorded accidents.
Case Study 5:
Consider a university database for the scheduling of classrooms for final exams. This database
could be modelled as the single entity set exam, with attributes course-name, section-
number, room-number, and time. Alternatively, one or more additional entity sets could be
defined, along with relationship sets to replace some of the attributes of the exam entity set,
as
• Course with attributes name, department, and c-number
• Section with attributes s-number and enrolment, and dependent as a weak entity
set on course
• Room with attributes r-number, capacity, and building
Draw the ER diagram of airline Reservation Systems with the following details:-
Case Study 2:
Draw the ER diagram of a College Management System with the following details:-
a. A college contains many departments
b. Each department can offer any number of courses
c. Many instructors can work in a department
d. An instructor can work only in one department
e. For each department there is a Head
f. An instructor can be head of only one department
g. Each instructor can take any number of courses
h. A course can be taken by only one instructor
i. A student can enrol for any number of courses
j. Each course can have any number of students
Case Study 3:
A university registrar’s office maintains data about the following entities: (a) courses,
including number, title, credits, syllabus, and prerequisites; (b) course offerings, including
course number, year, semester, section number, instructor(s), timings, and classroom; (c)
students, including student-id, name, and program; and (d) instructors, including
identification number, name, department, and title. Further, the enrollment of students in
courses and grades awarded to students in each course they are enrolled for must be
appropriately modeled. Construct an E-R diagram forthe registrar’s office. Document all
assumptions that you make about the mapping constraints. and instructor. The entity set
course-offering is a weak entity set dependent on course.
The assumptions made are:
a. A class meets only at one particular place and time. This E-R diagram cannot model a
class meeting at different places at different times.
b. There is no guarantee that the database does not have two classes meeting at the same
place and time.
Case Study 4:
Draw the Entity- Relationship Diagram (ERD) for the following scenario: A salesperson may
manage many other salespeople. A salesperson is managed by only one salespeople. A
salesperson can be an agent for many customers. A customer is managed by one salespeople.
A customer can place many orders. An order can be placed by one customer. An order lists
many inventory items. An inventory item may be listed on many orders. An inventory item
is assembled from many parts. A part may be assembled into many inventory items. Many
employees assemble an inventory item from many parts. A supplier supplies many parts. A
part may be supplied by many suppliers.
Case Study 5:
Congratulations! You have gotten a job planning databases for the European Union. Your first
on job assignment is to help the various countries maintain information about their
inhabitants. Your model should capture the following information:
• In each country, there are provinces, which contain towns. There cannot be two
provinces with the same name in a single country. Similarly, there cannot be two towns
with the same name in a single province.
• People live in towns. Men and women work in a town. Children learn in a school in a town.
• A person can be a man, a woman, or a child, and has a first-name, last-name, id, and
birthday. Children are any people under the age of 18.
• A man can be married to a woman (polygamy is not allowed, i.e., one man can be married
only to one woman). Although the Pope strongly disapproves, divorce, and subsequent
remarriage, is possible
For each marriage, store the date of the marriage and information about who are the children
of the married couple. You should assume that the parents of a child were married at the time
of his birth. Draw an entity relationship diagram to model the information described above.
Remember to put edge constraints and participation constraints where needed. Underline
the key attributes of each entity in the diagram. If you use the ISA relationship, state any
covering and overlap constraints that hold. Make any necessary and logical assumptions.
State any such assumptions clearly. If there are any constraints in the problem that could not
be expressed in the diagram, state these clearly.
Case Study 6:
Suppose you are given the following requirements for a simple database for the National
Hockey League (NHL):
the NHL has many teams,
each team has a name, a city, a coach, a captain, and a set of players,
each player belongs to only one team,
each player has a name, a position (such as left wing or goalie), a skill level, and
a set of injury records,
a team captain is also a player,
a game is played between two teams (referred to as host_team and guest_team)
and has a date (such as May 11th, 1999) and a score (such as 4 to 2).
Construct a clean and concise ER diagram. List your assumptions and clearly indicate the
cardinality mappings as well as any role indicators in your ER diagram.
Case Study 7:
Assume we have the following application that models soccer teams, the games they play,
and the players in each team. In the design, we want to capture the following:
We have a set of teams, each team has an ID (unique identifier), name, main
stadium, and to which city this team belongs.
Each team has many players, and each player belongs to one team. Each player has
a number (unique identifier), name, DoB, start year, and shirt number that he uses.
Teams play matches, in each match there is a host team and a guest team.
The match takes place in the stadium of the host team.
For each match we need to keep track of the following:
o The date on which the game is played
o The final result of the match
o The players participated in the match. For each player, how many goals he
scored, whether or not he took yellow card, and whether or not he took
red card.
o During the match, one player may substitute another player. We want to
capture this substitution and the time at which it took place.
Each match has exactly three referees. For each referee we have an ID (unique
identifier), name, DoB, years of experience. One referee is the main referee and the
other two are assistant referee.
Case Study 8:
Professors can teach the same course in several semesters, and each offering must
be recorded.
Professors can teach the same course in several semesters, and only the most recent
such offering needs to be recorded (assume this condition applies in all subsequent
questions)
Every professor must teach some course.
Every professor teaches exactly one course.
Every professor teaches exactly one course and every course must be taught
by some professor.
Now suppose that certain courses can be taught by the team of professors
jointly, but it is possible that no one professor in a team can teach the course.
Case Study 9:
Consider the following information about the university data base
Design and draw an ER diagram for the following collection of data. Use only the basic
ER model here, i.e., entities relationships and attributes. Be sure to indicate any key
and participation constraints.
Create an ERD for a car dealership. The dealership sells both new and used cars, and it
operates a service facility. Base your design on the following business rules:
A salesperson may sell many cars, but each car is sold by only one salesperson.
A customer may buy many cars, but each car is bought by only one customer.
A salesperson writes a single invoice for each car he or she sells.
A customer gets an invoice for each car he or she buys.
A customer may come in just to have his or her car serviced; that is, a customer need not buy
a car to be classified as a customer.
When a customer takes one or more cars in for repair or service, one service ticket is
written for each car.
The car dealership maintains a service history for each of the cars serviced. The
service records are referenced by the car’s serial number.
A car brought in for service can be worked on by many mechanics, and each mechanic may
work on many cars.
A car that is serviced may or may not need parts (e.g., adjusting a carburetor or cleaning a
fuel injector nozzle does not require providing new parts).
Case Study 15:
The following is a description of some data requirements for a chain of pharmacies. Draw
the appropriate entity-relationship (E-R) diagram. Clearly show all cardinality constraints,
cardinality limits, and existence dependencies.
(a) A pharmaceutical company manufactures one or more drugs, and each drug is
manufactured and marketed by exactly one pharmaceutical company.
(b) Drugs are sold in pharmacies. Each pharmacy has a unique identification. Every pharmacy
sells one or more drugs, but some pharmacies do not sell every drug.
(c) Drug sales must be recorded by prescription, which are kept as a record by the pharmacy.
A prescription clearly identifies the drug, physician, and patient, as well as the date it is filled.
(d) Doctors prescribe drugs for patients. A doctor can prescribe one or more drugs for a
patient and a patient can get one or more prescriptions, but a prescription is written by
only one doctor.
(e) Pharmaceutical companies may have long-term contracts with pharmacies and a
pharmacy can contract with zero, one, or more pharmaceutical companies. Each contract is
uniquely identified by a contract number.
For each reality show, its name, genre, basic_info and participants name. Any reality
show has at least two or more participants.
For each producer, the company name, company country. A show is produced by
exactly one producer. And one producer produces exactly one show.
For each television, its name, start year, head office. A television may
broadcasts multiple shows. Each show is broadcasted by exactly one television.
For each user, his/her username, password, and age. A user may rate multiple
shows, and a show may be rated by multiple users. Each rating has a score of 0 to
10.
Company organized into DEPARTMENT. Each department has unique name and a particular
employee who manages the department. Start date for the manager is recorded. Department
may have several locations.
A department controls a number of PROJECT. Projects have a unique name, number and
a single location.
Company’s EMPLOYEE name, ssno, address, salary, sex and birth date are recorded. An
employee is assigned to one department, but may work for several projects (not necessarily
controlled by her dept). Number of hours/week an employee works on each project is
recorded; the immediate supervisor for the employee.
Employee’s DEPENDENT are tracked for health insurance purposes (dependent name,
birthdate, relationship to employee).
• For each MEMBER we keep track of the unique MemdID, a well as Name, Zip, and the
Date the membership was paid
• For each MEMBERSHIP type we keep track of the unique Mid, as well as MName and Price
• For each PASS CATEGORY we keep track of the unique PassCatID, as well as PCName and
Price
• For each ONE DAY PASS we keep track of the unique PassID and Date
• For each MERCHANDISE item we keep track of the unique MrchID, as well as Name and
Price
• For each sale TRANSACTION we keep track of the unique Tid and Date
• Each member pays for exactly one membership type; each membership type has at least
one member but can have many members
• Each member can buy many day passes but does not have to buy any, each day pass was
bought by exactly one member
• Each day pass belongs to exactly one pass category; a pass category can have many
individual day passes issued for it but does not have to have any
• Each sale transaction involves exactly one member; each member can be involved in many
sale transactions but does not have to be involved in any
• Each merchandise item is sold via at least one sale transaction but it can be sold via many
sale transactions; each sale transaction involves at least one merchandise item but can involve
many merchandise items
• Every time a merchandise item is sold via a sale transaction, we keep track of the quantity
(how many instances of that particular merchandise item were sold via that particular sale
transaction)
Your task is to create and ER Diagram based on these requirements.
The instructor will use the system to update the marks of the student (project, assignment,
internal test marks and the semester end examination marks). The instructor will also mark
the attendance of the students through the system.
The student can view his / her marks and attendance through the system.
In addition to the above, the system also keeps track of the residential status of the
student. The student may be a hostelite or a day scholar. If he is a hostelite, the system will
maintain his / her hostels name, room number and the fees pertaining to the same.
Lab 2 – DDL Commands & Constraints
Objective: At the end of the assignments, participants will be able to
understand basic DDL, Create table with constraints, Alter, Truncate, Drop
and Rename
5. Create a new table using a subquery. Name the new table your first name -- e.g.,
gaurav_table. Using a subquery, copy grad_candidates into gaurav_table.
8. Create a new column in the smith_table table called start_date. Use the TIMESTAMP
WITH LOCAL TIME ZONE as the datatype.
11. Truncate the gaurav_table table. Then do a SELECT * statement. Are the columns
still there?
12. What the distinction is between TRUNCATE and DROP for tables?
13. List the changes that can and cannot be made to a column.
Exercise -2:
Exercise -1:
1. Insert 5 rows into the tables for the database created of your case study.
Exercise -2:
1. Create the tables mentioned below and insert the rows as shown. Please assume
the datatype and constraints as required.
Use the default schema of EMP Table & DEPT Table of the database and implement the listed
queries:
Exercise -1:
To solve the below mentioned queries (1-10), please upload the SMS database script provided by
your faculty.
1. Find the names of faculty who are working in the same department in which
Gagan Kumar Verma is working.
2. Find the status of research project/s in which Ram Mohan Prasad is working.
3. Find the names of students who have registered for mini-project.
4. Find the names of faculty who are working in more than two research projects.
5. Find the number of students registered in each course of a department.
6. Create a report that displays the faculty id, last name, and salary of all faculty
who earn more than the average salary. Sort the results in ascending order of
salary.
7. Write a query that displays the faculty id and last name of all employees who
are working in a department with any employee whose last name includes
letter t.
8. Find the first name and salary of all faculty who are reporting to head of
the department Ayush Giri.
9. Find the names of students who are taking courses of faculty Mr. Ram
Mohan Prasad.
10. Find the department in which a faculty has joined most recently.
Exercise -2:
To solve the below mentioned queries (1-10), please create the below mentioned database.
account: branch:
1. Find customer ids of those customers who are borrower from the banks and who
appear in the list of account holders.
2. Find those customer names who are borrower.
3. Find the name of the customers who have a loan from the bank, but do not have an
account at the bank. (Hint: use NOT IN)
4. Get the Customer Id and name of those customers who have both account and loan
from the bank.
5. Get Branch Name of the branch having highest average balance amongst all branches.
6. Find the names of all branches that have assets greater than those of at least one
branch located in NOIDA. Use some
7. Find the names of all branches that have assets greater than that of each branch
located in NOIDA. (Use All)
8. Get the names of the customers who have account in each branch located in Noida.
Lab 6 – Joins using multiple tables
To solve the below mentioned queries, please upload the employee_department script provided
by your faculty.
To understand the employees/departments database, please refer below mentioned table design
1. Display the employee’s last name and employee number along with the manager’s last
name and manager number. Label the columns: Employee, Emp#, Manager, and Mgr#,
respectively.
2. Modify question 1 to display all employees and their managers, even if the employee
does not have a manager. Order the list alphabetically by the last name of the employee.
3. Display the names and hire dates for all employees who were hired before their
managers, along with their managers’ names and hire dates. Label the columns Employee,
Emp Hired, Manager and Mgr Hired, respectively.
Lab 7 – Restricting Data Using Group By Clause
Exercise -1:
Write the answers of following questions in practical file
Exercise -2:
Solve the following queries on the basis of SMS database.
1. Write a query in SQL to find the number of faculty in each department along with the
department id.
2. Write a query in SQL to find the sum of the allotment of salary amount of all
departments.
3. Write a SQL query to display the average salary amount in each department along with
their id.
4. Write a SQL query to find the number of faculty getting salary more than or equal to
Rs.60000.
5. Write a SQL query to find the number of faculty teaching a course and number of
students registered in that course.
6. Write a SQL query to find the number of faculty of each department who are
working in any research project.
7. Write a SQL query to find the first name, faculty id and department of all faculty
who are working in more than two research projects.
8. Write a SQL query to display the name and id of faculty who has maximum
experience.
9. Write a SQL query to find the first name and department of all faculty who have
completed any research project.
10. Write a SQL query to find the student names and their departments who have
registered in mini projects of Artificial Intelligence domain.
Bonus Exercise – 3:
Solve the following queries on the basis of schema given.
1. Using the three separate words “Oracle,” “Internet,” and “Academy,” use one command
toproduce thefollowingoutput:
2. Usethestring“Oracle InternetAcademy”toproducethefollowingoutput:
4. What’sthepositionof“I”in“OracleInternetAcademy”?
5. Startingwiththestring“OracleInternetAcademy”,padthe stringtocreate
****Oracle****Internet****Academy****
6. DisplayOracledatabaseemployeelast_nameandsalaryforemployee_idsbetween100and
102.Includeathirdcolumnthatdivideseachsalaryby1.55androundstheresulttotwodecimalplaces.
7. Display employee last_name and salary for those employees who work in department
80.Giveeachof themaraiseof5.333%andtruncatethe resultto twodecimalplaces.
Lab 9 – Implementation of Views & Indexing
Objective -Students will be able to implement the concept of View and Index
Create a table Employee
1. Insert 5 records
2. Create a view having Ename and Ecity
3. Update the view& set the Ecity to ‘Delhi’ where Ename is ‘John’
4. Insert a row in the view.
5. Update the view and increase the salary of employees in IT dept
6. Create view having details of employee working in deptno=10
7. Create a view having grouping functions like max(sal)and min(sal)
8. Update the above view and set the max salary to 90000
9. Delete the view created
Theory
Procedures are usually used to perform any specific task and functionsare
used to compute a value.
PROCEDURE:
The basic syntax for the creating procedure is:
body;
The declarative section is located between the IS /AS keyword andBEGIN keyword.
section )
BEGIN
(Executable section )
EXCEPTION
END Procedure_name;
To execute the procedure we have to write a block of
statement: Begin
Procedurename(data);
End;
FUNCTION:
(local declaration)
BEGIN
(Executable section )
EXCEPTION
). END function_name;
A function has two parts, namely function specification and function body.
The function specification begins with the keyword function and end with
return clause. The function bodies begins with the keyword is/as and end
with keyword end
THEORY
1. Cursor
2. Triggers
A. Explicit cursor
B. Implicit cursor
A. Explicit cursor:
An explicit cursor is one in which cursor name is explicitly assigned to select statement.
An implicit cursor is used for all other sql statements.Processing of an explicit cursor
involves four steps. Processing of an implicit cursor is taken care by PL/SQL .the
declaration of the cursor is done in the declarative part of the block.
Explicit cursor:
The set of rows return by query can contain zero or multiple rows depending upon the
query defined. The rows are called active set. Thecursor will point to the current row
in the active set.
After declaring a cursor, we can use the following commands to controlthe cursor.
1. Open
2. Fetch
3. Close
Triggers:
Types of triggers
A. before
B. after
C. for each row
D. for each statement.
Before/After options:
The before/after options can be used to specify when the trigger body should be fired
with respect to the triggering statement. If the user include a before option, then Oracle
fires the triggers before executing the triggering statement. On the other hand if AFTER is
used then , oracle files the trigger after executing the triggering statement.
For each row/ statement option included in the ‘create trigger ‘ syntax specifies that the
triggers fires once per row . By defaults, database triggers a database triggers fires for
each statement .
begin
select orderno into orno from
order_detail where qty_ord
<qyt_deld;
In this trigger before inserting order table it check the condition orno(order number)
equal to 001
and condition is satisfied then the value updated else error is occurred.