Study Vault Search notes, aliases and full text
Graph
LectureDBISAI-written · not yet proofread

VL03 Database Basics

Tables in the relational model (multisets of rows), SQLite3, database design with primary and foreign keys, and the semantics of basic SELECT–FROM–WHERE queries via the cross-product.

Held Source lecture-03

Organisation#

Tables#

A database stores its data in tables (Slide 7). The structure of a table is fixed by its schema: a fixed number of columns, each with a name and a domain (the set of possible values). The contents are stored in rows; while the schema is fixed, rows can be added, changed and deleted over time, and a table may have 0 rows.

titleyearrating
The Shawshank Redemption19949.3
The Dark Knight20089.1
Inception20108.8
Fight Club19998.8

Sets vs. multisets#

A set contains each element at most once; a multiset may contain an element several times (Slide 9):

S={1,3,7}, ∣S∣=3M={1,7,3,7,7}, ∣M∣=5 (7 occurs 3 times)S = \{1, 3, 7\},\ |S| = 3 \qquad M = \{1, 7, 3, 7, 7\},\ |M| = 5 \text{ (7 occurs 3 times)}
  • Codd’s original relational model (1969) only allowed sets. Most modern database systems allow multisets by default, so the formal definition above does too.
  • Checking for duplicates costs time and space. If uniqueness is needed, most systems let you declare a uniqueness constraint when creating the table.

Order (Slide 10): neither sets nor multisets have an order. A database does not have to remember the insertion order, and it can sometimes work more efficiently when it may reorder rows. If an order is needed, add a column with a row counter; most systems (SQLite3 included) offer an auto-incrementing counter, but it costs time and space, so use it only when needed.

Multiple tables and queries#

A database usually has several tables, and different tables often share columns; shared columns are what allows meaningful combination of data across tables (Slide 11). Deciding which tables, with which schemas, store the given data is the problem of database design, which matters for both usability and efficiency.

Tables are queried with SQL (Structured Query Language) (Slide 12). There are many dialects, but the core is the same in almost all systems. Title and rating of all movies with rating at least 8.0:

SELECT title, rating FROM movies WHERE rating >= 8.0;

SQLite3#

A system that manages relational tables is a relational database management system (RDBMS), e.g. MySQL, PostgreSQL, Microsoft SQL Server, SQLite, Oracle Database, MariaDB, IBM Db2, SAP HANA (Slide 13). The course uses SQLite3: a fully functional RDBMS that is very easy to use and reasonably efficient when the data is not very large.

Usage (Slide 14):

sudo apt install sqlite3   # Debian/Ubuntu
sqlite3                    # in-memory database
sqlite3 movies.db          # database stored in that file (created if missing, reused if it exists)

There are two kinds of commands:

  • SQL commands must end with a semicolon.
  • SQLite commands start with a dot and have no semicolon.

SQLite commands (Slide 15):

.separator "<TAB>"          column separator for input and output (type TAB with Ctrl+V TAB)
.import movies.tsv movies   read table "movies" from a TSV (tab-separated values) file
.read <file>                execute commands from a script file (typically *.sql)
.timer on                   show the execution time of every command
.help / .help import        list commands / help on one command

SQL commands (Slide 16):

-- movies is the table name; title, year, rating are column names; TEXT, INTEGER, REAL their domains
CREATE TABLE movies(title TEXT, year INTEGER, rating REAL);
SELECT title, year, rating FROM movies WHERE rating >= 8.0;
DROP TABLE IF EXISTS movies;   -- no error if the table does not exist

Database Design#

Motivation (Slide 17): in movies, each movie has exactly one title, one year and one rating. Now we want to store which actor played in which movie. A movie has many actors and most actors play in several movies, but a single cell cannot easily hold several values. How to store this in tables is the central question of database design.

SolutionIdeaVerdict
1 (Slide 18)extra column roles in movies with all actor names concatenatedterrible: hard to extract a single actor, hard to add information about the role
2 (Slide 19)extra column actor, one row per actor, repeating title/year/ratingnot that bad (query results will often look like this), but tables should not be created like that
3 (Slide 20)separate table roles(title TEXT, actor TEXT)much better (a column for the role could be added easily), but names are used as identifiers
4 (Slide 22)unique IDs for movies and persons; roles stores pairs of IDsthe right design

Solution 4 (Slide 22): give each row of movies a unique ID (e.g. the Wikidata ID), add a table persons with a unique ID per person (plus the birth date), and let roles reference both IDs:

CREATE TABLE movies(id TEXT, title TEXT, year INTEGER, score REAL);
CREATE TABLE persons(id TEXT, name TEXT, birth_date DATE);
CREATE TABLE roles(movie_id TEXT, person_id TEXT);

