What does the term “Tuple” Mean in Relational Databases?

前端 未结 10 1883
青春惊慌失措
青春惊慌失措 2021-01-29 21:31

Please explain what is meant by tuples in sql?Thanks..

相关标签:
10条回答
  • 2021-01-29 21:54

    row from a database table

    0 讨论(0)
  • 2021-01-29 22:00

    As I understand it a table has a set K of keys and a typing function T with domain K. A row, or "tuple", of the table is a function r with domain K such that r(k) is an element of T(k) for each key k. So the terminology is misleading in that a "tuple" is really more like an associative array.

    0 讨论(0)
  • 2021-01-29 22:03

    Most of the answers here are on the right track. However, a row is not a tuple. Tuples* are unordered sets of known values with names. Thus, the following tuples are the same thing (I'm using an imaginary tuple syntax since a relational tuple is largely a theoretical construct):

    (x=1, y=2, z=3)
    (z=3, y=2, x=1)
    (y=2, z=3, x=1)
    

    ...assuming of course that x, y, and z are all integers. Also note that there is no such thing as a "duplicate" tuple. Thus, not only are the above equal, they're the same thing. Lastly, tuples can only contain known values (thus, no nulls).

    A row** is an ordered set of known or unknown values with names (although they may be omitted). Therefore, the following comparisons return false in SQL:

    (1, 2, 3) = (3, 2, 1)
    (3, 1, 2) = (2, 1, 3)
    

    Note that there are ways to "fake it" though. For example, consider this INSERT statement:

    INSERT INTO point VALUES (1, 2, 3)
    

    Assuming that x is first, y is second, and z is third, this query may be rewritten like this:

    INSERT INTO point (x, y, z) VALUES (1, 2, 3)
    

    Or this:

    INSERT INTO point (y, z, x) VALUES (2, 3, 1)
    

    ...but all we're really doing is changing the ordering rather than removing it.

    And also note that there may be unknown values as well. Thus, you may have rows with unknown values:

    (1, 2, NULL) = (1, 2, NULL)
    

    ...but note that this comparison will always yield UNKNOWN. After all, how can you know whether two unknown values are equal?

    And lastly, rows may be duplicated. In other words, (1, 2) and (1, 2) may compare to be equal, but that doesn't necessarily mean that they're the same thing.

    If this is a subject that interests you, I'd highly recommend reading SQL and Relational Theory: How to Write Accurate SQL Code by CJ Date.

    * Note that I'm talking about tuples as they exist in the relational model, which is a bit different from mathematics in general.

    **And just in case you're wondering, just about everything in SQL is a row or table. Therefore, (1, 2) is a row, while VALUES (1, 2) is a table (with one row).

    UPDATE: I've expanded a little bit on this answer in a blog post here.

    0 讨论(0)
  • 2021-01-29 22:06

    tuple = 1 record; n-tuple = ordered list of 'n' records; Elmasri Navathe book (page 198 3rd edition).

    record = either ordered or unordered.

    0 讨论(0)
  • 2021-01-29 22:08

    A tuple is used to define a slice of data from a cube; it is composed of an ordered collection of one member from one or more dimensions. A tuple is used to identify specific sections of multidimensional data from a cube; a tuple composed of one member from each dimension in a cube completely describes a cell value.

    0 讨论(0)
  • 2021-01-29 22:11

    In relational databases, tables are relations (in mathematical meaning). Relations are sets of tuples. Thus table row in relational database is tuple in relation.

    Wiki on relations:

    In mathematics (more specifically, in set theory and logic), a relation is a property that assigns truth values to combinations (k-tuples) of k individuals. Typically, the property describes a possible connection between the components of a k-tuple. For a given set of k-tuples, a truth value is assigned to each k-tuple according to whether the property does or does not hold.

    0 讨论(0)
提交回复
热议问题