CSE 344: Section 4
ER diagrams, FDs
Apr 21st, 2025
Announcements
Database Design
E/R diagram Basics
is a
Subclass
Attribute
Entity set
Relationship
Weak Entity
Subclass
CREATE TABLE Product (
pname VARCHAR(30) PRIMARY KEY,
price INT)
CREATE TABLE Toy (
pname VARCHAR(30) PRIMARY KEY REFERENCES Product,
age INT)
CREATE TABLE Candy (
pname VARCHAR(30) PRIMARY KEY REFERENCES Product,
isChoc VARCHAR(5))
Relationships
Integrity Constraints in ER Diagrams
makes
makes
makes
Single Value Constraints (many-one) [at most one]
Referential Integrity Constraints (many-one) [exactly one]
Other Constraints
>2
Examples of Relationships
Question: "at most one" vs. "exactly one"?
Keys and FKs
Product
price
name
category
owns
Company
name
E/R to SQL
CREATE TABLE 3PlayerGame (
gid VARCHAR(30) PRIMARY KEY,
player1 VARCHAR(30) NOT NULL REFERENCES Players,
player2 VARCHAR(30) NOT NULL REFERENCES Players,
player3 VARCHAR(30) NOT NULL REFERENCES Players,
)
CREATE TABLE Players (
pid VARCHAR(30) PRIMARY KEY,
name VARCHAR(30)
)
Weak Entity Set
Worksheet Exercise 1:
Entity Relationship Diagrams
Worksheet Exercise 1: Entity Relationship Diagrams
Odegaard Library is in need of a new database, and they have asked you to help design it! Here are some of the requirements for what information needs to be stored in this database:
Worksheet Exercise 1: Entity Relationship Diagrams
book
num_of_pages
id
title
author
Worksheet Exercise 1: Entity Relationship Diagrams
book
num_of_pages
id
title
author
Worksheet Exercise 1: Entity Relationship Diagrams
book
num_of_pages
id
title
author
has_genre
genre
name
recommended_age
Worksheet Exercise 1: Entity Relationship Diagrams
reader
first_name
last_name
age
Worksheet Exercise 1: Entity Relationship Diagrams
reader
first_name
last_name
age
book
num_of_pages
id
title
author
has_genre
genre
name
recommended_age
check_out
day_check_out
Worksheet Exercise 1: Entity Relationship Diagrams
2) Convert the ER diagram to a series of CREATE TABLE statements. Include primary key and foreign key statements.
reader
first_name
last_name
age
book
num_of_pages
id
title
author
has_genre
genre
name
recommended_age
check_out
day_check_out
Worksheet Exercise 2
Worksheet Exercise 1: Entity Relationship Diagrams
CREATE TABLE Checkouts (
day_check_out VARCHAR(10),
book_id INT REFERENCES Books,
reader_email VARCHAR(50) REFERENCES Readers,
PRIMARY KEY (book_id, reader_email, day_check_out)
);
CREATE TABLE Readers (
email VARCHAR(50) PRIMARY KEY,
first_name VARCHAR(20),
last_name VARCHAR(20),
age INT
);
CREATE TABLE Books (
id INT PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(50),
genre VARCHAR(20) NOT NULL REFERENCES Genres,
num_pages INT
);
CREATE TABLE Genres (
name VARCHAR(20) PRIMARY KEY,
recommended_age INT
);
2) Convert the ER diagram to a series of CREATE TABLE statements. Include primary key and foreign key statements.
Functional Dependencies
Functional Dependency
A → B
Indicates that attribute A determines attribute B (B is dependent on A).
Some examples:
Transitive Dependency
If A → B and B → C
then A → C
{A}+ = Closure of attribute A: Set of all attributes that can be determined by A.
{A}+ = {ABC}
Example
Student(studentID, name, dateOfBirth, phoneNumber)
studentID | name | dateOfBirth | phoneNumber |
1234 | Allison | 01-09-1999 | 253-876-1028 |
1235 | Ben | 03-10-2000 | 206-999-1923 |
1236 | Chris | 05-23-1999 | 206-127-1010 |
1234 | Allison | 01-09-1999 | 253-307-1678 |
Example
Do the following FDs hold for�this relation?
{studentID} → {name}
{studentID} → {name, dateOfBirth}
{studentID} → {phoneNumber}
studentID | name | dateOfBirth | phoneNumber |
1234 | Allison | 01-09-1999 | 253-876-1028 |
1235 | Ben | 03-10-2000 | 206-999-1923 |
1236 | Chris | 05-23-1999 | 206-127-1010 |
1234 | Allison | 01-09-1999 | 253-307-1678 |
Example
Do the following FDs hold for�this relation?
{studentID} → {name}
{studentID} → {name, dateOfBirth}
{studentID} → {phoneNumber}
studentID | name | dateOfBirth | phoneNumber |
1234 | Allison | 01-09-1999 | 253-876-1028 |
1235 | Ben | 03-10-2000 | 206-999-1923 |
1236 | Chris | 05-23-1999 | 206-127-1010 |
1234 | Allison | 01-09-1999 | 253-307-1678 |
Example
Do the following FDs hold for�this relation?
{studentID} → {name}
{studentID} → {name, dateOfBirth}
{studentID} → {phoneNumber}
studentID | name | dateOfBirth | phoneNumber |
1234 | Allison | 01-09-1999 | 253-876-1028 |
1235 | Ben | 03-10-2000 | 206-999-1923 |
1236 | Chris | 05-23-1999 | 206-127-1010 |
1234 | Allison | 01-09-1999 | 253-307-1678 |
Example
Do the following FDs hold for�this relation?
{studentID} → {name}
{studentID} → {name, dateOfBirth}
{studentID} → {phoneNumber}
There are two tuples that agree on studentID but don’t agree on phoneNumber!
studentID | name | dateOfBirth | phoneNumber |
1234 | Allison | 01-09-1999 | 253-876-1028 |
1235 | Ben | 03-10-2000 | 206-999-1923 |
1236 | Chris | 05-23-1999 | 206-127-1010 |
1234 | Allison | 01-09-1999 | 253-307-1678 |
Closure
Closure Definition
Goal:
Closure
Closure Algorithm
Algorithm Steps:
1. Find some attribute(s) C to add to right side
2. Add them
3. Look back at the FDs to find more C
Closure Example
To determine the closure of ID we start with trivial FD: ID → ID because if we are given ID, then we know ID
Closure Example
Closure Example
Closure Example
Formally, the closure of ID is written as {ID}+ = {ID, Name, Job}
Closures (quicker)
Goal:
Observation:
Keys
We call the attribute(s) that determines all other attributes in a schema to be a superkey.
Ex. for R(A, B, C), the set {A, B, C} determines all attributes in R
If no subset of a superkey is also a superkey, then that superkey is a minimal key or key for short. If a relation has multiple keys, each key is a candidate key
Ex. if AB → ABC and C → ABC, then {AB} and {C} are candidate keys for R
Superkey
Minimal Key
Worksheet