home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / database / 6317 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  5.8 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!jvnc.net!yale.edu!ira.uka.de!chx400!bernina!neptune!nugget.inf.ethz.ch!marti
  2. From: marti@nugget.inf.ethz.ch (Robert Marti)
  3. Newsgroups: comp.databases
  4. Subject: Re: nested SQL select
  5. Message-ID: <1992Aug25.161811.6054@neptune.inf.ethz.ch>
  6. Date: 25 Aug 92 16:18:11 GMT
  7. References: <1992Aug14.071343.1758@neptune.inf.ethz.ch> <l990h3INN7it@jethro.Corp.Sun.COM>
  8. Sender: news@neptune.inf.ethz.ch (Mr News)
  9. Organization: Dept. Informatik, Swiss Federal Institute of Technology (ETH), Zurich, CH
  10. Lines: 110
  11. Nntp-Posting-Host: nugget.inf.ethz.ch
  12.  
  13. Editorial comment:  This posting is more of an academic exercise
  14. than of practical interest.  All I am trying to show is that any
  15. _pure_ relational algebra expression can be translated into a
  16. single SQL SELECT-statement -- for what it's worth ...
  17.  
  18. In article <l990h3INN7it@jethro.Corp.Sun.COM>, jdr@starflight.Corp.Sun.COM
  19. (Jon Roland) writes:
  20. jr> Relational algebra allows expressions which have no SQL
  21. jr> equivalents, because SQL is not an implementation of relational
  22. jr> theory, and doesn't threaten to become one in the future.
  23.  
  24. rm> An interesting statement.  Could you give me a few examples of
  25. rm> relational algebra expressions which have no SQL equivalent?
  26. rm> (Please only use operators from the set {selection, projection,
  27. rm> union, difference, cartesian product}.  This set of operators
  28. rm> has been shown to be complete.)
  29.  
  30. jr> You discussed one in your own posting.
  31.  
  32. Sigh.  No, I didn't.  But I assume that you refer to the following
  33. example in an earlier posting by Paul Singleton:
  34.  
  35. ps> I have a table T(A,B,C) and I want effectively to
  36. ps>   select A, count(B) from ( project A, B from T ) group by A
  37.  
  38. As I pointed out in my previous posting, this is _not_ a relational
  39. algebra (RA) expression -- strictly speaking:  "count" and "group by"
  40. are certainly useful operators, but they are _not_ part of RA.
  41. Indeed, in terms of RA, Paul Singleton's expression should
  42. probably look somewhat like
  43.  
  44.   project[A,count(B)]( group_by[A]( project[A,B](T) ) )
  45.   ^^^^^^^
  46.  
  47. (The select- (sometimes called restrict-) operator requires a selection
  48. predicate and an RA expression as parameters and filters out the tuples
  49. which do not fulfill the selection predicate.)
  50.  
  51. jr> A single relational algebra
  52. jr> expression can be composed that does not translate into a single SQL
  53. jr> statement, but requires several of them. Granted, it does translate,
  54. jr> but so clumsily that one has to complain.
  55.  
  56. Like I said before, it has been shown that the five operators
  57.  
  58.   select[ <selection-predicate> ]( <RA-expr> )
  59.   project[ <attribute-list> ]( <RA-expr> )
  60.   <RA-expr> * <RA-expr>                      <--- cartesian product
  61.   <RA-expr> + <RA-expr>                      <--- union
  62.   <RA-expr> - <RA-expr>                      <--- difference
  63.  
  64. are complete, i.e., that any relational algebra expression can be
  65. expressed using just these five operators.  Now, I will give an outline
  66. of an algorithm which shows why I believe that any RA expression _as_
  67. _defined_above_ can _always_ be translated into a single SQL statement:
  68.  
  69. First, we transform the RA-expression into an equivalent RA-expression
  70. in which the cartesion products will be performed before selections and
  71. projections, and they in turn will be performed before unions and
  72. differences.  This can be done using equivalence laws such as
  73. - moving union and difference "outward":
  74.   select[...](R + S)   ---->  select[...](R) + select[...](S)
  75.   select[...](R - S)   ---->  select[...](R) - select[...](S)
  76.   project[...](R + S)  ---->  project[...](R) + project[...](S)
  77.   project[...](R - S)  ---->  project[...](R) - project[...](S)
  78.   (R + S) * T          ---->  R*T + S*T
  79.   (R - S) * T          ---->  R*T - S*T
  80. - and moving selection and projection "outward":
  81.   select[...](R) * S   ---->  select[...](R * S)
  82.   project[...](R) * S  ---->  project[...](R * S)
  83.  
  84. Then, translate the parts between union and/or difference operators
  85. separately (see below) and concatenate the results using the SQL-
  86. UNION and WHERE NOT EXISTS constructs.  For each part between union
  87. and/or difference, the attributes in the projection lists end up in
  88. the SQL SELECT-clause (actually, a SELECT DISTINCT-clause, see below),
  89. the selection predicates of the select operators end up as conjunction
  90. in the SQL WHERE-clause and the names listed as arguments of the product-
  91. operator end up in the SQL FROM-clause.   (Don't forget to introduce
  92. aliases for table names if required ... )  I hope this also answers
  93. Paul Singleton's question:
  94. ps> Is there a general algorithm for translating relational algebra
  95. ps> expressions into SQL?
  96.  
  97. Jon Roland goes on to say:
  98. jr> More serious, however, is the fact that in relational algebra operators
  99. jr> applied to relations yield relations, whereas in SQL they do not always
  100. jr> do so. To be a relation, a table's rows must be unique, and SQL can
  101. jr> yield tables with redundant rows.
  102.  
  103. Sigh.  The question was whether you could come up with a legal RA expression
  104. which could _not_ be translated into SQL.  You instead tell me that there
  105. are SQL expressions which have no equivalent in RA.  Yes, there are, but
  106. that wasn't my point.
  107.  
  108. jr> There is no really good substitute for Turing-computable languages, even
  109. jr> only for use in data access.
  110.  
  111. Well, one of the nice things of languages which are not Turing-complete
  112. (e.g., SQL or even slightly more powerful languages such as Datalog with
  113. stratified negation) is that all statements that you can write in these
  114. languages will return an answer in a finite amount of time.  Many people
  115. view this as a desireable property in the typical database environment
  116. with potentially many users competing for resources.
  117.  
  118. -- 
  119. Robert Marti                    |  Phone:    +41 1 254 72 60
  120. Informationssysteme             |  FAX:      +41 1 262 39 73
  121. ETH-Zentrum                     |  E-Mail:   marti@inf.ethz.ch
  122. CH-8092 Zurich, Switzerland     |
  123.