Consider the relational database where the primary keys are
underlined. Give an expression in the relational algebra to express each of
the following queries:
employee (person-name,
street, city)
works (person-name,
company-name, salary)
company (company-name, city)
manages (person-name,
manager-name)
a.
Find the names of all employees who work for First Bank Corporation. (‘14)
Πperson-name (σcompany-name = “First Bank
Corporation” (works))
b.
Find the names of all employees who do not work for First
Bank Corporation. (‘08, ‘09, ’11, ‘16)
The
following solutions assume that all people work for exactly one company.
If one
allows people to appear in the database (e.g. in employee) but not
appear in works, the problem is more complicated. We give solutions
for this more realistic case later.
Πperson-name
(σcompany-name _= “First Bank Corporation”(works))
If
people may not work for any company:
Πperson-name(employee)
− Πperson-name (σ(company-name = “First Bank Corporation”)(works))
c.
Find the names and cities of residence of all employees who work for
First Bank Corporation.
Πperson-name,
city (employee ×(σcompany-name = “First Bank Corporation” (works)))
d.
Find the names, street address, and cities of residence of all employees
who work for First Bank Corporation and earn more than $10,000 per annum. (‘08, ‘09, ’11, ‘14,
‘16)
Πperson-name,
street, city (σ(company-name = “First Bank Corporation” ∧ salary > 10000) works × employee)
e.
Find the names of all employees in this database who live in the same
city as the company for which they work. (‘14) (‘08,
’11, ‘16)
Πperson-name (employee× works × company)
f.
Find the names of all employees who live in the same city and on the
same street as do their managers.
Πperson-name
((employee × manages) × (manager-name=employee2.person-name
∧ employee.street =employee2.street ∧ employee.city =employee2.city)(ρemployee2
(employee)))
g.
Find the names of all employees who earn more than every employee of
Small Bank Corporation. (‘08, ’11, 14, ‘16)
Πperson-name
(works) − (Πworks.person-name (works × (works.salary
≤works2.salary ∧ works2.company-name=“Small Bank
Corporation”) ρworks2(works)))
h.
Assume the companies may be located in several cities. Find all
companies located in every city in which Small Bank Corporation is located.
(‘14) (‘08, ’11, ‘16)
Note: Small Bank Corporation will be included in each answer.
Πcompany-name (company ÷ (Πcity (σcompany-name=“Small
Bank Corporation” (company))))
i.
Find the name of all employee who live in the same city and
same street as Rana (‘09)
j.
List the employee names and their salary by manager name.
(‘09)
Give an expression in the relational algebra for each
request:
a. Modify the database so that Jones now lives in Newtown.
employee ← Πperson-name, street, “Newtown”
(σperson-name=“Jones” (employee))
∪ (employee – σperson-name = “Jones”
(employee))
b. Give all employees of First Bank Corporation a 10
percent salary raise.
works ← Πperson-name,company-name,1.1*salary(σ(company-name=“First
Bank Corporation”)(works))∪ (works
− σcompany-name=“First Bank Corporation”(works))
c. Give all managers in this database a 10 percent salary
raise.
The update syntax allows reference to a single relation
only. Since this update requires access to both the relation to be updated (works)
and the manages relation, we must use several steps. First we identify
the tuples of works to be updated and store them in a temporary
relation (t1). Then we create a temporary relation containing the new
tuples (t2). Finally, we delete the tuples in t1, from works
and insert the tuples of t2.
t1
← Πworks.person-name, company-name, salary (σworks.person-name=manager-name(works
× manages))
t2
← Πperson-name,company-name,1.1∗ salary(t1) works ← (works − t1) ∪ t2
d. Give all managers in this database a10 percent salary raise,
unless the salary would be greater than $100,000. In such cases, give only a
3 percent raise.
The same situation arises here. As before, t1, holds
the tuples to be updated and t2 holds these tuples in their updated
form.
t1
← Πworks.person-name, company-name, salary(σworks.person-name=manager-name(works
× manages))
t2
← Πworks.person-name,company-name,salary∗1.03 (σt1.salary ∗ 1.1 > 100000(t1))
t2
← t2 ∪ (Πworks.person-name,company-name,salary∗1.1(σt1.salary ∗ 1.1 ≤ 100000(t1)))
works ← (works − t1) ∪ t2
e.
DeletealltuplesintheworksrelationforemployeesofSmallBankCorporation.
works ← works − σcompany−name=“Small Bank
Corporation”(works)
Give a relational-algebra expression for each of the
following queries:
a. Find the company with the most employees.
t1
← company-name G count-distinct person-name(works)
t2
← maxnum-employees(ρcompany-strength(company-name,num-employees)(t1))
b. Find the company with the smallest payroll.
t1 ← company-name G sum salary(works)
t2 ← minpayroll(ρcompany-payroll(company-name, payroll)(t1))
Πcompany-name(ρt3(company-name,
payroll)(t1) 1 ρt4(payroll)(t2))
c. Find those companies whose employees earn a higher
salary, on average, than the average salary at First Bank Corporation
t1
← company-name G avg salary(works)
t2
← σcompany-name = “First Bank Corporation”(t1)
|
15 March 2018
Relational Algebra 2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment