Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Tuple relational calculus
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Relational model}} '''Tuple calculus''' is a [[wikt:calculus|calculus]] that was created and introduced by [[Edgar F. Codd]] as part of the [[relational model]], in order to provide a [[Declarative programming|declarative]] database-query language for data manipulation in this [[data model]]. It formed the inspiration for the database-query languages [[QUEL query languages|QUEL]] and [[SQL]], of which the latter, although far less faithful to the original relational model and calculus, is now the de facto standard database-query language; a dialect of SQL is used by nearly every [[relational database management system|relational-database-management system]]. Michel Lacroix and Alain Pirotte proposed [[domain calculus]], which is closer to [[First-order predicate calculus|first-order logic]] and together with Codd showed that both of these calculi (as well as [[relational algebra]]) are equivalent in [[expressive power (computer science)|expressive power]].{{cn|date=April 2025}} Subsequently, query languages for the relational model were called ''relationally complete'' if they could express at least all of these queries. == Definition == === Relational database === Since the calculus is a [[query language]] for [[relational database]]s we first have to define a relational database. The basic relational building block is the [[Data domain|domain]] (somewhat similar, but not equal to, a [[data type]]). A [[tuple]] is a finite sequence of [[Attribute (computing)|attribute]]s, which are [[ordered pair]]s of domains and values. A [[relational model|relation]] is a set of (compatible) tuples. Although these relational concepts are mathematically defined, those definitions map loosely to traditional database concepts. A [[table (database)|table]] is an accepted visual representation of a relation; a tuple is similar to the concept of a [[row (database)|row]]. We first assume the existence of a set ''C'' of column names, examples of which are "name", "author", "address", etcetera. We define ''headers'' as finite subsets of ''C''. A ''relational database schema'' is defined as a [[tuple]] ''S'' = (''D'', ''R'', ''h'') where ''D'' is the domain of atomic values (see [[relational model]] for more on the notions of ''domain'' and ''atomic value''), ''R'' is a [[finite set]] of relation names, and :''h'' : ''R'' β 2<sup>''C''</sup> a [[function (mathematics)|function]] that associates a header with each relation name in ''R''. (Note that this is a simplification from the full relational model where there is more than one domain and a header is not just a set of column names but also maps these column names to a domain.) Given a domain ''D'' we define a ''tuple'' over ''D'' as a [[partial function]] that maps some column names to an atomic value in ''D''. An example would be (name : "Harry", age : 25). :''t'' : ''C'' βΈ ''D'' The set of all tuples over ''D'' is denoted as ''T''<sub>''D''</sub>. The subset of ''C'' for which a tuple ''t'' is defined is called the ''domain'' of ''t'' (not to be confused with the domain in the schema) and denoted as ''dom''(''t''). Finally we define a ''relational database'' given a schema ''S'' = (''D'', ''R'', ''h'') as a function :''db'' : ''R'' β 2<sup>''T''<sub>''D''</sub></sup> that maps the relation names in ''R'' to finite subsets of ''T''<sub>''D''</sub>, such that for every relation name ''r'' in ''R'' and tuple ''t'' in ''db''(''r'') it holds that :''dom''(''t'') = ''h''(''r''). The latter requirement simply says that all the tuples in a relation should contain the same column names, namely those defined for it in the schema. === Atoms === For the construction of the formulas we will assume an infinite set ''V'' of tuple variables. The formulas are defined given a database schema ''S'' = (''D'', ''R'', ''h'') and a partial function ''type'' : ''V'' βΈ 2<sup>''C''</sup>, called at ''type assignment'', that assigns headers to some tuple variables. We then define the ''set of [[atomic formula]]s'' ''A''[''S'',''type''] with the following rules: # if ''v'' and ''w'' in ''V'', ''a'' in ''type''(''v'') and ''b'' in ''type''(''w'') then the formula ''v''.''a'' = ''w''.''b'' is in ''A''[''S'',''type''], # if ''v'' in ''V'', ''a'' in ''type''(''v'') and ''k'' denotes a value in ''D'' then the formula ''v''.''a'' = ''k'' is in ''A''[''S'',''type''], and # if ''v'' in ''V'', ''r'' in ''R'' and ''type''(''v'') = ''h''(''r'') then the formula ''r''(''v'') is in ''A''[''S'',''type'']. Examples of atoms are: * (''t''.age = ''s''.age) β ''t'' has an age attribute and ''s'' has an age attribute with the same value * (''t''.name = "Codd") β tuple ''t'' has a name attribute and its value is "Codd" * Book(''t'') β tuple ''t'' is present in relation Book. The formal semantics of such atoms is defined given a database ''db'' over ''S'' and a tuple variable binding ''val'' : ''V'' β ''T''<sub>''D''</sub> that maps tuple variables to tuples over the domain in ''S'': # ''v''.''a'' = ''w''.''b'' is true if and only if ''val''(''v'')(''a'') = ''val''(''w'')(''b'') # ''v''.''a'' = ''k'' is true if and only if ''val''(''v'')(''a'') = ''k'' # ''r''(''v'') is true if and only if ''val''(''v'') is in ''db''(''r'') === Formulas === The atoms can be combined into formulas, as is usual in first-order logic, with the logical operators β§ (and), β¨ (or) and Β¬ (not), and we can use the existential quantifier (β) and the universal quantifier (β) to bind the variables. We define the ''set of formulas'' ''F''[''S'',''type''] inductively with the following rules: # every atom in ''A''[''S'',''type''] is also in ''F''[''S'',''type''] # if ''f''<sub>1</sub> and ''f''<sub>2</sub> are in ''F''[''S'',''type''] then the formula ''f''<sub>1</sub> β§ ''f''<sub>2</sub> is also in ''F''[''S'',''type''] # if ''f''<sub>1</sub> and ''f''<sub>2</sub> are in ''F''[''S'',''type''] then the formula ''f''<sub>1</sub> β¨ ''f''<sub>2</sub> is also in ''F''[''S'',''type''] # if ''f'' is in ''F''[''S'',''type''] then the formula Β¬ ''f'' is also in ''F''[''S'',''type''] # if ''v'' in ''V'', ''H'' a header and ''f'' a formula in ''F''[''S'',''type''<sub>[''v''->''H'']</sub>] then the formula β ''v'' : ''H'' ( ''f'' ) is also in ''F''[''S'',''type''], where ''type''<sub>[''v''->''H'']</sub> denotes the function that is equal to ''type'' except that it maps ''v'' to ''H'', # if ''v'' in ''V'', ''H'' a header and ''f'' a formula in ''F''[''S'',''type''<sub>[''v''->''H'']</sub>] then the formula β ''v'' : ''H'' ( ''f'' ) is also in ''F''[''S'',''type''] Examples of formulas: * ''t''.name = "C. J. Date" β¨ ''t''.name = "H. Darwen" * Book(''t'') β¨ Magazine(''t'') * β ''t'' : {author, title, subject} ( Β¬ ( Book(''t'') β§ ''t''.author = "C. J. Date" β§ Β¬ ( ''t''.subject = "relational model"))) Note that the last formula states that all books that are written by C. J. Date have as their subject the relational model. As usual we omit brackets if this causes no ambiguity about the semantics of the formula. We will assume that the quantifiers quantify over the universe of all tuples over the domain in the schema. This leads to the following formal semantics for formulas given a database ''db'' over ''S'' and a tuple variable binding ''val'' : ''V'' -> ''T''<sub>''D''</sub>: # ''f''<sub>1</sub> β§ ''f''<sub>2</sub> is true if and only if ''f''<sub>1</sub> is true and ''f''<sub>2</sub> is true, # ''f''<sub>1</sub> β¨ ''f''<sub>2</sub> is true if and only if ''f''<sub>1</sub> is true or ''f''<sub>2</sub> is true or both are true, # Β¬ ''f'' is true if and only if ''f'' is not true, # β ''v'' : ''H'' ( ''f'' ) is true if and only if there is a tuple ''t'' over ''D'' such that ''dom''(''t'') = ''H'' and the formula ''f'' is true for ''val''<sub>[''v''->''t'']</sub>, and # β ''v'' : ''H'' ( ''f'' ) is true if and only if for all tuples ''t'' over ''D'' such that ''dom''(''t'') = ''H'' the formula ''f'' is true for ''val''<sub>[''v''->''t'']</sub>. === Queries === Finally we define what a query expression looks like given a schema ''S'' = (''D'', ''R'', ''h''): : { ''v'' : ''H'' | ''f''(''v'') } where ''v'' is a tuple variable, ''H'' a header and ''f''(''v'') a formula in ''F''[''S'',''type''] where ''type'' = { (''v'', ''H'') } and with ''v'' as its only free variable. The result of such a query for a given database ''db'' over ''S'' is the set of all tuples ''t'' over ''D'' with ''dom''(''t'') = ''H'' such that ''f'' is true for ''db'' and ''val'' = { (''v'', ''t'') }. Examples of query expressions are: * { ''t'' : {name} | β ''s'' : {name, wage} ( Employee(''s'') β§ ''s''.wage = 50.000 β§ ''t''.name = ''s''.name ) } * { ''t'' : {supplier, article} | β ''s'' : {s#, sname} ( Supplier(''s'') β§ ''s''.sname = ''t''.supplier β§ β ''p'' : {p#, pname} ( Product(''p'') β§ ''p''.pname = ''t''.article β§ β ''a'' : {s#, p#} ( Supplies(''a'') β§ ''s''.s# = ''a''.s# β§ ''a''.p# = ''p''.p# ))) } == Semantic and syntactic restriction == === Domain-independent queries === Because the semantics of the quantifiers is such that they quantify over all the tuples over the domain in the schema it can be that a query may return a different result for a certain database if another schema is presumed. For example, consider the two schemas ''S<sub>1</sub>'' = ( ''D<sub>1</sub>'', ''R'', ''h'' ) and ''S<sub>2</sub>'' = ( ''D<sub>2</sub>'', ''R'', ''h'' ) with domains ''D<sub>1</sub>'' = { 1 }, ''D<sub>2</sub>'' = { 1, 2 }, relation names ''R'' = { "r<sub>1</sub>" } and headers ''h'' = { ("r<sub>1</sub>", {"a"}) }. Both schemas have a common instance: : ''db'' = { ( "r<sub>1</sub>", { ("a", 1) } ) } If we consider the following query expression : { ''t'' : {a} | ''t''.a = ''t''.a } then its result on ''db'' is either { (a : 1) } under ''S<sub>1</sub>'' or { (a : 1), (a : 2) } under ''S<sub>2</sub>''. It will also be clear that if we take the domain to be an infinite set, then the result of the query will also be infinite. To solve these problems we will restrict our attention to those queries that are ''domain independent'', i.e., the queries that return the same result for a database under all of its schemas. An interesting property of these queries is that if we assume that the tuple variables range over tuples over the so-called ''active domain'' of the database, which is the subset of the domain that occurs in at least one tuple in the database or in the query expression, then the semantics of the query expressions does not change. In fact, in many definitions of the tuple calculus this is how the semantics of the quantifiers is defined, which makes all queries by definition domain independent. === Safe queries === In order to limit the query expressions such that they express only domain-independent queries a syntactical notion of ''safe query'' is usually introduced. To determine whether a query expression is safe we will derive two types of information from a query. The first is whether a variable-column pair ''t''.''a'' is ''bound'' to the column of a relation or a constant, and the second is whether two variable-column pairs are directly or indirectly equated (denoted ''t''.''v'' == ''s''.''w''). For deriving boundedness we introduce the following reasoning rules: # in " ''v''.''a'' = ''w''.''b'' " no variable-column pair is bound, # in " ''v''.''a'' = ''k'' " the variable-column pair ''v''.''a'' is bound, # in " ''r''(''v'') " all pairs ''v''.''a'' are bound for ''a'' in ''type''(''v''), # in " ''f''<sub>1</sub> β§ ''f''<sub>2</sub> " all pairs are bound that are bound either in ''f''<sub>1</sub> or in ''f''<sub>2</sub>, # in " ''f''<sub>1</sub> β¨ ''f''<sub>2</sub> " all pairs are bound that are bound both in ''f''<sub>1</sub> and in ''f''<sub>2</sub>, # in " Β¬ ''f'' " no pairs are bound, # in " β ''v'' : ''H'' ( ''f'' ) " a pair ''w''.''a'' is bound if it is bound in ''f'' and ''w'' <> ''v'', and # in " β ''v'' : ''H'' ( ''f'' ) " a pair ''w''.''a'' is bound if it is bound in ''f'' and ''w'' <> ''v''. For deriving equatedness we introduce the following reasoning rules (next to the usual reasoning rules for equivalence relations: reflexivity, symmetry and transitivity): # in " ''v''.''a'' = ''w''.''b'' " it holds that ''v''.''a'' == ''w''.''b'', # in " ''v''.''a'' = ''k'' " no pairs are equated, # in " ''r''(''v'') " no pairs are equated, # in " ''f''<sub>1</sub> β§ ''f''<sub>2</sub> " it holds that ''v''.''a'' == ''w''.''b'' if it holds either in ''f''<sub>1</sub> or in ''f''<sub>2</sub>, # in " ''f''<sub>1</sub> β¨ ''f''<sub>2</sub> " it holds that ''v''.''a'' == ''w''.''b'' if it holds both in ''f''<sub>1</sub> and in ''f''<sub>2</sub>, # in " Β¬ ''f'' " no pairs are equated, # in " β ''v'' : ''H'' ( ''f'' ) " it holds that ''w''.''a'' == ''x''.''b'' if it holds in ''f'' and ''w''<>''v'' and ''x''<>''v'', and # in " β ''v'' : ''H'' ( ''f'' ) " it holds that ''w''.''a'' == ''x''.''b'' if it holds in ''f'' and ''w''<>''v'' and ''x''<>''v''. We then say that a query expression { v : H | f(v) } is ''safe'' if * for every column name ''a'' in ''H'' we can derive that ''v''.''a'' is equated with a bound pair in ''f'', * for every subexpression of ''f'' of the form " β ''w'' : ''G'' ( ''g'' ) " we can derive that for every column name ''a'' in ''G'' we can derive that ''w''.''a'' is equated with a bound pair in ''g'', and * for every subexpression of ''f'' of the form " β ''w'' : ''G'' ( ''g'' ) " we can derive that for every column name ''a'' in ''G'' we can derive that ''w''.''a'' is equated with a bound pair in ''g''. The restriction to safe query expressions does not limit the expressiveness since all domain-independent queries that could be expressed can also be expressed by a safe query expression. This can be proven by showing that for a schema ''S'' = (''D'', ''R'', ''h''), a given set ''K'' of constants in the query expression, a tuple variable ''v'' and a header ''H'' we can construct a safe formula for every pair ''v''.''a'' with ''a'' in ''H'' that states that its value is in the active domain. For example, assume that ''K''={1,2}, ''R''={"r"} and ''h'' = { ("r", {"a, "b"}) } then the corresponding safe formula for ''v''.b is: : ''v''.b = 1 β¨ ''v''.b = 2 β¨ β ''w'' ( r(w) β§ ( ''v''.b = ''w''.a β¨ ''v''.b = ''w''.b ) ) This formula, then, can be used to rewrite any unsafe query expression to an equivalent safe query expression by adding such a formula for every variable ''v'' and column name ''a'' in its type where it is used in the expression. Effectively this means that we let all variables range over the active domain, which, as was already explained, does not change the semantics if the expressed query is domain independent. == Systems == * [http://des.sourceforge.net DES β An educational tool for working with Tuple Relational Calculus and other formal languages] * [https://winrdbi.asu.edu/ WinRDBI β An educational tool for working with Tuple Relational Calculus and other formal languages] == See also == * [[Relational algebra]] * [[Relational calculus]] <!--*[[Tuple calculus]]--> ** [[Domain relational calculus]] (DRC) == References == {{Reflist}} *{{cite journal |last1=Codd |first1=E. F. |author-link=Edgar F. Codd |title=A relational model of data for large shared data banks |journal=[[Communications of the ACM]] |date=June 1970 |volume=13 |issue=6 |pages=377β387 |doi=10.1145/362384.362685}} [[Category:Relational model]] [[Category:Logical calculi]] [[de:KalkΓΌl (Datenbank)]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite journal
(
edit
)
Template:Cn
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)