03:00
Lecture 13: Databases
September 1, 2026
π€ This week we will move away from the language R and instead focus on SQL.
π The bulk of the final exam will focus on the language R. Assignment 5 and the last labs will also use R.
π In todayβs lecture we will introduce ourselves to larger, more complex database structures. We aim to answer the questions:
Furthermore we will cover the relational model, both the:
A database is an organized collection of structured information, or data, typically stored electronically in a computer system.
π Essentially anything can be stored as a set of related data values.
We have to when datasets get incredibly large.
For example, a standard Excel document can store roughly:
Most Excel documents however are much smaller, around 0.5MB to 1MB.
What happens if we are dealing with a very large dataset?
Consider the nycflights13 data from previous weeks.
This is still a fairly small dataset. Really large datasets can be many (GB) or even (TB).
For example:
If we import a large dataset into R, then we are storing that data in our computerβs memory.
In our environment tab in RStudio we can see a read-out of the amount of memory we are currently using.

π RStudio can only use so much memory before computer performance declines rapidly with high memory usage.
03:00
Apple -> About This Mac -> MemoryTask Manager -> Performance -> MemoryR has available.
?gc.gc() (results will vary!)My Mac has 1TB of storage and 16GB of RAM (which is starting to lag behind 2026 laptops now).
used columns report the memory R is currently occupying.max used columns report the peak usage so far this session.Ncells (objects) and Vcells (vectors/data).The table below details several databases that are too large to load into R.
| File Number | File Name | Size |
|---|---|---|
| 1 | Diabetic Retinopathy Detection | 82.2 GiB |
| 2 | American Epilepsy Society Seizure Prediction Challenge | 59.6 GiB |
| 3 | Avito Duplicate Ads Detection | 48.4 GiB |
| 4 | Microsoft Malware Classification Challenge (BIG 2015) | 35.3 GiB |
| 5 | GE Flight Question | 34.4 GiB |
Our approach:
π We only ever hold what we need in memory β the connection lets us query a database far larger than our RAM.
We defined a database earlier as an organized collection of structured information, or data, typically stored electronically in a computer system.
Our databases will satisfy the following two traits:
We manage databases through Database Management Systems (DBMS).
There are four main types:
Broadly, we can classify DBMS into:
In PSTAT 10 we will specifically work with Relational Databases.

We divide the relational database model into three parts:
π Today we discuss data structure and data integrity.
We will consider the following definitions:
A relational database is a collection of structured data organized into relations.
For example, see the table below:
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
From the first row we see that a student with ID S1, named Remy, is enrolled in PSTAT8.
Relations have the following properties:
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
StudentId, FirstName, Course#.The domain of a relation attribute is the set of atomic values used to model that attributeβs data.
Course# would be PSTAT5A, PSTAT5LS, PSTAT8, PSTAT10, ...Keys are (subjectively) the most important structural component of the relational model.
A key is a set of attributes that determine the other attributes in each tuple, establish and identify links between relations, and set any required constraints on a database.

| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
A super key is a set of attributes uniquely identifying a tuple in a relation.
03:00
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
Which of the following are super keys?
(StudentId, FirstName, Course#)(StudentId, FirstName)(StudentId, Course#)(StudentId)| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
The following are both super keys:
(StudentId, FirstName, Course#)(StudentId, Course#)Remy (S1) appears in two rows, so (StudentId) and (StudentId, FirstName) are not super keys β they cannot tell those rows apart.Course# distinguishes the tuples, so any set containing it uniquely identifies a row.| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
A candidate key is a unique tuple identifier (super key) with no redundancy.
Consider the previous exampleβs super keys:
{StudentId, FirstName, Course#} & {StudentId, Course#}
FirstName attribute is redundant; both attribute sets point to the same tuple.{StudentId, Course#} is a candidate key.A primary key is a set of attributes that uniquely identifies any tuple in a given relation.
null.π In our previous example {StudentId, Course#} was the primary key for ENROLLMENT.
A foreign key is a set of attributes that links attributes across relations.
R1 and R2, the foreign key links attributes from R1 to those in R2.Before we do though, think about why foreign keys might be useful?
| StudentId | FirstName |
|---|---|
| S1 | Remy |
| S2 | Sam |
| S3 | Taylor |
| S4 | Jordan |
Primary Key: {StudentId}
| StudentId | GPA |
|---|---|
| S1 | 3.81 |
| S2 | 3.97 |
| S3 | 3.02 |
| S4 | 2.79 |
Primary Key: {StudentId, GPA}
Foreign Key: {StudentId}
The structure of keys may remind you of the basics of set theory:
Written in the language of set theory we have that:
\[ \{\text{Super Keys}\}\supset \{\text{Candidate Keys}\}\supset \{\text{Primary Keys}\} \]
Foreign keys link relations together.
The first portion of this lecture was focused on data structure, i.e.:
The next portion focuses on data integrity:
A relation has entity integrity when its primary keys are unique and have values that are unique and not NULL for every attribute (column).
π Entity integrity ensures we do not have (potential) duplicate tuples in a relation.
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
Primary Key: {StudentId, Course#}
Suppose we want to add a new student named Sam who is enrolled in PSTAT174.
We considered two types of keys:
We think of integrity the same way:
Foreign keys can take two types of values:
NULL.Referential integrity ensures that we never attempt to reference a nonexistent tuple.
| CUST_NO | NAME | Age |
|---|---|---|
| C1 | Alex | 21 |
| C2 | Charlie | 26 |
Primary Key: {CUST_NO}
| ORDER_NO | DATE | CUST_NO |
|---|---|---|
| 011 | 6/11/23 | C1 |
| 012 | 6/24/23 | null |
| 013 | 7/1/23 | C3 |
| 015 | 7/12/23 | C2 |
Foreign Key: {CUST_NO}
Note the third tuple of the SALES_ORDER relation:
\[ \{013,~7/1/23,~C_3\}. \]
In customers, we have CUST_NO values:
\[ \{C_1, C_2\}. \]
π Thus referential integrity is violated β we reference a primary key value which does not exist.
04:00
| Product | Price ($) | Category | Manufacturer |
|---|---|---|---|
| Galaxy S23 | 899 | Phones | Samsung |
| null | 829 | Phones | Apple |
| Macbook Air | 1099 | Laptops | Apple |
| Fire TV | 449 | Televisions | Amazon |
| Product | Order_NO | Cust_ID |
|---|---|---|
| Fire TV | 1 | CS1 |
| Galaxy S23 | 2 | CS11 |
| Macbook Pro | 3 | CS11 |
We are given the tables above: ITEMS and ORDERS, with primary keys Product and Cust_ID.
ORDERS?| Product | Price ($) | Category | Manufacturer |
|---|---|---|---|
| Galaxy S23 | 899 | Phones | Samsung |
| null | 829 | Phones | Apple |
| Macbook Air | 1099 | Laptops | Apple |
| Fire TV | 449 | Televisions | Amazon |
| Product | Order_NO | Cust_ID |
|---|---|---|
| Fire TV | 1 | CS1 |
| Galaxy S23 | 2 | CS11 |
| Macbook Pro | 3 | CS11 |
Productnull primary key in ITEMS β Product.Macbook Pro in ORDERS β Product has no matching tuple.The relation schema is the relation name and its attributes.
| StudentId | FirstName | Course# |
|---|---|---|
| S1 | Remy | PSTAT8 |
| S1 | Remy | PSTAT131 |
| S2 | Sam | PSTAT10 |
| S3 | Taylor | PSTAT131 |
| S4 | Jordan | PSTAT174 |
Schema: Enrollment {StudentId, FirstName, Course#}
{StudentId, Course#}; this type of key is known as a composite key.The database schema is the set of all relation schema in a database.
For example, the earlier STUDENT and GPA relations together form a database schema:
\[ \texttt{Student \{StudentId, FirstName\}},\quad \texttt{GPA \{StudentId, GPA\}}. \]
π The database schema records both the relations and the keys that link them.
π This lecture we introduced ourselves to databases, specifically:
π Next class we will discuss accessing and modifying databases.