Docsity

Relational Algebra, Relational Calculus, Datalog, Study notes of Algebra

Toggle sidebar
bg1
Relational)Algebra,)Relational)Calculus,)Datalog!
!
Past!Midterm!problems!
1)!Schema:!R(A,!B),!S(C,!D,!E),!T(F,!G);!Spring,!2011!
Write!Relational!Algebra!Plan!for!the!SQL!query!below.!Your!answer!should!be!a!tree!representing!the!
relational!algebra!plan.!!
SELECT!R.B,!S.E,!sum(T.G)!
FROM!R,!S,!T!
WHERE!R.A!=!S.C!and!S.D!=!T.F!and!T.H!>!55!
GROUP!BY!R.B,!S.E!
!
2)!Consider!the!following!database!schema!(Fall!2011)!
Neighbors(name1,name2,duration),!Colleagues(name1,name2,duration)!
(a)!Write!a!Relational!Algebra!Plan!for!the!SQL!query!below.!Your!answer!can!be!in!the!form!of!an!
expression!or!a!tree,!whichever!you!prefer:!
SELECT!DISTINCT!C1.name1,!C2.name2!
FROM!Colleagues!C1,!Neighbors!N,!Colleagues!C2!
WHERE!C1.name2!=!N.name1!
AND!N.name2!=!C2.name1!
AND!C1.duration!<!10!
AND!C2.duration!<!10!
AND!N.duration!>!100!
pf3

Partial preview of the text

Download Relational Algebra, Relational Calculus, Datalog and more Study notes Algebra in PDF only on Docsity!

Relational Algebra, Relational Calculus, Datalog Past Midterm problems

  1. Schema: R(A, B), S(C, D, E), T(F, G); Spring, 2011 Write Relational Algebra Plan for the SQL query below. Your answer should be a tree representing the relational algebra plan. SELECT R.B, S.E, sum(T.G) FROM R, S, T WHERE R.A = S.C and S.D = T.F and T.H > 55 GROUP BY R.B, S.E
  2. Consider the following database schema (Fall 2011) Neighbors(name1,name2,duration), Colleagues(name1,name2,duration) (a) Write a Relational Algebra Plan for the SQL query below. Your answer can be in the form of an expression or a tree, whichever you prefer: SELECT DISTINCT C1.name1, C2.name FROM Colleagues C1, Neighbors N, Colleagues C WHERE C1.name2 = N.name AND N.name2 = C2.name AND C1.duration < 10 AND C2.duration < 10 AND N.duration > 100

(b) Write a Datalog query that returns all neighbors who do not have any colleagues in common. Solution : NonAnswers(n1, n2):-ยญโ€ Neighbors(n1, n2, -ยญโ€), Colleagues(n1, c, -ยญโ€), Colleagues(n2, c, -ยญโ€) A(n1, n2):-ยญโ€ Neighbors(n1, n2, -ยญโ€), NOT NonAnswer(n1,n2) (c) Indicate if the following relational calculus queries are correct or not (true or false). Note: This is not meant to be a tricky question. Errors, if any, should be reasonably obvious. You do NOT need to correct wrong queries:

  • ๐ด ๐‘ฅ = โˆƒ๐‘ฆโˆƒ๐‘ง ๐‘๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ ๐‘ฅ, ๐‘ฆ, โˆ’ ๐ถ๐‘œ๐‘™๐‘™๐‘’๐‘Ž๐‘”๐‘ข๐‘’๐‘ (๐‘ฆ, ๐‘ง, โˆ’) TRUE
  • ๐ด ๐‘ฅ = ๐‘๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ ๐‘ฅ, โˆ’, โˆ’ (โˆ€๐‘ฆ ๐‘๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ๐‘  ๐‘ฅ, ๐‘ฆ, โˆ’ ๐ถ๐‘œ๐‘™๐‘™๐‘’๐‘Ž๐‘”๐‘ข๐‘’๐‘  ๐‘ฅ, ๐‘ฆ, โˆ’ ) FALSE
  • ๐ด ๐‘ฅ = ๐‘๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ๐‘  ๐‘ฅ, โˆ’, โˆ’ โ‹€(โˆ€๐‘ฆ ๐‘๐‘’๐‘–๐‘”โ„Ž๐‘๐‘œ๐‘Ÿ๐‘  ๐‘ฅ, ๐‘ฆ, โˆ’ โ†’ โˆƒ๐‘ง๐ถ๐‘œ๐‘™๐‘™๐‘’๐‘Ž๐‘”๐‘ข๐‘’๐‘  ๐‘ฆ, ๐‘ง, โˆ’ ) TRUE Extra Stuffs! Indices For each statement below, indicate whether it is true of false. i. An index may help a select-ยญโ€from-ยญโ€where SQL query run faster, or may not affect its running time, but it can never make a query run slower. TRUE ii. ii. An index may help an update (insert, delete, or update) SQL query run faster, or may not affect its running time, but it can never make a query run slower. FALSE iii. Consider a selection operation ๐œŽ!"#$%!!"โ‹€!"#$%!!""(๐‘ƒ๐‘Ÿ๐‘œ๐‘‘๐‘ข๐‘๐‘ก) Using an unclustered index on price will make the query at least as fast as scanning the entire table Product. FALSE