“WHAT IF” QUERIES

“What if” queries are a frequent requirement; they’re used to explore the effect of making certain changes without actually having to make (and subsequently unmake, possibly) the changes in question. Here’s an example (“What if parts in Paris were in Nice instead and their weight was doubled?”):

image with no caption

As you can see, the Tutorial D expression on the left here makes use of EXTEND once again. Note, however, that the target attributes in the assignments in braces aren’t “new” attributes, as they normally are for EXTEND; instead, they’re attributes already existing in the specified relation. What the expression does is this: It yields a relation containing exactly one tuple t2 for each tuple t1 in the current value of relvar P for which the city is Paris—except that, in that tuple t2, the weight is double that in tuple t1 and the city is Nice, not Paris.[107] In other words, the expression overall is shorthand for the following (and this expansion should help you understand the SQL version of the query):

     WITH ( R1 := P WHERE CITY = 'Paris' ,
            R2 := EXTEND R1 : { NC := 'Nice' , NW := 2 * WEIGHT } ,
            R3 := R2 { ALL BUT CITY , WEIGHT } ) :
     R3 RENAME { NC AS CITY , NW AS WEIGHT }

And now I can take care of some unfinished business from Chapter 5. In that chapter, I said the relational UPDATE operator was shorthand for a certain relational assignment, but the details were a little more complicated than they were for INSERT and DELETE. Now I can explain those details. By way of example, consider the following UPDATE statement:

     UPDATE P WHERE CITY = 'Paris' :
                  { CITY := 'Nice' , WEIGHT := 2 * WEIGHT } ;

This statement is logically equivalent to the following relational assignment:

     P := ( P WHERE CITY ≠ 'Paris' )
            UNION
          ( EXTEND ( P WHERE CITY = 'Paris' ) :
                           { CITY := 'Nice' , WEIGHT := 2 * WEIGHT } ) ;

Alternatively, recall from Chapter 5 that “updating relvar R” really means we’re replacing the relation r1 that’s the original value of R by another relation r2, where r2 is computed as (r1 MINUS s1) UNION s2 for certain relations s1 and s2. In the case at hand, using “≝” to denote “is defined as,” we have:

     s1  ≝  P WHERE CITY = 'Paris'
     s2  ≝  EXTEND ( P WHERE CITY = 'Paris' ) :
                           { CITY := 'Nice' , WEIGHT := 2 * WEIGHT } )

Thus, the expanded form of the UPDATE becomes:

     P := ( P MINUS s1 ) UNION s2 ;

Note: Actually, we could safely replace MINUS and UNION here by I_MINUS and D_UNION, respectively, and we could safely drop the parentheses. (In both cases, why?)



[107] Note, therefore, that the input relation isn’t exactly being “extended” in the usual sense, so it might be nice to find a better keyword than EXTEND for the purpose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.29.151