My understanding about Codd's concept of "safe queries" was created to ensure that a query would always terminate. One key ability of a Turing machine is that it can work on infinite calculations (and thus isn't guaranteed to terminate). If the safe query restriction were removed, would relational calculus be Turing-complete since that means it doesn't have to terminate?
-
3Nice question! BTW: David Fetter of PostgreSQL recently sketched out an embedding of a Cyclic Tag System (which is known to be Turing-complete) in pure SQL:2008, thus proving that it is Turing-complete. (The missing pieces compared to earlier versions were Common Table Expressions and Windowing.) Note: I am well aware of the fact that SQL and the Relational Calculus are very different, but I thought it would be an interesting datapoint. – Jörg W Mittag Mar 27 '10 at 15:55
-
3Another thing: just because something allows writing non-terminating programs, that doesn't necessarily mean it's Turing-complete. Example: a programming language that has an infinite loop as its only construct. – Jörg W Mittag Mar 27 '10 at 15:56
2 Answers
I don't know if this is correct, but I have a hypothesis. If we allowed free variables in the formula, then we could think of queries as functions. For example, consider the following query:
$$ \left\{ \left\langle A, B, C \right\rangle \mid \left\langle A, B, C \right\rangle \in \mathbb{R} \land A = x\right\} $$
If the above query has a result $M$, we can consider that to be a function of the form $ \lambda x.M $. If we allow relations to be free variables, we can define a function of the form $ \lambda \mathbb{R}.\mathbb{R} $. Now, if we allow "higher-order queries", ie queries that can query queries, we can represent the Church numerals (with $q$ being a query): $$ \begin{aligned} 0 &= \lambda q\mathbb{R}.\mathbb{R} \\ 1 &= \lambda q\mathbb{R}.q \;\; \mathbb{R} \\ 2 &= \lambda q\mathbb{R}.q \;\; \left(q \;\; \mathbb{R} \right) \\ 3 &= \lambda q\mathbb{R}.q \;\; \left(q \;\; \left(q \;\; \mathbb{R} \right)\right) \end{aligned} $$ Therefore, I think that relational calculus can also be a $ \lambda $ calculus, and therefore be Turing-complete. Can anyone tell me if I'm on the right path?
The notion of "safe queries" in relational calculus is indeed related to ensuring that queries terminate, as well as preventing certain undesirable side effects such as modifying the database state. However, allowing unsafe queries alone would not be enough to make relational calculus Turing-complete.
Turing-completeness requires that a system or language be able to perform any computation that can be performed by a Turing machine, which includes infinite computations that may not terminate. Simply allowing queries to not terminate would not be enough to make relational calculus Turing-complete, as it would still lack the ability to perform other types of computations that a Turing machine can do, such as conditional branching and looping.
That being said, there are extensions to relational calculus that do increase its expressive power and bring it closer to Turing-completeness, such as adding recursion or support for user-defined functions. These extensions do allow for more complex computations, but they still may not be able to perform all computations that a Turing machine can do.

- 97