Lecture #01: Relational Model & Relational Algebra: 1 Databases
Lecture #01: Relational Model & Relational Algebra: 1 Databases
1 Databases
A database is an organized collection of inter-related data that models some aspect of the real-world (e.g.,
modeling the students in a class or a digital music store). People often confuse “databases” with “database
management systems” (e.g., MySQL, Oracle, MongoDB). A database management system (DBMS) is the
software that manages a database.
Consider a database that models a digital music store (e.g., Spotify). Let the database hold information
about the artists and which albums those artists have released.
Early DBMSs
Database applications were difficult to build and maintain because there was a tight coupling between logical
and physical layers. The logical layer is which entities and attributes the database has while the physical
layer is how those entities and attributes are being stored. Early on, the physical layer was defined in the
application code, so if we wanted to change the physical layer the application was using, we would have to
change all of the code to match the new physical layer.
4 Relational Model
Ted Codd noticed that people were rewriting DBMSs every time they wanted to change the physical layer,
so in 1970 he proposed the relational model to avoid this. This relational model has three key points:
• Store database in simple data structures (relations).
• Access data through high-level language.
• Physical storage left up to implementation.
A data model is a collection of concepts for describing the data in a database. The relational model is an
example of a data model.
A schema is a description of a particular collection of data, using a given data model.
The relational data model defines three concepts:
• Structure: The definition of relations and their contents. This is the attributes the relations have and
the values that those attributes can hold.
• Integrity: Ensure the database’s contents satisfy constraints. An example constraint would be that
any value for the year attribute has to be a number.
• Manipulation: How to access and modify a database’s contents.
A relation is an unordered set that contains the relationship of attributes that represent entities. Since the
relationships are unordered, the DBMS can store them in any way it wants, allowing for optimization.
A tuple is a set of attribute values (also known as its domain) in the relation. Originally, values had to be
atomic or scalar, but now values can also be lists or nested data structures. Every attribute can be a special
value, NULL, which means for a given tuple the attribute is undefined.
A relation with n attributes is called an n-ary relation.
Keys
A relation’s primary key uniquely identifies a single tuple. Some DBMSs automatically create an internal
primary key if you do not define one. A lot of DBMSs have support for autogenerated keys so an application
does not have to manually increment the keys.
A foreign key specifies that an attribute from one relation has to map to a tuple in another relation.
6 Relational Algebra
Relational Algebra is a set of fundamental operations to retrieve and manipulate tuples in a relation. Each
operator takes in one or more relations as inputs, and outputs a new relation. To write queries we can “chain”
these operators together to create more complex operations.
Select
Select takes in a relation and outputs a subset of the tuples from that relation that satisfy a selection predicate.
The predicate acts like a filter, and we can combine multiple predicates using conjunctions and disjunctions.
Syntax: σpredicate (R).
Projection
Projection takes in a relation and outputs a relation with tuples that contain only specifed attributes. You can
rearrange the ordering of the attributes in the input relation as well as manipulate the values.
Syntax: πA1,A2,. . . ,An (R).
Union
Union takes in two relations and outputs a relation that contains all tuples that appear in at least one of the
input relations. Note: The two input relations have to have the exact same atttributes.
Syntax: (R ∪ S).
Intersection
Intersection takes in two relations and outputs a relation that contains all tuples that appear both of the input
relations. Note: The two input relations have to have the exact same atttributes.
Syntax: (R ∩ S).
Difference
Difference takes in two relations and outputs a relation that contains all tuples that appear in the first relation
but not the second relation. Note: The two input relations have to have the exact same atttributes.
Syntax: (R − S).
Product
Product takes in two relations and outputs a relation that contains all possible combinations for tuples from
the input relations.
Syntax: (R × S).
Join
Join takes in two relations and outputs a relation that contains all the tuples that are a combination of two
tuples where for each attribute that the two relations share, the values for that attrubite of both tuples is the
same.
Syntax: (R ./ S).
Observation
Relational algebra is a procedural language because it defines the high level-steps of how to compute a
query. For example, σb id=102 (R ./ S) is saying to first do the join of R and S and then do the select,
whereas (R ./ (σb id=102 (S))) will do the select on S first, and then do the join. These two statements will
actually produce the same answer, but if there is only 1 tuple in S with b id=102 out of a billion tuples, then
(R ./ (σb id=102 (S))) will be significantly faster than σb id=102 (R ./ S).
A better approach is to say the result you want, and let the DBMS decide the steps it wants to take to compute
the query. SQL will do exactly this, and it is the de facto standard for writing queries on relational model
databases.