Relational Database Design: Charles Severance
Relational Database Design: Charles Severance
Charles Severance
www.wa4e.com
http://www.wa4e.com/lectures/SQL-02-MySQL-Design-Handout.txt
Complex Data Models and
Relationships
http://en.wikipedia.org/wiki/Relational_model
Database Design
• Database design is an art form of its own with particular skills and
experience.
• Our goal is to avoid the really bad mistakes and design clean and easily
understood databases.
• Basic Rule: Don’t put the same string data in twice - use a relationship
instead
• When there is one thing in the “real world” there should only be one
copy of that thing in the database
Track Len Artist Album Genre Rating Count
For each “piece of info”...
Album
• Is the column an object or an Len
attribute of another object? Genre
belongs-to
Genre
Representing Relationships
in a Database
We want to keep track of which band is the “creator” of each music track...
What album does this song “belong to”?
• Add a special “key” column to each table, which you will make
references to.
http://en.wikipedia.org/wiki/Database_normalization
Integer Reference Pattern
We use integer columns in one Artist
table to reference (or look up)
rows in another table.
Album
Key Terminology
Finding our way around....
Three Kinds of Keys
belongs-to
Genre
belongs-to Track
Album Title
Rating
Len Track
Count
track_id
Album title
Table rating
album_id
Primary key len
Logical key title
Foreign key count
album_id
Artist Track
artist_id Album track_id
name album_id title
title rating
artist_id len
Table
Primary key count
Logical key album_id
Foreign key Genre genre_id
Naming the Foreign key
genre_id
artist_id is a convention name
Creating our Music Database
CREATE DATABASE Music
DEFAULT CHARACTER SET utf8;
USE Music;
CREATE TABLE Artist (
artist_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY(artist_id)
) ENGINE = InnoDB;
Album
Genre
Artist
Using Join Across Tables
http://en.wikipedia.org/wiki/Join_(SQL)
Relational Power
• By removing the replicated data and replacing it with references to a
single copy of each bit of data, we build a “web” of information that the
relational database can read through very quickly - even for very large
amounts of data.
• Often when you want some data it comes from a number of tables
linked by these foreign keys.
The JOIN Operation
• The JOIN operation links across several tables as part of a SELECT
operation.
• You must tell the JOIN how to use the keys that make the connection
between the tables using an ON clause.
The tables that
hold the data
Joining two tables without an ON clause gives all possible combinations of rows.
SELECT Track.title, Genre.name FROM Track JOIN Genre
ON Track.genre_id = Genre.genre_id
It Can Get Complex...
SELECT Track.title, Artist.name, Album.title, Genre.name
FROM Track JOIN Genre JOIN Album JOIN Artist ON
Track.genre_id = Genre.genre_id AND Track.album_id =
Album.album_id AND Album.artist_id = Artist.artist_id
What we want
to see
The tables that
hold the data
How the tables
are linked
ON DELETE CASCADE
Child
• SET NULL – Set the foreign key columns in the child rows to null
http://stackoverflow.com/questions/1027656/what-is-mysqls-default-on-delete-behavior
Many-to-Many Relationships
www.tsugi.org
Review:
belongs-to Track One to Many
Album Title
One Many Rating
Len Track
Count
id
Table
Primary key Album title
Logical key One
rating
Foreign key id
len
title Many
count
https://en.wikipedia.org/wiki/One-to-many_(data_model)
album_id
One Many
One
Many
https://en.wikipedia.org/wiki/One-to-many_(data_model)
Many to Many
Account
Course Member
Many account_id
course_id account_id
Many One email
title One course_id
name
role
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
Start with a Fresh Database
CREATE TABLE Account (
account_id INTEGER NOT NULL AUTO_INCREMENT,
email VARCHAR(128) UNIQUE,
name VARCHAR(128),
PRIMARY KEY(account_id)
) ENGINE=InnoDB CHARACTER SET=utf8;
• By normalizing the data and linking it with integer keys, the overall
amount of data which the relational database must scan is far lower
than if the data were simply flattened out.
• The key is to have one copy of any data element and use relations and
joins to link the data to multiple places.
• This greatly reduces the amount of data that must be scanned when
doing complex operations across large amounts of data.