# Math and SQL, Part 1: Introduction - Relations and Functions

# Math and SQL, Part 1: Introduction - Relations and Functions

Join the DZone community and get the full member experience.

Join For Free**Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.**

There are two goals I have in this series. The first is to heavily ground database thinking in mathematics thinking. The second is to connect this more explicitly to functional programming, which attempts to do the same more generally. The basic foundation of functional programming and relational theory is the same. While this may seem very rudamentary for Planet Postgresql, I am hoping that the information here will be useful in theoretical courses, etc.

This series will also discuss uses of the same ideas in Perl. Many of the same ideas are extremely important for developers in many languages. Consequently these will all be tagged both PostgreSQL and Perl and most will include perl code as well as PostgreSQL code.

### I: SQL as Relational Math Implementation

In basic terms, SQL provides an interface for relational database systems. The most powerful features of it are essentially relational algebra-based. In order to better handle concurrent connections, there is a lot missing from relational math, and in order to handle real-world data, there are features in relational algebra not present in relational math.

However for purposes of this series, I will focus on PostgreSQL's SQL interpretation and I will use non-standard syntax features when that makes the math clearer. I like PostgreSQL in this regard because it means that the math is fairly clear.

Code samples explaining ways to accomplish various operations will be provided in Perl. Note that these are mostly fairly naive versions. If one were really creating an RDBMS in Perl, the code would probably be very different.

### II: Relational Math, Relations and Functions

At the core of both databases and functional programming are the concepts of relations and functions. At their foundation, these are fairly old concepts. Since a function is a subset of relation, let's start by defining and understanding what a relation is:

*A relation is a set of correlated facts*.

For example, let's look at two relations, one showing square roots and one showing squares:

- For square roots:
- 1: 1
- 1: -1
- 4: 2
- 4: -2
- 9: 3
- 9: -3
- For squares
- -3: 9
- -2: 4
- -1: 1
- 0: 0
- 1: 1
- 2: 4
- 3: 9

*A function is a relation where the fact of the relation are fully determined by the domain of the function. Therefore if you know the fact from the domain, there is only one related fact.*

- f(x) = x + 1
- f(x) = 2 * x
- f(x) = x
^{2}

*for any given x there is exactly one f(x).*

*a candidate key is a subset of a relation which, as a domain, turns the relation into a function. All facts in a relation are a function of each candidate key.*

package KeyValueStore; my %store; sub set { my ($key, $value) = @_; $store{$key} = $value; } sub remove { my $key = shift; delete $store{$key}; } sub get { my $key = shift; $store{$key}; } package main; use KeyValueStore; KeyValueStore::set('foo', 'bar'); KeyValueStore::set('1', '2'); print KeyValueStore::get('foo'); 1;

**Connect any Java based application to your SaaS data. Over 100+ Java-based data source connectors.**

Published at DZone with permission of Chris Travers , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

{{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}