Difference between Relational Algebra and Relational calculus
Asked Answered
R

3

14

What is the exact difference between relational algebra and relational calculus? At most references, it will be

Relational algebra is procedural and calculus is non-procedural.

What do these terms mean?

We can solve all problems using relational algebra. So why we would use relational calculus?

Rightward answered 29/9, 2015 at 6:40 Comment(1)
What are "these"? Algebra/calculus? Procedural/non-procedural?Bougainville
B
7

TL;DR: Queries calling RA (relational algebra) operators & queries of the two relational calculi (RCs) TRC (tuple RC) & DRC (domain RC) are different syntax for the same thing: a relation value or the property/condition that a relation value's tuples have to meet. As is SQL (a mix(up) of them). As is the predicate calculus, the language of precision in mathematics, logic, science (including computer science) & engineering (including software engineering). And RA as procedural vs RCs as declarative is a myth.


A relation holds the tuples that make some predicate--statement template parameterized by attributes--into a true proposition--statement.

/* tuples where employee PERSONNAME lives on STREET in CITY */
Employee
/* tuples where employee PERSONNAME works at COMPANY for $SALARY */
WorksFor

A RA-style query expression involves attribute names, relation variable/constant names, relation literals (involving attribute names & values) & relation operators. The operators are JOIN, UNION, MINUS, PROJECT, RESTRICT, etc. It denotes the relation value that you get by evaluating the expression. But it is also requirements for the value to meet.

/* RA query for tuples where
    FOR SOME STREET & CITY [employee PERSONNAME lives on STREET in CITY]
AND NOT FOR SOME COMPANY & SALARY
        [employee PERSONNAME works at COMPANY for $SALARY AND COMPANY = 'FBC']
*/
    PROJECT PERSONNAME (Employee)
MINUS PROJECT PERSONNAME (RESTRICT COMPANY = 'FBC' (WorksFor))

A RC expression is set-builder notation for a relation value. It involves a predicate with relation variable/constant names, attribute names & values, predicate operators & quantified names (logic variables). The operators are AND, OR, NOT, FOR SOME/ALL and =. It is usually seen as requirements for the value to meet. But it also denotes the relation value that you get by evaluating the expression or a certain equivalent one.

The DRC has quantified names that are attributes. We use a shorthand for statements with one parameter per attribute:

Employee(PERSONNAME, STREET, CITY)
    means (PERSONNAME, STREET, CITY) IN Employee
    means employee PERSONNAME lives on STREET in CITY
WorksFor(PERSONNAME, COMPANY, SALARY)
    means (PERSONNAME, COMPANY, SALARY) IN WorksFor
    means employee PERSONNAME works at COMPANY for $SALARY

/* DRC query for the same tuples as the RA query above */
tuples like (PERSONNAME) where
    FOR SOME STREET & CITY [Employee(PERSONNAME, STREET, CITY)]
AND NOT FOR SOME COMPANY & SALARY
        [WorksFor(PERSONNAME, COMPANY, SALARY) AND COMPANY = 'FBC']

The TRC has quantified names that are tuples. We dot a name to get the value associated with an attribute name in it. (Like for a field of a programming language record.) We use a shorthand for statements with one parameter (a tuple):

Employee(T)
    means T IN Employee
    means employee T.PERSONNAME lives on T.STREET in T.CITY
Worksfor(T)
    means T IN Worksfor
    means employee T.PERSONNAME works at T.COMPANY for $T.SALARY

/* TRC query for the same tuples as the RA query above */
tuples equal to some tuple T like (PERSONNAME) where
    FOR SOME E [Employee(E) AND E.PERSONNAME = T.PERSONNAME]
AND NOT FOR SOME W [
        WorksFor(W)
    AND W.COMPANY = 'FBC'
    AND E.PERSONNAME = T.PERSONNAME
    ]

