CHAPTER 9

9.1

        VAR NON_COLOCATED VIRTUAL
 ( ( S { SNO } JOIN P { PNO } ) NOT MATCHING ( S JOIN P ) )
KEY { SNO , PNO } ;
     CREATE VIEW NON_COLOCATED AS
          ( SELECT SNO , PNO
            FROM   S , P
            WHERE  S.CITY <> P.CITY )
            /* UNIQUE ( SNO , PNO ) */ ;

9.2 Substituting the view definition for the view reference in the outer FROM clause, we obtain:

     SELECT DISTINCT STATUS , QTY
     FROM ( SELECT SNO , SNAME , STATUS , PNO , QTY
            FROM   S NATURAL JOIN SP
            WHERE  CITY = 'London' ) AS Temp
     WHERE  PNO IN
          ( SELECT PNO
            FROM   P
            WHERE  CITY <> 'London' )

This simplifies (potentially!) to:

     SELECT DISTINCT STATUS , QTY
     FROM   S NATURAL JOIN SP
     WHERE  CITY = 'London'
     AND    PNO IN
          ( SELECT PNO
            FROM   P
            WHERE  CITY <> 'London' )

9.3 The sole key is {SNO,PNO}. The predicate is: Supplier SNO is under contract, is named SNAME, has status STATUS, is located in London, and supplies part PNO in quantity QTY.

9.4 Note that a. and b. are expressions, the rest are statements.

  1.   ( P WHERE WEIGHT > 14.0 ) WHERE COLOR = 'Green'

    This expression can be simplified to:

    P WHERE WEIGHT > 14.0 AND COLOR = 'Green'

    The simplification is worth making, too, because the first formulation implies two passes over the data while the second implies just one.

  2.   ( EXTEND ( P WHERE WEIGHT > 14.0 ) :
                     { W := WEIGHT + 5.3 } ) { PNO , W }
  3.   INSERT ( P WHERE WEIGHT > 14.0 )
    RELATION { TUPLE { PNO 'P9' , PNAME 'Screw' , WEIGHT 15.0 ,
                                  COLOR 'Purple' , CITY 'Rome' } } ;

    Observe that this INSERT is logically equivalent to a relational assignment in which the target is specified as something other than a simple relvar reference. The ability to update views implies that such assignments must indeed be legitimate, both syntactically and semantically, although the corresponding syntax is currently not supported in Tutorial D (neither for assignment in general nor for INSERT in particular). Note: Similar but not identical remarks apply to parts d. and e. below.

  4. DELETE ( P WHERE WEIGHT > 14.0 ) WHERE WEIGHT < 9.0 ;

    This syntax is currently illegal, although oddly enough the following (which is obviously logically equivalent to that just shown) is legal:

    DELETE P WHERE WEIGHT > 14.0 AND WEIGHT < 9.0 ;

    Of course, this DELETE is actually a “no op,” because WEIGHT > 14.0 AND WEIGHT < 9.0 is a logical contradiction. Do you think the optimizer would be able to recognize this fact?

  5.   UPDATE ( P WHERE WEIGHT > 14.0 ) WHERE WEIGHT = 18.0 :
                                           { COLOR := 'White' } ;

    Again this syntax is currently illegal, but the following is legal:

    UPDATE P WHERE WEIGHT > 14.0 AND WEIGHT = 18.0 :
                                           { COLOR := 'White' } ;

    Do you think the optimizer would be able to recognize the fact that the restriction condition WEIGHT > 14.0 here can be ignored?

