03 04 Database Design Many To Many
03 04 Database Design Many To Many
http://www.wa4e.com/lectures/SQL-02-MySQL-Design-Handout.txt
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
http://en.wikipedia.org/wiki/Relational_model
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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.
www.tsugi.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
www.sakaiproject.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
• 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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Track Track
Artist Rating
Album belongs-to
Len
Artist
Count
Genre Album belongs-to
Rating
Len belongs-to
Count Genre
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Track
Artist Rating
belongs-to
Len
Count
Album belongs-to
belongs-to
Genre
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Normalization
and Foreign Keys
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Album
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Key Terminology
Finding our way around....
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Foreign Keys
Album
• A foreign key is when a table has a Artist
album_ id
column containing a key that points artist_id
title
to the primary key of another table. name
artist_id
...
...
• When all primary keys are integers,
then all foreign keys are integers. This
is good - very good.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Track
Artist
belongs-to Rating
Len
Album Count
belongs-to
belongs-to
Genre
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
USE Music;
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
INSERT INTO Album (title, artist_id) VALUES ('Who Made Who', 2);
INSERT INTO Album (title, artist_id) VALUES ('IV', 1);
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
We Have Relationships!
Track
Album
Genre
Artist
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
http://en.wikipedia.org/wiki/Join_(SQL)
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
• You must tell the JOIN how to use the keys that make the
connection between the tables using an ON clause.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
SELECT Track.title,
Track.genre_id,
Genre.genre_id,
Genre.name
FROM Track JOIN Genre
Joining two tables without an ON clause gives all possible combinations of rows.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
What we want
to see
The tables that
hold the data
How the tables
are linked
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
ON DELETE CASCADE
Child
ON DELETE CASCADE
ON DELETE Choices
• 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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Many-to-Many Relationships
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
www.tsugi.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
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
album_id
https://en.wikipedia.org/wiki/One-to-many_(data_model)
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Many
https://en.wikipedia.org/wiki/One-to-many_(data_model)
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Many to Many
• Sometimes we need to model a
relationship that is many to many.
• We need to add a “connection”
table with two foreign keys.
• There is usually no separate
primary key.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
member-of
User
Course
title Many Many email
name
User
Course Member user_id
course_id Many
user_id email
Many One
title One course_id name
role
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
Member User
Course Many One
One user_id
Many user_id
course_id
course_id email
title
role name
CREATE TABLE Member (
user_id INTEGER,
course_id INTEGER,
role INTEGER,
Insert Memberships
https://www.mysql.com/products/workbench/
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
www.tsugi.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
• 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.
Summary
• Relational databases allow us to scale to very large amounts of data.
• 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.
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance (www.dr- Continue new Contributors and Translators here
chuck.com) as part of www.wa4e.com and made available under a
Creative Commons Attribution 4.0 License. Please maintain this
last slide in all copies of the document to comply with the
attribution requirements of the license. If you make a change, feel
free to add your name and organization to the list of contributors
on this page as you republish the materials.