(A few variants on the originals of the RA and RCs are taught. Eg some identify arguments by order and others by name. Sometimes extra capabilities are added. Eg allowing a function call in a RC is as expressive as allowing a certain relation constant plus operator R RENAME A TO N in a RA.)

However, there is a correspondence between RA operators and RC operators & between RA expressions and RC expressions:

If:
• R -- holds tuples where r
• S -- holds tuples where s
then:
• R JOIN S holds tuples where r AND s
• R UNION S holds tuples where r OR s
• R MINUS S holds tuples where r AND NOT s
• R PROJECT columns to keep holds tuples where FOR SOME columns to drop, r
• R RESTRICT condition holds tuples where r AND condition

A RA expression's value is the tuples that satisfy the corresponding RC expression.

If we want to map from a RC expression to an RA expression on an operator-by-operator basis then we need extended RA operators: one generalizing UNION & one corresponding to NOT. Those aren't operators we would like to actually use in an implementation--the relations values returned are in a certain sense undesirably big. But every RC expression using them can be rearranged mechanically to a normal form that uses only basic RA operators.

So: RA as procedural vs RCs as declarative is a myth. Every RA operator has a corresponding RC operator, every RC operator has a (possibly extended) corresponding RA operator, and every expression of one has (in basic and extended senses) a corresponding expression of the other. They are two notations for the same things. An expression of either can be taken as "procedural" by executing it as parsed or normalized and as "declarative" by executing it otherwise. The myth is trying to capture the idea that a RC expression isn't operator-by-operator like an expression using basic RA operators. But a RC expression does identify its non-obvious normal form's RA expression using basic operators. And it is operator-by-operator like a RA expression including extended operators.

(The myth may be helped because of the history of the words. "Modern algebra" has expressions with operators taking & giving values and can be calculated. "The calculus" aka analysis--differentiation & integration--has expressions describing values via impossible-to-calculate infinite limits & summations. We calculate in other ways, in general only calculating approximations.)

(Also, ironically: "The predicate calculus" is considered to specify things "declaratively" without regard to how they may be otherwise calculated or estimated. But the standard semantics/meaning of such an expression is given by following an algorithm that walks the expression tree. So it has an obvious "procedural" interpretation.)

