Lab Manual It405dbms
Lab Manual It405dbms
SCIENCE
& TECHNOLOGY BHOPAL
DEPARTMENT OF INFORMATION
TECHNOLOGY
LAB MANUAL
DBMS LAB
(IT-405)
Entity-Relationship Model
The entity-relationship (E-R) data model is based on a perception of a real world that
consists of a collection of basic objects, called entities, and of relationships among these
objects. An entity is a “thing” or “object” in the real world that is distinguishable from
other objects. For example, each person is an entity, and bank accounts can be considered
as entities.
Entities are described in a database by a set of attributes. For example, the attributes
account-number and balance may describe one particular account in a bank, and they
form attributes of the account entity set. Similarly, attributes customer-name, customer-
street address and customer-city may describe a customer entity. An extra attribute
customer-id is used to uniquely identify customers (since it may be possible to have two
customers with the same name, street address, and city). A unique customer identifier
must be assigned to each customer. In the United States, many enterprises use the social-
security number of a person (a unique number the U.S. government assigns to every
person in the United States) as a customer identifier.
Exercise:
Consider the following information about a university database:
1. Professors have an SSN, a name, an age, a rank, and a research specialty.
2. Projects have a project number, a sponsor name (e.g., NSF), a starting date, an
ending date, and a budget.
3. Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S.
or Ph.D.).
4. Each project is managed by one professor (known as the project’s principal
investigator).
5. Each project is worked on by one or more professors (known as the project’s co-
investigators).
6. Professors can manage and/or work on multiple projects.
7. Each project is worked on by one or more graduate students (known as the
project’s research assistants).
8. When graduate students work on a project, a professor must supervise their work
on the project. Graduate students can work on multiple projects, in which case
they will have a (potentially different) supervisor for each one.
9. Departments have a department number, a department name, and a main office.
10. Departments have a professor (known as the chairman) who runs the department.
11. Professor work in one or more departments and for each department that they
work in, a time percentage is associated with their job.
12. Graduate students have one major department in which they are working on their
degree.
13. Each graduate student has another, more senior graduate student (known as a
student advisor) who advises him or her on what courses to take.
Design and draw an ER diagram that captures the information about the university.
Use only the basic ER model here; that is, entities, relationships, and attributes.
Be sure to indicate any key and participation constraints.
Solution:
Experiment # 2
A superkey is a set of one or more attributes that, taken collectively, allow us to identify
uniquely an entity in the entity set. For example, the customer-id attribute of the entity set
customer is sufficient to distinguish one customer entity from another. Thus, customer-id
is a superkey. Similarly, the combination of customer-name and customer-id is a
superkey for the entity set customer. The customer-name attribute of customer is not a
superkey, because several people might have the same name. The concept of a superkey
is not sufficient for our purposes, since, as we saw, a superkey may contain extraneous
attributes. If K is a superkey, then so is any superset of K. We are often interested in
superkeys for which no proper subset is a superkey. Such minimal superkeys are called
candidate keys.
It is possible that several distinct sets of attributes could serve as a candidate key.
Suppose that a combination of customer-name and customer-street is sufficient to
distinguish among members of the customer entity set. Then, both {customer-id} and
{customer-name, customer-street} are candidate keys. Although the attributes customerid
and customer-name together can distinguish customer entities, their combination does not
form a candidate key, since the attribute customer-id alone is a candidate key.
We shall use the term primary key to denote a candidate key that is chosen by the
database designer as the principal means of identifying entities within an entity set. A key
(primary, candidate, and super) is a property of the entity set, rather than of the individual
entities. Any two individual entities in the set are prohibited from having the same value
on the key attributes at the same time. The designation of a key represents a constraint in
the real-world enterprise being modeled.
A foreign key is a key used to link two tables together. This is sometimes called a
referencing key. Foreign Key is a column or a combination of columns whose values
match a Primary Key in a different table. The relationship between two tables matches
the Primary Key in one of the tables with a Foreign Key in the second table.
Exercise:
Suppose that we have a ternary relationship R between entity sets A, B, and C such that
A has a key constraint and total participation and B has a key constraint; these are the
only constraints. A has attributes a1 and a2, with a1 being the key; B and C are similar. R
has no descriptive attributes. Write SQL statements that create tables corresponding to
this information so as to capture as many of the constraints as possible.
Solution:
The following SQL statements create the corresponding relations.
The first SQL statement folds the relationship R into table A and thereby guarantees the
participation constraint.
Experiment # 3
Solution:
Observe that we would need check constraints or assertions in SQL to enforce the rule
that Professors work in at least one department.
Note that the Major table is not necessary since each Graduate has only one major
and so this can be an attribute in the Graduates table.
Observe that we cannot enforce the participation constraint for Projects in the Work In
table without check constraints or assertions in SQL.
Note that we do not need an explicit table for the Work Proj relation since every time a
Graduate works on a Project, he or she must have a Supervisor.
Experiment # 4
Objective: To learn different clauses used to insert information using INSERT INTO
statement.
Problem statement: Insert at least three tuples in each table of the university database.
The syntax for the INSERT statement when inserting a single record using the VALUES keyword is:
Or the syntax for the Oracle INSERT statement when inserting multiple records using a SELECT statement is:
Parameters or Arguments
table is the table to insert the records into.
expression1, expression2 are the values to assign to the columns in the table. So column1 would be assigned the value
of expression1, column2 would be assigned the value of expression2, and so on.
source_table is the source table when inserting data from another table.
conditions are conditions that must be met for the records to be inserted.
Experiment # 5
Problem statement: Find the information given below for the university database.
The meaning of these relations is straightforward; for example, Enrolled has one record
per student-class pair such that the student is enrolled in the class.
Write the following queries in SQL. No duplicates should be printed in any of the
answers.
1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by I.
Teach.
2. Find the age of the oldest student who is either a History major or enrolled in a course
taught by I. Teach.
3. Find the names of all classes that either meet in room R128 or have five or more
students enrolled.
4. Find the names of all students who are enrolled in two classes that meet at the same
time.
5. Find the names of faculty members who teach in every room in which some class is
taught.
6. Find the names of faculty members for whom the combined enrollment of the courses
that they teach is less than five.
7. For each level, print the level and the average age of students for that level.
8. For all levels except JR, print the level and the average age of students for that level.
9. For each faculty member that has taught classes only in room R128, print the faculty
member’s name and the total number of classes she or he has taught.
10. Find the names of students enrolled in the maximum number of classes.
11. Find the names of students not enrolled in any class.
12. For each age value that appears in Students, find the level value that appears most
often. For example, if there are more FR level students aged 18 than SR, JR, or SO
students aged 18, you should print the pair (18, FR).
2. SELECT MAX(S.age)
FROM Student S
WHERE (S.major = ‘History’)
OR S.snum IN (SELECT E.snum
FROM Class C, Enrolled E, Faculty F
WHERE E.cname = C.name AND C.fid = F.fid
AND F.fname = ‘I.Teach’ )
3. SELECT C.name
FROM Class C
WHERE C.room = ‘R128’
OR C.name IN (SELECT E.cname
FROM Enrolled E
GROUP BY E.cname
HAVING COUNT (*) >= 5)
Problem statement: Find the information given below for the airline database.
Note that the Employees relation describes pilots and other kinds of employees as well;
every pilot is certified for some aircraft, and only pilots are certified to fly. Write each
of the following queries in SQL.
1. Find the names of aircraft such that all pilots certified to operate them have salaries
more than 80,000.
2. For each pilot who is certified for more than three aircraft, find the eid and the
maximum cruising range of the aircraft for which she or he is certified.
3. Find the names of pilots whose salary is less than the price of the cheapest route from
Los Angeles to Honolulu.
4. For all aircraft with cruising range over 1000 miles, find the name of the aircraft and
the average salary of all pilots certified for this aircraft.
5. Find the names of pilots certified for some Boeing aircraft.
6. Find the aids of all aircraft that can be used on routes from Los Angeles to
Chicago.
7. Identify the routes that can be piloted by every pilot who makes more than $100,000.
8. Print the enames of pilots who can operate planes with cruisingrange greater than
3000 miles but are not certified on any Boeing aircraft.
9. A customer wants to travel from Madison to New York with no more than two changes
of flight. List the choice of departure times from Madison if the customer wants to arrive
in New York by 6 p.m.
10. Compute the difference between the average salary of a pilot and the average salary
of all employees (including pilots).
11. Print the name and salary of every non pilot whose salary is more than the average
salary for pilots.
12. Print the names of employees who are certified only on aircrafts with cruising range
longer than 1000 miles.
13. Print the names of employees who are certified only on aircrafts with cruising range
longer than 1000 miles, but on at least two such aircrafts.
14. Print the names of employees who are certified only on aircrafts with cruising range
longer than 1000 miles and who are certified on some Boeing aircraft.
Answer: The answers are given below:
4. Observe that aid is the key for Aircraft, but the question asks for aircraft names; we
deal with this complication by using an intermediate relation Temp:
6. SELECT A.aid
FROM Aircraft A
WHERE A.cruisingrange > ( SELECT MIN (F.distance)
FROM Flights F
WHERE F.from = ‘Los Angeles’ AND F.to = ‘Chicago’ )
9. SELECT F.departs
FROM Flights F
WHERE F.flno IN ( ( SELECT F0.flno
FROM Flights F0
WHERE F0.from = ‘Madison’ AND F0.to = ‘New York’
AND F0.arrives < ‘18:00’ )
UNION
( SELECT F0.flno
FROM Flights F0, Flights F1
WHERE F0.from = ‘Madison’ AND F0.to <> ‘New York’
AND F0.to = F1.from AND F1.to = ‘New York’
AND F1.departs > F0.arrives
AND F1.arrives < ‘18:00’ )
UNION
( SELECT F0.flno
FROM Flights F0, Flights F1, Flights F2
WHERE F0.from = ‘Madison’
AND F0.to = F1.from
AND F1.to = F2.from
AND F2.to = ‘New York’
AND F0.to <> ‘New York’
AND F1.to <> ‘New York’
AND F1.departs > F0.arrives
AND F2.departs > F1.arrives
AND F2.arrives < ‘18:00’ ))
Problem statement: Find the information given below for the insurance database.
Exercise:
Consider the insurance database, where the primary keys are underlined.
person (driver-id, name, address)
car (license, model, year)
accident (report-number, date, location)
owns (driver-id, license)
participated (driver-id, car, report-number, damage-amount)
Construct the following SQL queries for this relational database.
a. Find the total number of people who owned cars that were involved in accidents in
1989.
b. Find the number of accidents in which the cars belonging to “John Smith” were
involved.
c. Add a new accident to the database; assume any values for required attributes.
d. Delete the Mazda belonging to “John Smith”.
e. Update the damage amount for the car with license number “AABB2000” in the
accident with report number “AR2197” to $3000.
Answer: Note: The participated relation relates drivers, cars, and accidents.
a. Find the total number of people who owned cars that were involved in accidents in
1989.
Note: this is not the same as the total number of accidents in 1989. We must count people
with several accidents only once.
select count (distinct name)
from accident, participated, person
where accident.report-number = participated.report-number
and participated.driver-id = person.driver-id
and date between date ’1989-00-00’ and date ’1989-12-31’
b. Find the number of accidents in which the cars belonging to “John Smith” were
involved.
select count (distinct *)
from accident
where exists
(select *
from participated, person
where participated.driver-id = person.driver-id
and person.name = ’John Smith’
and accident.report-number = participated.report-number)
c. Add a new accident to the database; assume any values for required attributes. assume
“Jones” owns one Toyota. First we must find the license of the given car. Then the
participated and accident relations must be updated in order to both record the accident
and tie it to the given car. We assume values “Berkeley” for location, ’2001-09-01’ for
date and date, 4007 for reportnumber and 3000 for damage amount.
insert into accident
values (4007, ’2001-09-01’, ’Berkeley’)
Since model is not a key of the car relation, we can either assume that only one of John
Smith’s cars is a Mazda, or delete all of John Smith’s Mazdas (the query is the same).
Again assume name is a key for person.
delete car
where model = ’Mazda’ and license in
(select license
from person p, owns o
where p.name = ’John Smith’ and p.driver-id = o.driver-id)
Note: The owns, accident and participated records associated with the Mazda still exist.
e. Update the damage amount for the car with license number “AABB2000” in the
accident with report number “AR2197” to $3000.
update participated
set damage-amount = 3000
where report-number = “AR2197” and driver-id in
(select driver-id
from owns
where license = “AABB2000”)
Experiment # 8
Objective: To learn different clauses used to find information in a SELECT statement.
Problem statement: Find the information given below for the employee database.
Exercise:
Consider the employee database, where the primary keys are underlined.
employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Answer:
a. Find the names of all employees who work for First Bank Corporation.
select employee-name
from works
where company-name = ’First Bank Corporation’
b. Find the names and cities of residence of all employees who work for First Bank
Corporation.
select e.employee-name, city
from employee e, works w
where w.company-name = ’First Bank Corporation’ and
w.employee-name = e.employee-name
c. Find the names, street address, and cities of residence of all employees who work for
First Bank Corporation and earn more than $10,000.
If people may work for several companies, the following solution will only list those who
earn more than $10,000 per annum from “First Bank Corporation” alone.
select *
from employee
where employee-name in
(select employee-name
from works
where company-name = ’First Bank Corporation’ and salary ¿ 10000)
As in the solution to the previous query, we can use a join to solve this one also.
d. Find all employees in the database who live in the same cities as the companies for
which they work.
select e.employee-name
from employee e, works w, company c
where e.employee-name = w.employee-name and e.city = c.city
and w.company -name = c.company -name
e. Find all employees in the database who live in the same cities and on the same streets
as do their managers.
select P.employee-name
from employee P, employee R, manages M
where P.employee-name = M.employee-name and
M.manager-name = R.employee-name and
P.street = R.street and P.city = R.city
f. Find all employees in the database who do not work for First Bank Corporation.
The following solution assumes that all people work for exactly one company.
select employee-name
from works
where company-name = ’First Bank Corporation’
If one allows people to appear in the database (e.g. in employee) but not appear in works,
or if people may have jobs with more than one company, the solution is slightly more
complicated.
select employee-name
from employee
where employee-name not in
(select employee-name
from works
where company-name = ’First Bank Corporation’)
g. Find all employees in the database who earn more than every employee of
Small Bank Corporation.
The following solution assumes that all people work for at most one company
select employee-name
from works
where salary > all
(select salary
from works
where company-name = ’Small Bank Corporation’)
If people may work for several companies and we wish to consider the total earnings of
each person, the problem is more complex. It can be solved by using a nested subquery,
but we illustrate below how to solve it using the with clause.
with emp-total-salary as
(select employee-name, sum(salary) as total-salary
from works
group by employee-name
)
select employee-name
from emp-total-salary
where total-salary > all
(select total-salary
from emp-total-salary, works
where works.company-name = ’Small Bank Corporation’ and
emp-total-salary.employee-name = works.employee-name
)
h. Assume that the companies may be located in several cities. Find all companies located
in every city in which Small Bank Corporation is located.
The simplest solution uses the contains comparison which was included in the original
System R Sequel language but is not present in the subsequent SQL versions.
select T.company-name
from company T
where (select R.city
from company R
where R.company-name = T.company-name)
contains
(select S.city
from company S
where S.company-name = ’Small Bank Corporation’)
select S.company-name
from company S
where not exists ((select city
from company
where company-name = ’Small Bank Corporation’)
except
(select city
from company T
where S.company-name = T.company-name))
i. Find all employees who earn more than the average salary of all employees of their
company.
The following solution assumes that all people work for at most one company.
select employee-name
from works T
where salary > (select avg (salary)
from works S
where T.company-name = S.company-name)
j. Find the company that has the most employees.
select company-name
from works
group by company-name
having count (distinct employee-name) >= all
(select count (distinct employee-name)
from works
group by company-name)
k. Find the company that has the smallest payroll.
select company-name
from works
group by company-name
having sum (salary) <= all (select sum (salary)
from works
group by company-name)
l. Find those companies whose employees earn a higher salary, on average, than the
average salary at First Bank Corporation.
select company-name
from works
group by company-name
having avg (salary) > (select avg (salary)
from works
where company-name = ’First Bank Corporation’)
Experiment # 9
Objective: To learn different clauses used to update information.
Problem statement: Modify the information given below for the employee database.
Exercise:
update works T
set T.salary = T.salary * 1.1
where T.employee-name in (select manager-name
from manages)
and T.salary * 1.1 <= 100000
and T.company-name = ’First Bank Corporation’
SQL-92 provides a case operation (see Exercise 4.11), using a more concise solution is:-
update works T
set T.salary = T.salary ∗
(case
when (T.salary ∗ 1.1 > 100000) then 1.03
else 1.1
)
where T.employee-name in (select manager-name
from manages) and
T.company-name = ’First Bank Corporation’
e. Delete all tuples in the works relation for employees of Small Bank Corporation.
delete works
where company-name = ’Small Bank Corporation’
Experiment # 10
Objective: To learn constraints on database updation.
Exercise:
Write check conditions for the employee database schema to ensure that:
a. Every employee works for a company located in the same city as the city in which the
employee lives.
b. No employee earns a salary higher than that of his manager.
Answer:
a. check condition for the works table:-
check((employee-name, company-name) in
(select e.employee-name, c.company-name
from employee e, company c
where e.city = c.city
)
)
b. check condition for the works table:-
check(
salary < all
(select manager-salary
from (select manager-name, manages.employee-name as emp-name,
salary as manager-salary
from works, manages
where works.employee-name = manages.manager-name)
where employee-name = emp-name
)
)