9.5 Here first is an SQL version of the view definition from Exercise 9.4:

     CREATE VIEW HP AS
          ( SELECT PNO , PNAME , COLOR , WEIGHT , CITY
            FROM   P
            WHERE  WEIGHT > 14.0 )
            /* UNIQUE ( PNO ) * / ;

For parts a.-e., I first show an SQL analog of the Tutorial D formulation, followed by the expanded form:

  1.   SELECT HP.PNO , HP.PNAME , HP.COLOR , HP.WEIGHT , HP.CITY
    FROM   HP
    WHERE  HP.COLOR = 'Green'
    
    SELECT HP.PNO , HP.PNAME , HP.COLOR , HP.WEIGHT , HP.CITY
    FROM ( SELECT PNO , PNAME , COLOR, WEIGHT , CITY
          FROM   P
          WHERE  WEIGHT > 14.0 ) AS HP
    WHERE  HP.COLOR = 'Green'

    I leave further simplification, here and in subsequent parts, as a subsidiary exercise (barring explicit statements to the contrary).

  2.   SELECT PNO , WEIGHT + 5.3 AS W
    FROM   HP
    
    SELECT HP.PNO , HP.WEIGHT + 5.3 AS W
    FROM ( SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , P.CITY
          FROM   P
           WHERE  P.WEIGHT > 14.0 ) AS HP
  3.   INSERT INTO HP ( PNO , PNAME , WEIGHT , COLOR , CITY )
          VALUES ( 'P9' , 'Screw' , 15.0 , 'Purple' , 'Rome' ) ;
    
    INSERT INTO ( SELECT P.PNO , P.PNAME , P.WEIGHT , P.COLOR , P.CITY
           FROM   P
          WHERE  P.WEIGHT > 14.0 ) AS HP
         VALUES ( 'P9' , 'Screw' , 15.0 , 'Purple' , 'Rome' ) ;

    The remarks regarding Tutorial D in the solution to Exercise 9.4c apply here also, mutatis mutandis.

  4.   DELETE FROM ( SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , P.COLOR
           FROM   P
     WHERE  P.WEIGHT > 14.0 ) AS HP
    WHERE HP.WEIGHT < 9.0 ;

    This transformed version isn’t valid SQL syntax, but a valid equivalent is a little easier to find:

    DELETE FROM P WHERE WEIGHT > 14.0 AND WEIGHT < 9.0 ;

    (As noted in the answer to Exercise 9.4d, this DELETE is actually a “no op.”)

  5.   UPDATE ( SELECT P.PNO , P.PNAME , P.COLOR , P.WEIGHT , P.COLOR
      FROM   P
      WHERE  P.WEIGHT > 14.0 ) AS HP
    SET    COLOR = 'White'
    WHERE  HP.WEIGHT = 18.0 ;

Syntactically valid equivalent:

UPDATE P
SET    COLOR = 'White'
WHERE  WEIGHT = 18.0 AND WEIGHT > 14.0 ;

9.6 Here are some:

  • If users are to operate on views instead of base relvars, it’s clear that those views should look to the user as much like base relvars as possible. In accordance with The Principle of Interchangeability, in fact, the user shouldn’t have to know they’re views at all but should be able to treat them as if they were base relvars. And just as the user of a base relvar needs to know what keys that base relvar has, so the user of a view needs to know what keys that view has. Explicitly declaring those keys is one way to make that information available.

  • The DBMS might be unable to infer keys for itself (this is almost certainly the case, in general, with SQL products on the market today). Explicit declarations are thus likely to be the only means available (to the DBA, that is) of informing the DBMS, as well as the user, of the existence of such keys.

  • Even if the DBMS were able to infer keys for itself, explicit declarations would at least enable the system to check that its inferences and the DBA’s explicit specifications were consistent.

  • The DBA might have some knowledge that the DBMS doesn’t, and might thus be able to improve on the DBMS’s inferences.

  • As shown in the body of the chapter, such a facility could provide a simple and convenient way of stating certain important constraints that could otherwise be stated only in circumlocutory fashion.

Subsidiary exercise: Which if any of the foregoing points do you think apply not just to key constraints in particular but to integrity constraints in general?

9.7 One example is as follows: The suppliers relvar is equal to the join of its projections on {SNO,SNAME}, {SNO,STATUS}, and {SNO,CITY}—just so long as appropriate constraints are in force, that is (what are those constraints exactly?). So we could make those projections base relvars and make the join a view.

9.8 Here are some pertinent observations. First, the replacement process itself involves several steps, which might be summarized as follows:

     /* define the new base relvars: */
     VAR LS BASE RELATION
       { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
       KEY { SNO } ;

     VAR NLS BASE RELATION
       { SNO CHAR , SNAME CHAR , STATUS INTEGER , CITY CHAR }
       KEY { SNO } ;
     /* copy the data to the new base relvars: */
     LS  := ( S WHERE CITY = 'London' ) ;
     NLS := ( S WHERE CITY ≠ 'London' ) ;
     /* drop the old relvar: */
     DROP VAR S ;
     /* create the desired view: */
     VAR S VIRTUAL ( LS D_UNION NLS ) KEY { SNO } ;

Now we must do something about the foreign key in relvar SP that references the old base relvar S. Clearly, it would be best if that foreign key could now be taken as referring to the view S instead;[207] if this is impossible (as it typically is in today’s products), then we might want to define another base relvar as follows:

     VAR SS BASE RELATION { SNO CHAR } KEY { SNO } ;

And copy the data (obviously this must be done before dropping base relvar S):

     SS := S { SNO } ;

Now we need to add the following foreign key specification to the definitions of relvars LS and NLS:

     FOREIGN KEY { SNO } REFERENCES SS

Finally, we must change the specification for the foreign key {SNO} in relvar SP to refer to SS instead of S.

9.9 a. No answer provided—except to note that if it’s difficult to answer the question for some product, then that very fact is part of the point of the exercise in the first place. b. Same as for part a., but more so. c. Ditto.

9.10 For the distinction, see the body of the chapter. SQL doesn’t support snapshots at the time of writing.

(It does support CREATE TABLE AS—see the last part of the answer to Exercise 1.16, earlier in this appendix—which allows a base table to be initialized when it’s created, but CREATE TABLE AS has no REFRESH option.)

9.11 “Materialized view” is a deprecated term for a snapshot. The term is deprecated because it muddies concepts that are logically distinct and ought to be kept distinct—by definition, views simply aren’t materialized, so far as the model is concerned—and it’s leading us into a situation in which we no longer have a clear term for a concept we did have a clear term for, originally. It should be firmly resisted. (I realize I’ve probably already lost this battle, but I’m an eternal optimist.) In fact, I’m tempted to go further; it seems to me that people who advocate use of the term “materialized view” are betraying their lack of understanding of the relational model in particular and the distinction between model and implementation in general.

9.12 First, here’s a definition of Design b. in terms of Design a. (pertinent constraints included):

     VAR SSP VIRTUAL ( S JOIN SP )
         KEY { SNO , PNO } ;

     VAR XSS VIRTUAL ( S NOT MATCHING SP )
         KEY { SNO } ;
     CONSTRAINT B_FROM_A IS_EMPTY ( SSP JOIN XSS ) ;

(Constraint B_FROM_A and the specified key constraints are together what we would have to tell the user if we wanted the user to think of relvars SSP and XSS as base relvars, not views.) And here’s a definition of Design a. in terms of Design b.:

     VAR S VIRTUAL ( XSS D_UNION SSP { ALL BUT PNO , QTY } )
         KEY { SNO } ;

     VAR SP VIRTUAL ( SSP { SNO , PNO , QTY } )
         KEY { SNO , PNO } ;
     CONSTRAINT A_FROM_B IS_EMPTY ( SP NOT MATCHING S ) ;

Given these constraints, the designs are information equivalent. But Design a. is superior, because the relvars in that design are in fifth normal form. By contrast, relvar SSP in Design b. isn’t even in second normal form; as a consequence, it displays redundancy and is thereby subject to certain “update anomalies.” Consider also what happens with Design b. if some supplier ceases to supply any parts, or used not to supply any but now does. Further discussion of the problems with Design b. is beyond the scope of this book; I just note that (as the example suggests) database design disciplines like normalization can help in the task of choosing “the best” design from a set of designs that are information equivalent.

Incidentally, I note in passing that—given that {SNO} is a key for relvar S—constraint A_FROM_B here shows another way of formulating a referential constraint. In practice, of course, it would be simpler just to include the following foreign key specification as part of the definition of relvar SP:

     FOREIGN KEY { SNO } REFERENCES S

9.13 The following discussion relies on the fact that (as Appendix A explains in more detail) databases are really variables—i.e., we really need to draw a distinction between database values and database variables, analogous to that between relation values and relation variables. Let DBD1 and DBD2 be (logical) database designs; let DB1 and DB2 be database variables conforming to DBD1 and DBD2, respectively; and let db1 and db2 be the current values of DB1 and DB2, respectively. Further, let there exist mappings M12 and M21—i.e., sequences of relational algebra operations, loosely speaking—that transform db1 into db2 and db2 into db1, respectively. Then db1 and db2 are information equivalent, meaning that for every expression involving only relations from db1, there’s an expression involving only relations from db2 that yields the same result (and vice versa).

Now let database variables DB1 and DB2 be such that for every possible value db1 of DB1 there exists an information equivalent value db2 of DB2 (and vice versa). Then DB1 and DB2 per se are information equivalent, as are the corresponding designs DBD1 and DBD2.

Now let database variables DB1 and DB2, as well as their current values db1 and db2, be information equivalent. Let U1 be an update on DB1 that transforms db1 into db1′. Then there must exist an update U2 on DB2 that transforms db2 into db2′, such that db1′ and db2′ are information equivalent. Note that the remarks of this paragraph apply in particular to the case in which DB1 consists only of base relvars and DB2 consists only of views of relvars in DB1.

Finally, let database variables DB1 and DB2, as well as their current values db1 and db2, not be information equivalent. Then there must exist an expression involving only relations from db1 with no counterpart involving only relations from db2 (or vice versa), and there must exist an update on DB1 with no counterpart on DB2 (or vice versa)—speaking somewhat loosely in both cases. Again note that the remarks of this paragraph apply in particular to the case in which DB1 consists only of base relvars and DB2 consists only of views of relvars in DB1.

9.14 (You might want to review the section THE RELIANCE ON ATTRIBUTE NAMES in Chapter 6 before reading this answer.) Yes, views should indeed have been sufficient to solve the logical data independence problem. But the trouble with views as conventionally understood is that a view definition specifies both the application’s perception of some portion of the database and the mapping between that perception and the database “as it really is.” In order to achieve the kind of data independence I’m talking about here, those two specifications need to be kept separate (and the mapping specification in particular should be hidden from the user).



[207] Indeed, logical data independence is a strong argument in favor of allowing constraints in general to be defined for views as well as base relvars.

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

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