# Relation (mathematics)
### Binary Relation
A binary relation over sets $X$ and $Y$ is a *subset* of the [cartesian-product](cartesian-product.md) $X \times Y$. That is, it is a set of ordered pairs $(x,y)$ consisting of elements $x$ in $X$ and $y$ in $Y$. It encodes the common concept of a relation:
> An element $x$ is **related** to an element $y$ if and only if the pair $(x, y)$ belongs to the set of ordered pairs that defines the *binary relation*.
A few examples are:
* **Is greater than**, **is equal to**, **divides**. For example the "is greater than" relation would only include the pairs where $x$ is greater than $y$
* A **function**. For example, the function $f(x) = x^2$ would be considered a binary relation over the sets $\mathbb{R}$ and $\mathbb{R}$, where the relation is a subset of $\mathbb{R} \times \mathbb{R} = \mathbb{R}^2$. It would look like: $\{ (0,0), (1,1), (2,4), (3,9), \dots \}$
### n-ary Relation
Here we are looking at sets $X_1, \dots, X_n$, where the n-ary relation is a subset of the cartesian product of $X_1 \times \dots \times X_n$.
From a database perspective, imagine that our table was about items and made up of the attributes:
* Weight = {0, 5, 10, 15, 20}
* Size = {small, medium, large}
* color = {red, blue, green, yellow}
The cartesian product of these sets (the sample space if you will) is: $weight \times size \times color$. An specific sample from this cartesian product could be $(5, medium, blue)$. Remember, we defined a relation as:
> A subset of the cartesian product of our sets under consideration.
So, a relation in this case would simply be a subset of our cartesian product, i.e. a subset of $weight \times size \times color$. This could possibly be:
$\big\{(5, medium, blue), (5, large, blue), (10, medium, green)\big\}$
This by definition defines a relation. The relation here could intuitively be thought of as describing some set in the real world. For instance, that may be: "the set of items we offer" or "the set of items we have in our inventory" or "the set of items on sale" or "the set of items present in our database". Regardless, it is by definition a relation. It is also a essentially a table! This is how we link the concept of a relation to a database!
### Relation
A relation between two sets is a collection of ordered pairs containing one object from each set. If the object xx is from the first set and the object yy is from the second set, then the objects are said to be related if the ordered pair (x,y)(x,y) is in the relation.
A [function](https://mathinsight.org/definition/function "Function definition: A function is a relation from a set of inputs to a set of possible outputs where each input is related to exactly one output.") is a type of relation. But, a relation is allowed to have the object xx in the first set to be related to more than one object in the second set. So a relation may not be represented by a [function machine](https://mathinsight.org/function_machine "The function machine: The function machine metaphor helps explain the definition and properties of a function."), because, given the object xx to the input of the machine, the machine couldn't spit out a unique output object that is paired to xx.
### Relation
In relational database theory, a relation is a set of tuples (think rows).
---
Date: 20210707
Links to:
Tags: #review
References:
* [Relations - Mathematics (wikipedia)](https://en.wikipedia.org/wiki/Relation_(mathematics))
* [Relations - binary relation wikipedia](https://en.wikipedia.org/wiki/Binary_relation)
* [Relation - mathinsight](https://mathinsight.org/definition/relation)
* [Relation - databases, wikipedia](https://en.wikipedia.org/wiki/Relation_(database)#:~:text=The%20term%20relation%20schema%20refers,in%20terms%20of%20that%20heading.&text=In%20SQL%2C%20a%20database%20language,each%20attribute%20form%20a%20column.)