Math and SQL, Part 1: Introduction - Relations and Functions
Join the DZone community and get the full member experience.
Join For FreeThere 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
- f(x) = x + 1
- f(x) = 2 * x
- f(x) = x2
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;
Published at DZone with permission of Chris Travers, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Security Challenges for Microservice Applications in Multi-Cloud Environments
-
What ChatGPT Needs Is Context
-
How To Use Pandas and Matplotlib To Perform EDA In Python
-
What Is JHipster?
Comments