Chapter 1: Introduction to the Relational Model
Relational database basics.
Table of Contents
Relational Databases #
Relational databases are based on the relational model and consists of tables that are identified by a name and where each table contains a set of columns and a set of rows (also called instances or records).
Take the ff. users
table for example:
id | first_name | last_name | date_of_birth |
---|---|---|---|
1 | Jane | Doe | 1990-02-20 |
2 | John | Smith | 1990-01-01 |
3 | Kim | Chi | 2003-11-03 |
4 | Prybhat | Privtr | 2005-06-15 |
5 | Adams | Lernar | 2010-12-15 |
6 | Karen | Freeman | 2001-09-11 |
Here, the table contains columns: id
, first_name
, last_name
,
date_of_birth
and rows (or instances).
Relational Model #
The relational model is a model that contains relations which then contains a set of tuples of values.
For example, the users
relation:
Each tuple also contains attributes which describes an item or value in it.
The domain of an attribute defines the set of permitted values for a tuple item.
For example, the first_name
attribute in the users
relation should
contain all possible values of a first_name
.
It is also required that the domain of an attribute be atomic, meaning that the values contain indivisible units only.
An example of a non-atomic attribute would be a phone_numbers
attribute
that contains a set of phone numbers.
Keys #
Keys are a set of attributes that uniquely identify instances of a relation.
Formally, for a set of attributes in a relation and a set of keys where , for values if then .
A superkey is a set of attributes that can uniquely identify relation instances while a candidate key is a proper subset of a superkey where if any attributes within it are removed then it will fail to uniquely identify relation instances.
Primary keys are specific candidate keys that are chosen to uniquely identify instances in a relation.
Foreign Keys #
A foreign key is set of attributes in a relation that is used to reference an instance in another relation using the referenced relation's attributes that it corresponds to.
For example, an addresses
relation that contains an attribute user_id
that
corresponds to an instance in the users
relation using the id
attribute
in that relation:
Relational Query Languages #
A query language is a language that describes what information to get in a database.
In relational databases the Structured Query Language(SQL) is widely used as the query language.
Relational Algebra #
Relational algebra is a mathematical formalism and the basis for relational query languages such as SQL.
It consists of a set of operators which takes in one or two relations as inputs and produces a new relation as their result.
The ff. are operators in relational algebra:
Select Operator #
Selects tuples that satisfy a given predicate.
For example to select for addresses that has a "USA"
entry in the country
attribute:
We may also use relational operators such as , , , , as well as connectives and , or , and not :
Projection Operator #
Retrieves attributes of a relation.
For example to get only the first_name
and last_name
of the users
relation:
Cross-Product Operator #
Retrieves the product of two relations where each instances are concatenated to one another.
R \bigtimes SJoin Operator #
An operator that combines the cross product with a select statement to retrieve instances between two relations that contain referenced keys:
Let denote some predicate:
R \bowtie_{\theta} S = \sigma_{\theta}(R \bigtimes S)