12.07.2015 Views

Relational calculus and SQL

Relational calculus and SQL

Relational calculus and SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7<strong>Relational</strong> Calculus<strong>Relational</strong> algebra tells us how to construct atable, while relational <strong>calculus</strong> tells us what toget for that table.For example, consider the query “Get suppliernumbers <strong>and</strong> cities for suppliers who supplypart P2”. An algebraic formulation could bethe following:((S JOIN SP) WHERE P#=P#(‘P2’){S#, CITY}More specifically, we 1) join supplier <strong>and</strong> shipmenttuples over S#; 2) restrict the result ofthat join to tuples for part P2; <strong>and</strong> 3) projectthe result of the just obtained restriction overS# <strong>and</strong> CITY.1


What does a Calculusian do?In contrast, a <strong>calculus</strong> formulation simply states“Get S# <strong>and</strong> CITY for suppliers such thatthere exists a shipment SP with the same S#value <strong>and</strong> with the P# value equal to P2.”She will write down something like this:(SX.S#, SX.City)Where Exists SPX (SPX.S#=SX.S# <strong>and</strong> SPX.P#=’P2’);This also immediately leads to the following<strong>SQL</strong> statement:Select S#, Cityfrom S, SPWhere SP.S#=S.S# AND SP.P#=’P2’;2


Range variablesIn the previous expression, both SX <strong>and</strong> SPXare examples of a range variable, which is basicallya variable that goes over some specifiedtable. If a variable V ranges over a table r,then at any time, the value of V has to be arow of r. Thus, the value of SX has to be arow in the table S.We can then make it precise what we want.For example,RANGE OF SX IS S;RETRIEVE (SX.S#) WHERE SX.CITY="London";means that ‘For ech possible row of table S,retrieve the S# piece, iff its CITY piece equalsto “London”.’ In <strong>SQL</strong>,Select S# from S Where S.City=’London’;3


What should they look like?We begin with the range variables:::= RANGEVAR RANGES OVER;For example,RANGEVAR SX RANGERS OVER S;RANGEVAR SPX RANGES OVER SP;A range variable does not need range over abase table.4


A nasty piece?Given the following specification, where ‘,’ operatorindicates a union operation,RANGEVAR SU RANGES OVER(SX WHERE SX.CITY=‘London’ ),(SX WHERE EXISTS SPX(SPX.S#=SX.S#AND SPX.P#=P#(‘P1’)));SU takes all the suppliers that either live in Londonor supply P1. In RA, we have(S Where City=’London’)UNION ((S Join SP) Where SP.P#=’P1’)while in <strong>SQL</strong>, we haveSelect * from S Where City=’London’UnionSelect S.* from S, SPWhere S.S#=SP.S# <strong>and</strong> SP.P#=’P1’;5


Free <strong>and</strong> bound variablesAll references to SX, PX, <strong>and</strong> SPX are freein the following comparisons in the sense thatthey can choose whatever values they want:SX.S#=S#(‘S1’)SX.S#=SPX.S#SPX.P#=PX.P#PX.WEIGHT


QuantifiersQuantifiers, collectively, provides us with convenience<strong>and</strong>, sometimes, necessity.The existential quantifier EXISTS V P(V ) meansthat there exists at least one value of V thatmakes P (V ) true, <strong>and</strong> the universal quantifier,FORALL V P(V ) means that for all values of V ,P (V ) is true.For example, if the range of V is the collectionof the members of the US Senate in 2004,<strong>and</strong> if P (V ) is the statement “V is female”,then EXISTS VP(V ) is true, but FORALL VP(V )is false.7


What should it look like?A <strong>calculus</strong> expression looks like the following:::= [WHERE]::=In other words, we just state what attributesof rows (from what tables) do we want, <strong>and</strong>what conditions those rows have to satisfy. Forexample,SX.SNAME WHERE EXISTS SPX (SPX.S#=SX.S# ANDSPX.P#=P#(‘P2’))8


From Calculus to <strong>SQL</strong>1. Get supplier numbers <strong>and</strong> status for suppliersin Paris with status > 20.(SX.S#, SX.STATUS)WHERE SX.CITY=‘Paris’ AND SX.STATUS>20We can immediately turn it into an <strong>SQL</strong> expression:Select S#, StatusFrom SWhere City=’Paris’ <strong>and</strong> Status>20;9


2. Get all pairs of supplier numbers such thatthey are located in the same city.(SX.S# AS SA, SY.S# AS SB)WHERE SX.CITY=SY.CITY AND SX.S#


3. Get the supplier names for suppliers whosupply at least one red partSX.SNAME WHERE EXISTS SPX (SPX.S#=SX.S# ANDEXISTS PX(PX.P#=SPX.P# ANDPX.COLOR=COLOR(‘Red’)))Its algebraic expression is the following:((((S JOIN SP) JOIN P) WHERE COLOR=‘Red’)){SNAME}<strong>and</strong> the corresponding <strong>SQL</strong> query is the following:Select SNAMEFrom S, P, SPWhere S.S#=SP.S# AND SP.P#=P.P#AND P.COLOR=’Red’;11


4. Get the supplier names for suppliers whosupply all partsSX.SNAME WHERE FORALL PX (EXISTS SPX(SX.S#=SPX.S# ANDSPX.P#=PX.P#))Its algebraic expression is the following:(((S{S#} DIVIDEBY P{P#} PER SP{S#,P#})JOIN S) {SNAME}Comparing them, the <strong>calculus</strong> expression tellsexplicitly what kind of rows we want. We canthen convert it into an <strong>SQL</strong> query as we didin Chapter 6 for which you all hated me.12


5. Get full supplier information for supplierswho don’t supply P2SX WHERE NOT EXISTS SPX(SPX.S#=SX.S# ANDSPX.P#=P#(‘P2’))We can then immediately put it into the following<strong>SQL</strong> query:Select *From SWhere Not Exists(Select *From SPWhere SP.S#=S# <strong>and</strong> SP.P#=’P2’;);13


Remember this piece?The algebraic expression is the following:((S{SNAME} MINUS (SP WHERE P#=P#(‘P2’)){S#})JOIN S) {SNAME}Below is its conversion:Select SNAMEfrom SMINUSSelect SNAMEfrom S, SPwhere S.S#=SP.S# <strong>and</strong> SP.P#=’P2’;Question: Which one do you prefer?14


Calculus vs. AlgebraIt can be shown that any <strong>calculus</strong> expressioncan be mechanically converted to an algebraicexpression. Thus, anything we can do withCalculus can be done with algebra, as well.Thus, we say RA is relational complete. So is<strong>SQL</strong>.The other direction is also true. Thus, the twoare really equivalent to each other in terms oftheir capability.Another point is that RA is a procedural language,while RC is a non-procedural language,thus “closer” to <strong>SQL</strong>.To use which one as a guide to come up withthe <strong>SQL</strong>, or nothing at all, is really a personalchoice.15


In addition to...It is nice for a language to be computationallycomplete as well, i.e., it is capable of computingall computable functions. We added suchoperators as EXTEND AND SUMMARIZE tomake relational algebra to be more computationallycapable. We might want to do thesame for the <strong>calculus</strong>, but it already includesanalogs of the algebraic EXTEND AND SUM-MARIZE operators. For examples,1. Get the part number of those parts withweight>6000 grams, <strong>and</strong> their weight in grams.(PX.P#,PX.WEIGHT*454 AS GMWT)WHERE PX.WEIGHT*454>WEIGHT(6000)We already saw its <strong>SQL</strong> format as follows:select P.*, P.WEIGHT*454 AS GMWT from Pwhere P.WEIGHT*454>6000;16


2. For each shipment, get full shipment details,including total shipment weight.(SPX, SPX.QTY * PX.WEIGHT AS SHIPWT) WHERE PX.P#=SPX.P#Its algebraic expression is as follows:EXTEND (P JOIN SP) ADD (WEIGHT*QTY) AS SHIPWTWe also saw its <strong>SQL</strong> format as follows:select SP.*, SP.QTY*P.Weight as SHWTfrom sp, pwhere sp.p#=p.p#;17


3. For each part, get the part number <strong>and</strong> thetotal shipment quantity(PX.P#, SUM(SPX WHERE SPX.P#=PX.P#, QTY) AS TOTQTY)Question: What is its <strong>SQL</strong> format?Select P#, Sum(QTY)From P, SPWhere P.P#=SP.P#Group by P#;4. For each supplier, get the supplier number<strong>and</strong> the total number of parts supplied.(SX.S#, COUNT (SPX WHERE SPX.S#=SX.S#) AS #_PARTS)Question: What is its <strong>SQL</strong> format?Select S#, Count(*)From SPGroup by S#;18


It is your turn1. Read through all the other examples in §8.6,<strong>and</strong> self-study §8.8.2. Complete at least 10 each, but AMAP, ofExercises 8.13 <strong>and</strong> 8.14.19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!