O'Reilly logo

SQL and Relational Theory, 2nd Edition by C.J. Date

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A NOTE ON RECURSION

Consider the following edited extract from Exercise 5.16 in Chapter 5:

The well known bill of materials application involves a relvar—PP, say—showing which parts contain which parts as immediate components. Of course, immediate components are themselves parts, and they can have further immediate components of their own.

Figure 7-5 below shows (a) a sample value for that relvar PP and (b) the corresponding transitive closure TC.[108] The predicates are as follows:

  • PP: Part PX contains part PY as an immediate component.

  • TC: Part PX contains part PY as a component at some level (not necessarily immediate).

Relvars PP and TC (sample values)

Figure 7-5. Relvars PP and TC (sample values)

Given a (relation) value pp for relvar PP, the relation tc that’s the transitive closure of pp can be defined as follows:

Definition: The pair (px,py) appears in tc if and only if:

  1. It appears in pp, or

  2. There exists some pz such that the pair (px,pz) appears in pp and the pair (pz,py) appears in tc.

In other words, if we think of pp as representing a directed graph, with a node for each part and an arc from each node to each corresponding immediate component node, then (px,py) appears in the transitive closure if and only if there’s a path in that graph from node px to node py. Observe that the definition involves a recursive reference to relation tc.

Aside: In practice relvar PP would probably have a QTY attribute as well (showing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required