Bougainville answered 29/9, 2015 at 10:6 Comment(8)
@philipyx Excellent answer. It's funny to see RA often described in RDBMS literature as "procedural". I know 5 years have passed, but could you elaborate a bit on "As is SQL (a mix(up) of them)." Which parts of SQL are RA and which RC in your view?Polished
(EXISTS and ANY might be more from the RC side for example?)Polished
@Polished Re 'mix': Inner Join vs Natural Join vs USING clause: are there any advantages? Re 'procedural': Back when 'procedural' was used (to be forever parroted) 'functional programming' hadn't been coined & the word was used not in the sense of state-oriented vs statelessness+functors, but in the sense of executing per a parse tree vs first needing prenex NF, as in expressions being nested calls of operators of an algebra vs having a calculus of equivalenced arbitrary expressions).Bougainville
@Polished "SQL is a mix" is informal. This answer connects PC, RA, TRC & DRC. The latter 2 are PC variants--"safe" queries quantifying over tuples vs columns. (There are many variants of PC, TRC & DRC.) In SQL you can see RA (JOIN, SELECT AS) & TRC (t.c, EXISTS (...)). My comment link maps SQL to PC & DRC by using dot in column names; but one could map to TRC, quantifying over tuples & using dot as tuple element access. PS Arbitrary boolean expressions in RA calls are of type function-from-tuple-to-boolean. Presentations not saying so are of languages & don't typically give a full RA.Bougainville
Thanks. So in your view "algebra" is characterized by being executed/calculated by traversal that is close to the surface syntax, and "calculus" implies having to first interpret the syntax by means of equivalences/inferences? (cont'd)Polished
While there exists a clear definition of "an algebra", the word "calculus" seems to me much less well-defined. I've looked into the history of proof methods where the term calculus seems more aligned with Frege/Hilbert style, but the line is blurry. Boole's system (Boolean algebra) is sometimes referred to as a calculus, for example. (cont'd)Polished
Seems to me Codd is in any case most interested in PC/RC as a specification syntax, not as a separate system of deduction. His intention is for RC to be translated into RA. So my conclusion is that he took the terms from the algebra of sets and predicate calculus, but without there being a super-clear definition of what exactly "a calculus" is, hence a lot of confusion (is "calculus" more or less declarative etc). Not implying Codd didn't understand these topics deeply, of course.Polished
@Polished The 2 terms just don't have precise generic meanings & they have multiple specific uses. Codd just happened to use those terms, their use doesn't imply anything precise about what he described using them. Names denote, they don't define. Codd didn't use the terms until 2 1971 papers--"algebraic view of queries [...] operation of a relational algebra" in Further Nomalization of the Data Base Relational Model & "an applied predicate calculus--the relational calculus" in Relational Completeness of Data Base Sublanguages. (So the inspiring calculus was at least PC.)Bougainville
Z
3

Relational algebra deals with more specific set expressions, join operations, and set combinations while relational calculus mostly sticks to AND-OR relations and either the existential (There exists an x such that [condition(x)]) or the universal (For all x's, [condition(x)]) quantifiers. Relational algebraic expressions are similar to an assembly language in functionality and specificity while relational calculus expressions are closer to a high-level programming language in appearance and functionality.

This presentation from an NYU class was helpful to me.

Zsolway answered 29/9, 2015 at 7:41 Comment(0)
J
-1

Difference between the Relational algebra and the Relational Calculus

  1. Relational algebra operations manipulate some relations and provide some expression in the form of queries where as relational calculus are formed queries on the basis of pairs of expressions.

  2. RA have operator like join, union, intersection, division, difference, projection, selection etc. where as RC has tuples and domain oriented expressions.

  3. RA is procedural language where as RC is non procedural query system.

  4. Expressive power of RA and RC are equivalent. This means any query that could be expressed in RA could be expressed by formula in RC.

  5. Any KC formula is translated in Algebric query.

  6. There is modification which is easy in queries in RA than the RC.

  7. RA formed the mathematical form and have no specificjuer1 language RC also has mathematical form but has one query language QUEL.

  8. Relational algebra is easy to manipulate and understand than RC.

  9. RA queries are more powerful than the RC.

  10. RC are formed WFFs where as RA does not form any formula.

  11. RA is a procedural. That means we have write some conditions in order.

  12. RC is a Non procedural. In here we have write the conditions in any order.

Example:-

Library Example:-

Book is a relation it have a following attributes.

      1. Book Name
      2. Accession Number
      3. Year of publication

Problem:-

 Find out the all book names published in the year 2000

Relational Algebra:-

   -------------------------------------------------------
   |                       (σ            (book) )        |
   |       ¶                 (yr_pub=2000)               |
   |        (Book name)                                  |
    ------------------------------------------------------

Relational calculus:-

    S = { t | ∃ u ∈  book (t[book name] = u[book name]) ∧ (u[yr_pub] = 2000 ) }

In this relational calculus we can write the condition in any where like the below.

    S = { t | ∃ u ∈  book  (u[yr_pub] = 2000 ) ∧ (t[book name] = u[book name])  }

But in relational algebra, we have to find first what are the tuples have the year of publication is 2000, then only we have to extract the book name column.

Joo answered 1/10, 2015 at 5:27 Comment(1)
Aren't you use different forms of relation representation for RC and RA here? I.e. as I can see for RC you use kinda normalized form with t (book text) and u (book publication) relations while in RA you use the only one relation that already represent book with its publication year. I.e. you'd have to write ¶_{name} (book ▷◁_{name = name} σ_{year=2000} (publication)) for RA or S = { t | u ∈ book (u[yr_pub] = 2000 ) } for RC.Conversable

© 2022 - 2024 — McMap. All rights reserved.