MODEL ANSWERS: DBMSASSIGNMENT # 2
(REFER UNSOLVED EXCERCISES: CHAPTER 3 OF KORTH)
- PS Gill
Problem # 1 For the schema given below, express the following queries in
relational algebra:-
employee (person-name, street, city)
works (person-name, company-name, salary)
company (company-name, city)
manages (person-name, manager-name)
(a) Find names of employees working for First-Bank-Co
∏person-name (σcompany-name = “First-Bank-Co” (works))
(b) Find names and cities of residence of employees working for First-Bank-Co
∏person-name, city (σcompany-name = “First-Bank-Co” (employee × works))
(c) Find name, street and city of residence of employees working for First-Bank-Co
and earning more than 10,000
∏person-name, street, city(σcompany-name = “First-Bank-Co” ∧ salary > 10000 (employee × works))
(d) Find names of employees working in the same city where they live
∏person-name (σemployee. city = company. city (employee × works × company))
(e) Find names of employees who live in the same street and city as their managers
emp ← employee × manages
∏ emp. person-name (σemp. manager-name = mgr. person-name ∧ emp. city = mgr. city ∧ emp. street = mgr. street
(emp × ρmgr (employee)))
(f) Find names of employees, who are not working for Fist-Bank-Co
∏ person-name ( σcompany-name ≠ “First-Bank-Co” (works))
(g) Find names of employees, who earn more than every employee of First-Bank-Co
temp ← G max ( salary) ( σ company-name = “First-Bank-Co” (works) )
∏ person-name (σ salary > Temp (works) )
(h) Find companies located in every city, where First-Bank-Co is located
company ÷ ∏city (σ company-name = “First-Bank-Co” (company) )
Problem # 2 For the schema given below, find name & city of customers having a loan
from the bank:-
Customer (customer-name, customer-street, customer-city)
Branch (branch-name, branch-city, assets)
Account (account-number, branch-name, balance)
2.
Loan (loan-number, branch-name,amount)
Depositor (customer-name, account-number)
Borrower (customer-name, loan-number)
Answer:- ∏ customer-name, customer-city (customer × borrower)
(a) Though Jackson’s name is there in the borrower table, why does he not appear in
the result (For Borrower Table, refer Fig 3.7 of Korth)?
Since, Jackson’s name is not there in the customer table (For Customer Table, refer
Fig 3.4 of Korth). So, the natural join of customer and borrower will not contain
any tuple for Jackson.
(b) What should be done to make Jackson appear in the result?
Add Jackson’s information in customer table.
(c ) Without modifying the database, use outer-join to make Jackson appear in the
result.
∏ customer-name, customer-city (customer × borrower)
Problem # 3 Consider the schema shown in Problem # 1 and give relational algebra
expressions for the following:-
(a) Modify database so that Jones now lives in Newtown
temp ← ∏ person-name, street, “Newtown” (σperson-name= “Jones” (employee))
employee ← temp ∪ ( employee – (σperson-name= “Jones” (employee)))
(b) Give all employees of First-Bank-Co a 10% rise in salary
temp ← ∏ person-name, company-name, salary*1.1 (σcompany-name= “First-Bank-Co” (works))
works ← Temp ∪ ( works – (σcompany-name= “First-Bank-Co” (works)))
(c) Give all managers a 10% rise in salary
temp1 ←∏works. person-name (works × works. person-name = manages. manager-name manages)
temp2 ← works × temp1
works ← ∏ person-name, company-name, salary*1.1 (temp2) ∪ (works – temp2)
(d) Give all managers a 10% rise in salary unless salary is more than 100000; and in
that case give a 3% rise.
temp1 ←∏works. person-name (works × works. person-name = manages. manager-name manages)
temp2 ← works × Temp1
temp3 ← ∏ person-name, company-name, salary*1.10 (σsalary < 100000 (temp2))
temp4 ← ∏ person-name, company-name, salary*1.03 (σsalary > 100000 (temp2))
works ← temp3 ∪ temp4 ∪ (works – temp2)
3.
(e) Delete alltuples of works for employees working for Small-Bank-Co
works ← works - σcompany-name= “Small-Bank-Co” (works)
Problem # 4 For the schema shown in Problem # 2, find the accounts held by
more than two customers.
temp ← account-number G count-distinct (customer-name) as NUMB (depositor)
∏ account-number (σ NUMB > 2 (temp) )
Problem # 5 For the schema shown in Problem # 2, give relational algebra
expressions for the following queries:-
(a) Find the company with most employees.
temp1 ← company-name G count-distinct (person-name) as NUMB (works)
temp2 ← G max (NUMB) (temp1)
∏ company-name (σ NUMB = temp2 (temp1) )
(b) Find the company with the smallest pay-roll.
temp1 ← company-name G count-distinct (person-name) as NUMB (works)
temp2 ← G min (NUMB) (temp1)
∏ company-name (σ NUMB = Temp2 (temp1) )
(c) Find the company, whose employees earn more salary, on the average, than the
average salary of First-Bank-Co employees.
temp1 ← company-name G average (salary) as AVG-SAL (works)
temp2 ← G average (salary) (σ company-name = “First-Bank-Co” (works))
∏ company-name (σ AVG-SAL > Temp2 (temp1) )
Problem # 6 Let there be Schemas R (A,B,C) & S(D,E,F) and relations r(R) & s(S).
Give expressions in Tuple Relational Calculus (TRC) equivalent to the following
expressions in Relational Algebra (RA).
(a) ∏A (r)
TRC: { t | ∃ s ∈ r ( t [A] = s[A] ) }
(b) σB=17 (r)
TRC: { t | t ∈ r ∧ t [B] = 17 }
(c) r × s
TRC:{ t | ∃ u ∈ r ( t [A] = u[A] ∧ t [B] = u [B] ∧ t [C] = u [C] ] ∧
∃ v ∈ s ( t [D] = v[D] ∧ t [E] = v [E] ∧ t [F] = v [F] ) ) }
4.
(d) ∏A,F (σC=D(r × s))
{ t | ∃ u ∈ r ( t [A] = u[A] ∧ ∃ v ∈ s ( t [F] = v [F] ∧ u [C] = v [D]) ) }
Problem # 7 Let there be Schema R (A,B,C) & and relations r1 (R) & r2 (R). Give
expressions in Domain Relational Calculus (DRC) equivalent to the following
expressions in Relational Algebra (RA).
(a) ∏A (r1)
DRC: { 〈a〉 | ∃ b,c ( 〈a,b,c〉 ∈ r1 ) }
(b) σB=17 (r1)
DRC: { 〈a,b,c〉 | 〈a,b,c〉 ∈ r1 ∧ b = 17 }
(c) r1 ∪ r2
DRC: { 〈a,b,c〉 | 〈a,b,c〉 ∈ r1 ∨ 〈a,b,c〉 ∈ r2}
(d) r1 ∩ r2
DRC: { 〈a,b,c〉 | 〈a,b,c〉 ∈ r1 ∧ 〈a,b,c〉 ∈ r2}
(e) r1 − r2
DRC: { 〈a,b,c〉 | 〈a,b,c〉 ∈ r1 ∧ ¬ 〈a,b,c〉 ∈ r2}
(f) ∏A,B (r1) × ∏B,C (r2)
DRC: { 〈a1,b,c2〉 | ∃ c1 ( 〈a1,b,c1〉 ∈ r1 ∧ ∃ a2 ( 〈a2,b,c2〉 ∈ r2 ))}
Problem # 8 Let there be Schemas R (A,B) & S(A,C) and relations r(R) & s(S). Give
Relational Algebra expressions equivalent to the following Domain Relational Calculus
(DRC) expressions.
(a) { 〈a〉 | ∃ b ( 〈a,b〉 ∈ r ∧ b = 17 )}
RA (σ B= 17 (r))
(b) { 〈a, b,c〉 | 〈a,b〉 ∈ r ∧ 〈a,c〉 ∈ s)}
RA r × s
(c) { 〈a〉 | ∃ b ( 〈a,b〉 ∈ r ) ∨ ∀ c (∃ d ( 〈d,c〉 ∈ s ) ⇒ 〈a,c〉 ∈ s )}
RA ∏A ( r ∪ ∏A,B ( ( r × s ) ÷ ∏C (s)))