0% found this document useful (0 votes)
32 views64 pages

03 04 Database Design Many To Many

Web application for everyone: Relational Database Design

Uploaded by

kiauroraswi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views64 pages

03 04 Database Design Many To Many

Web application for everyone: Relational Database Design

Uploaded by

kiauroraswi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

WEB APPLICATIONS

Relational Database Design FOR EVERYBODY

Relational Database Design


Charles Severance
www.wa4e.com

http://www.wa4e.com/lectures/SQL-02-MySQL-Design-Handout.txt
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Relational Database Design

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.

• Others may performance tune things later.

• Database design starts with a picture...


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

www.tsugi.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

www.sakaiproject.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Building a Data Model


• Drawing a picture of the data objects for our application and then
figuring out how to represent the objects and their relationships

• 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 Len Artist Album Genre Rating Count


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

For each piece of info ...


• Is the column an object or an Len Album
attribute of another object? Genre
• Once we define objects, we need Artist Rating
to define the relationships
between objects. Track
Count
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”?

Which album is this song related to?


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Database Normalization (3NF)


There is *tons* of database theory - way too much to understand
without excessive predicate calculus

• Do not replicate data. Instead, reference data. Point at data.

• Use integers for keys and for references.

• 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

Integer Reference Pattern


We use integer columns in one Artist
table to reference (or look up)
rows in another table.

Album
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Key Terminology
Finding our way around....
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Three Kinds of Keys


• Primary key - generally an integer auto-
increment field Album
album_id
• Logical key - what the outside world uses title
for lookup
artist_id
• Foreign key - generally an integer key ...
pointing to a row in another table
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Primary Key Rules


User
Best practices: user_id
email
• Never use your logical key as the primary key.
password
name
• Logical keys can and do change, albeit slowly.
created_at
modified_at
• Relationships that are based on matching
login_at
string fields are less efficient than integers.
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

Building a Physical Data Schema


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

Creating our Music Database


CREATE DATABASE Music
DEFAULT CHARACTER SET utf8;

USE Music;
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

CREATE TABLE Artist (


artist_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
) ENGINE = InnoDB;

CREATE TABLE Album (


album_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
artist_id INTEGER,

INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (artist_id)


REFERENCES Artist (artist_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

CREATE TABLE Genre (


genre_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
INDEX USING BTREE (name)
) ENGINE = InnoDB;

CREATE TABLE Track (


track_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
len INTEGER,
rating INTEGER,
count INTEGER,
album_id INTEGER,
genre_id INTEGER,

INDEX USING BTREE (title),

CONSTRAINT FOREIGN KEY (album_id) REFERENCES Album (album_id)


ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (genre_id) REFERENCES Genre (genre_id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

INSERT INTO Artist (name) VALUES ('Led Zepplin');


INSERT INTO Artist (name) VALUES ('AC/DC');
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

INSERT INTO Artist (name) VALUES ('Led Zepplin');


INSERT INTO Artist (name) VALUES ('AC/DC');
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

INSERT INTO Genre (name) VALUES ('Rock');


INSERT INTO Genre (name) VALUES ('Metal');
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

INSERT INTO Track


(title, rating, len, count, album_id, genre_id)
VALUES ('Black Dog', 5, 297, 0, 2, 1);
INSERT INTO Track
(title, rating, len, count, album_id, genre_id)
VALUES ('Stairway', 5, 482, 0, 2, 1);
INSERT INTO Track
(title, rating, len, count, album_id, genre_id)
VALUES ('About to Rock', 5, 313, 0, 1, 2);
INSERT INTO Track
(title, rating, len, count, album_id, genre_id)
VALUES ('Who Made Who', 5, 207, 0, 1, 2);
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

We Have Relationships!
Track

Album
Genre

Artist
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Using Join Across Tables

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

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.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

The tables that


hold the data

SELECT Album.title, Artist.name FROM Album JOIN Artist ON


What we want Album.artist_id = Artist.artist_id How the tables
to see are linked
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Album.title Album.artist_id Artist.artist_id Artist.name

SELECT Album.title, Album.artist_id, Artist.artist_id,Artist.name


FROM Album JOIN Artist ON Album.artist_id = Artist.artist_id
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

SELECT Track.title, Genre.name FROM Track JOIN Genre


ON Track.genre_id = Genre.genre_id
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

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
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

ON DELETE CASCADE

Child

We are telling MySQL to


"clean up" broken references Parent

DELETE FROM Genre WHERE name = 'Metal'


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

ON DELETE CASCADE

DELETE FROM Genre WHERE name = 'Metal'


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

ON DELETE Choices

• Default / RESTRICT – Don’t allow changes that break the constraint

• CASCADE – Adjust child rows by removing or updating to maintain


consistency

• 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

One One Many

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

Start with a Fresh Database


CREATE TABLE User (
user_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(128) UNIQUE,
name VARCHAR(128)
) ENGINE=InnoDB CHARACTER SET=utf8;

CREATE TABLE Course (


course_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(128) UNIQUE
) ENGINE=InnoDB CHARACTER SET=utf8;
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,

CONSTRAINT FOREIGN KEY (user_id) REFERENCES User (user_id)


ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (course_id) REFERENCES Course (course_id)
ON DELETE CASCADE ON UPDATE CASCADE,

PRIMARY KEY (user_id, course_id)


) ENGINE=InnoDB CHARACTER SET=utf8;
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Insert Users and Courses


INSERT INTO User (name, email) VALUES ('Jane', '[email protected]');
INSERT INTO User (name, email) VALUES ('Ed', '[email protected]');
INSERT INTO User (name, email) VALUES ('Sue', '[email protected]');

INSERT INTO Course (title) VALUES ('Python');


INSERT INTO Course (title) VALUES ('SQL');
INSERT INTO Course (title) VALUES ('PHP');
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Insert Memberships

INSERT INTO Member (user_id, course_id, role) VALUES (1, 1, 1);


INSERT INTO Member (user_id, course_id, role) VALUES (2, 1, 0);
INSERT INTO Member (user_id, course_id, role) VALUES (3, 1, 0);

INSERT INTO Member (user_id, course_id, role) VALUES (1, 2, 0);


INSERT INTO Member (user_id, course_id, role) VALUES (2, 2, 1);

INSERT INTO Member (user_id, course_id, role) VALUES (2, 3, 1);


INSERT INTO Member (user_id, course_id, role) VALUES (3, 3, 0);
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

SELECT User.name, Member.role, Course.title


FROM User JOIN Member JOIN Course
ON Member.user_id = User.user_id AND
Member.course_id = Course.course_id
ORDER BY Course.title, Member.role DESC, User.name
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

https://www.mysql.com/products/workbench/
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

www.tsugi.org
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

Complexity Enables Speed


• Complexity makes speed possible and allows you to get very fast
results as the data size grows.

• 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.

• It might seem like a tradeoff - spend some time designing your


database so it continues to be fast when your application is a success.
WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

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.

• Database and SQL design is a bit of an art form.


WEB APPLICATIONS
Relational Database Design FOR EVERYBODY

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.

Initial Development: Charles Severance, University of Michigan


School of Information

Insert new Contributors and Translators here including names and


dates

You might also like