Introduction
The database symbol is a cylinder because databases were originally stored on a hard disk of stacked plates.
Question: Why use a database management system (DBMS) instead of a filesystem?
- Data consistency (schema)
- Crash recovery
- Efficient data retrieval
- Concurrent access to data
- Physical data independence (ability to change how data is stored without changing application code)
A DBMS allows users to store, query, and update databases in accordance to some data model. Examples of data models include:
- Relational
- Key-value
- Graph
- Document
- Vector
DBMS design principles:
- More specialized databases, driven by application semantics
- Few primitives (relational operators) which can be composed
- Data independence; applications should not need to understand how data is stored
- Declarative programming
Relational Algebra
Early database applications tightly coupled logical and physical layers, which made it difficult to make any changes to the program. To deal with the maintenance overhead, Ted Codd wrote A relational model of data for large shared data banks, which later became the basis of the relational data model.
The key ideas are:
- Data should be stored in simple data structures (relations)
- The DBMS should be responsible for the physical storage of the data
- Data should be accessed through high-level, declarative language, so that the DBMS can optimize it
Terminology:
- A relation is an unordered set of tuples
- A tuple is a set of attribute values
- A primary key uniquely identifies a single tuple in a relation
- A foreign key specifies that an attribute from one relation maps to a tuple in another relation
Relational models can define constraints, such as:
- Table schema is respected
- Primary keys are unique
- The foreign key exists in another table
- Global assertions
There are two different types of data management languages (DMLs):
- Procedural: the query specifies how to find the desired result (e.g. relational algebra)
- Declarative: the query only specifies what data is wanted (e.g. SQL)
Relational algebra is based on set algebra. That means that it represents relations as unordered lists with no duplicates. Each operator takes in one relation and outputs a new relation.
| Symbol | Meaning |
|---|---|
Select all tuples which satisfy pred | |
| Generate a relation with only the specified attributes | |
| Generate a relation that contains all tuples that appear in either relation | |
| Generate a relation that contains all tuples that appear in both relations | |
| Generate a relation that contains all tuples that appear in but not in | |
| Generate the Cartesian product of the two relations | |
| Perform a natural join; only return tuples which have common attributes |
SQL
Unlike in relational algebra, SQL assumes that data is stored in a bag, not a set. This means that there can be duplicated rows. This is for efficiency reasons—if we do a union on two very large tables, it is expensive to deduplicate the output.
SQL operations
- Selection ()
- Projection ()
- Aggregation
- Join
- Views
GROUP BY rules:
- Non-aggregated values in
SELECToutput must appear inGROUP BYclause. - Each
GROUP BYclause will cause the output to contain one tuple per group.
Types of JOIN:
- Cross join: Cartesian product
- Inner join: every pair that matches, dropping non-matching pairs
- Outer join: every pair that matches, keeping non-matching tuples
Within inner joins, there is also:
- Natural join (): matcch tuples where the shared attributes are equivalent
- Theta or equijoin (): match tuples using some join predicate defined by
- Semi join (): same as theta join, except output relation only contains attributes from
- Anti semijoin (): tuples of that do not match with any tuple of (not technically an inner join)
For more details about SQL syntax, go to the SQL page.