SlideShare a Scribd company logo
MODEL ANSWERS: DBMS ASSIGNMENT # 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)
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)
(e) Delete all tuples 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] ) ) }
(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)))
May you always prosper!
Bye!
PS Gill
May you always prosper!
Bye!
PS Gill

More Related Content

What's hot

Query processing
Query processingQuery processing
Query processing
Deepak Singh
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
Dr. C.V. Suresh Babu
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
Karthi Keyan
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
Iffat Anjum
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Control structures in C
Control structures in CControl structures in C
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
BalajiGovindan5
 
Message passing ( in computer science)
Message   passing  ( in   computer  science)Message   passing  ( in   computer  science)
Message passing ( in computer science)
Computer_ at_home
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
Sunita Sahu
 
distributed Computing system model
distributed Computing system modeldistributed Computing system model
distributed Computing system model
Harshad Umredkar
 
Structure in C
Structure in CStructure in C
Structure in C
Fazle Rabbi Ador
 
fault-tolerance-slide.ppt
fault-tolerance-slide.pptfault-tolerance-slide.ppt
fault-tolerance-slide.ppt
Shailendra61
 
Data Encryption Standard
Data Encryption StandardData Encryption Standard
Data Encryption Standard
JeevananthamArumugam
 
CS8592-OOAD Lecture Notes Unit-3
CS8592-OOAD Lecture Notes Unit-3CS8592-OOAD Lecture Notes Unit-3
CS8592-OOAD Lecture Notes Unit-3
Gobinath Subramaniam
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
VishrudhMamidala
 
Presentation of computer network on data link layer
Presentation of computer network on data link layerPresentation of computer network on data link layer
Presentation of computer network on data link layer
sumit gyawali
 
Enum
EnumEnum
Enum
zindadili
 

What's hot (20)

Query processing
Query processingQuery processing
Query processing
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Binary operator overloading
Binary operator overloadingBinary operator overloading
Binary operator overloading
 
Message passing ( in computer science)
Message   passing  ( in   computer  science)Message   passing  ( in   computer  science)
Message passing ( in computer science)
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
 
distributed Computing system model
distributed Computing system modeldistributed Computing system model
distributed Computing system model
 
Structure in C
Structure in CStructure in C
Structure in C
 
fault-tolerance-slide.ppt
fault-tolerance-slide.pptfault-tolerance-slide.ppt
fault-tolerance-slide.ppt
 
Data Encryption Standard
Data Encryption StandardData Encryption Standard
Data Encryption Standard
 
CS8592-OOAD Lecture Notes Unit-3
CS8592-OOAD Lecture Notes Unit-3CS8592-OOAD Lecture Notes Unit-3
CS8592-OOAD Lecture Notes Unit-3
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
Presentation of computer network on data link layer
Presentation of computer network on data link layerPresentation of computer network on data link layer
Presentation of computer network on data link layer
 
Enum
EnumEnum
Enum
 

Dbms+exercises

  • 1. MODEL ANSWERS: DBMS ASSIGNMENT # 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 all tuples 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)))
  • 5. May you always prosper! Bye! PS Gill
  • 6. May you always prosper! Bye! PS Gill