Example rows (Slide 23):

idtitleyearrating
Q172241The Shawshank Redemption19949.3
Q163872The Dark Knight20089.1
Q25188Inception20108.8
Q190050Fight Club19998.8
idnamebirth_date
Q48377Morgan Freeman1937-06-01
Q95048Tim Robbins1958-10-16
Q45772Christian Bale1974-01-30
Q202381Maggie Gyllenhaal1977-11-16
movie_idperson_id
Q172241Q48377
Q172241Q95048
Q163872Q48377
Q163872Q45772
Q163872Q202381

Primary key and foreign key#

In an RDBMS (including SQLite3) the keys can be declared explicitly (Slide 25):

CREATE TABLE movies(id TEXT PRIMARY KEY,
                    title TEXT, year INTEGER, score REAL);
CREATE TABLE persons(id TEXT PRIMARY KEY,
                     name TEXT, birth_date DATE);
CREATE TABLE roles(
  movie_id TEXT REFERENCES movies(id),
  person_id TEXT REFERENCES persons(id));

The RDBMS then checks that every row has a primary key, that it is unique, and that every foreign key value exists in the referenced table.

Relationships between tables#

Two tables AA and BB sharing keys (directly or via other tables) stand in one of four relationships (Slide 26):

KindMeaning
1:1each record in AA can be associated with exactly one record in BB
1:Na record in AA can be associated with several records in BB, but each record in BB with only one record in AA
N:1like 1:N, the other way round
N:Ma record in AA can be associated with several records in BB and vice versa

SQL Basics#

The semantics is built up incrementally for the running example (all roles: which person played in which movie):

SELECT movies.title, persons.name
FROM movies, persons, roles
WHERE roles.person_id = persons.id
  AND roles.movie_id = movies.id;

FROM: the cross-product#

SELECT * FROM movies, persons, roles; computes the cross-product T1×⋯×TkT_1 \times \dots \times T_k of the tables T1,…,TkT_1, \dots, T_k listed in FROM: the set of all (row1,…,rowk)(\mathit{row}_1, \dots, \mathit{row}_k) with rowi∈Ti\mathit{row}_i \in T_i, i.e. all combinations obtained by taking one row from each table (Slide 29). With #cols\#\mathrm{cols} and #rows\#\mathrm{rows} the number of columns and rows of a table:

#cols(T1×⋯×Tk)=∑i=1k#cols(Ti),#rows(T1×⋯×Tk)=∏i=1k#rows(Ti).\#\mathrm{cols}(T_1 \times \dots \times T_k) = \sum_{i=1}^{k} \#\mathrm{cols}(T_i), \qquad \#\mathrm{rows}(T_1 \times \dots \times T_k) = \prod_{i=1}^{k} \#\mathrm{rows}(T_i).

WHERE: filtering the cross-product#

Different tables can use the same column name, so columns are disambiguated as <table name>.<column name> (Slide 33). A query with a WHERE clause computes all rows of the cross-product (from FROM) for which the WHERE expression evaluates to true. In the example, only the rows in which the role’s person_id matches the person’s id and the role’s movie_id matches the movie’s id survive, i.e. exactly one row per role:

lecture-03, PDF page 34
lecture-03, PDF page 34
idtitleidnamemovie_idperson_id
Q172241The Shawshank RedemptionQ48377Morgan FreemanQ172241Q48377
Q163872The Dark KnightQ48377Morgan FreemanQ163872Q48377
Q163872The Dark KnightQ202381Maggie GyllenhaalQ163872Q202381

(Slide 35)

SELECT: choosing columns#

The full query computes all rows of the cross-product for which the WHERE expression is true, restricted to the columns listed in SELECT (Slides 36–37):

titlename
The Shawshank RedemptionMorgan Freeman
The Dark KnightMorgan Freeman
The Dark KnightMaggie Gyllenhaal

Concepts#

  • Relational Model — formal definition of a table as (C,D,R)(C, D, R) with a multiset of rows; primary and foreign keys
  • SQL Query Structure — basic SELECT … FROM … WHERE … queries and their semantics via cross-product, filter and column choice
  • Cross-product (Cartesian product) — semantics of the FROM clause; #cols\#\mathrm{cols} add up, #rows\#\mathrm{rows} multiply
  • Database design — choosing tables and schemas; movies/persons/roles with IDs
  • Table relationships (1:1, 1:N, N:1, N:M) — how tables relate via primary and foreign keys
  • Multiset — rows may repeat and have no order

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only