How To Be Normal - A Guide for Developers

22
Presented by, MySQL AB® & O’Reilly Media, Inc. How To Be Normal - A Guide for Developers Presented By: Mike Hillyer

description

How To Be Normal - A Guide for Developers. Presented By: Mike Hillyer. Who Am I?. Mike Hillyer, BSc Former MySQL Documentation Team Member MySQL Core/Pro Certified Top 5 for MySQL at Experts-Exchange.com Former MySQL Expert at SearchEnterpriseLinux - PowerPoint PPT Presentation

Transcript of How To Be Normal - A Guide for Developers

Page 1: How To Be Normal - A Guide for Developers

Presented by,

MySQL AB® & O’Reilly Media, Inc.

How To Be Normal- A Guide for Developers

Presented By:

Mike Hillyer

Page 2: How To Be Normal - A Guide for Developers

Who Am I?

Mike Hillyer, BSc Former MySQL Documentation Team Member MySQL Core/Pro Certified Top 5 for MySQL at Experts-Exchange.com Former MySQL Expert at SearchEnterpriseLinux

More at http://www.mikehillyer.com/about/

Page 3: How To Be Normal - A Guide for Developers

Who Are You?

How Many of You: Are DBAs? Are Developers? Thought this was a session on fashion and

social etiquette? Know whether BCNF and DKNF are fashion

brands or normal forms? Know what a normal form is?

Page 4: How To Be Normal - A Guide for Developers

What Are You Watching? What is normalization and what are its benefits? What are the normal forms?

First Normal Form Second Normal Form Third Normal Form And so on…

Normalization Scenarios Relationships and Joins How Much is Too Much?

http://www.mikehillyer.com/presentations/

Page 5: How To Be Normal - A Guide for Developers

You May Be Infected…

Excellus Databasicus (Spreadsheet Syndrome)

SymptomsAsking “How many columns can a table have?”Using few tables, often only one.Ballooning storage needs.Redundant data.

TreatmentNormalization

Page 6: How To Be Normal - A Guide for Developers

What is Normalization?

Introduced by E.F. Codd. The modification of a schema so that it

conforms to defined normal forms. Ensuring that every non-key column relates to

“The Key, The Whole Key, and Nothing But the Key”So Help Me Codd

Makes data atomic. Reduces redundancy.

Page 7: How To Be Normal - A Guide for Developers

What Are The Benefits?

Decreased storage consumption.Removed redundancy means less data.

Better/Faster(/Stronger) searches.Less data to scan.Easier searches on (previously) mixed data.

Improved data integrity.When data is only in one place you only have to get it right/fix it once.

Page 8: How To Be Normal - A Guide for Developers

What Are The Normal Forms?

First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF)

Page 9: How To Be Normal - A Guide for Developers

What is First Normal Form? (1NF) Requires a Primary Key. (The Key) Requires that all data is atomic.

Also work on removing horizontal and vertical redundancies from your table.

Name

Mike Hillyer

ID First_Name

Last_Name

1 Mike Hillyer

Page 10: How To Be Normal - A Guide for Developers

What is Second Normal Form? (2NF)

Requires that all fields relate to an entire composite key, not just parts. (The Whole Key)

Reviewer_ID

First_Name

Last_Name

ISBN Score

1 Mike Hillyer 2233 4.5

Reviewer_ID

ISBN Score

1 2233 4.5

Page 11: How To Be Normal - A Guide for Developers

What is Third Normal Form? (3NF)

Requires that all fields depend directly on the primary key, and not on other non-key fields.

(And Nothing But The Key)

Address_ID

Address City State Zip

1 123 Main St Santa Clara

California

90221Zip City State

90221 Santa Clara

California

Page 12: How To Be Normal - A Guide for Developers

Three’s Not Enough, Are There More?

Boyce-Codd Normal Form Fourth Normal Form Fifth Normal Form Domain/Key Normal Form Sixth Normal Form Non-First Normal Form

http://en.wikipedia.org/wiki/Database_normalization

Page 13: How To Be Normal - A Guide for Developers

How Are My Entities Related?

Three Forms One to One

Same Table?

One to ManyPlace PK of the One in the Many

Many to ManyCreate a joining table

Page 14: How To Be Normal - A Guide for Developers

How Do I Put Things Back Together?

INNER JOIN

OUTER JOINLEFT JOINRIGHT JOIN

ID

1

2

3

ID

5

4

3

ID

3+=

ID

1

2

3

ID

5

4

3

+= ID ID

1 NULL

2 NULL

3 3

Page 15: How To Be Normal - A Guide for Developers

By Example: User Tracking

Table has no Primary Key. Name is not atomic. Table starts with all possible user

information in a single table. Phone numbers and email addresses

are horizontally redundant. Company, department, city, state, zip

are vertically redundant.

Page 16: How To Be Normal - A Guide for Developers

1NF Satisfied

Page 17: How To Be Normal - A Guide for Developers

2NF Satisfied

Page 18: How To Be Normal - A Guide for Developers

3NF Satisfied

Page 19: How To Be Normal - A Guide for Developers

Have I Over-Normalized?

email

PK address

FK1 user_idformat

phone

PK phone_id

FK1 type_idarea_codeNXXNCX

FK2 country_id

user

PK user_id

first_namelast_namenicknameunitstreet_numberstreet_namestreet_typequadrantweb_urlpicturenotes

FK1 postal_code

user_phone

PK,FK1 user_idPK,FK2 phone_id

extension

company

PK company_id

name

user_department

PK,FK1 user_idPK,FK2 department_id

type

PK type_id

type

country

PK country_id

Namephone_code

department

PK department_id

nameFK1 company_id

postal_code

PK postal_code

FK1 city_id

province

PK province_id

NameAbbreviation

FK1 country_id

city

PK city_id

nameFK1 province_id

Page 20: How To Be Normal - A Guide for Developers

When Should I De-Normalize?

Start by normalizing, then watch your slow query logs and run EXPLAIN.

De-Normalization may be needed on certain queries (joining and sorting).

Maintain data integrity with triggers.

Page 21: How To Be Normal - A Guide for Developers

Are We Done Yet?

Slides and audio available soon.

http://www.mikehillyer.com/presentations/ http://dev.mysql.com/tech-resources/articles/

intro-to-normalization.html

Stay for the giveaway! [email protected]

Page 22: How To Be Normal - A Guide for Developers

Are You Hiring?

Test Automation Engineer Software Engineer Web Engineer (a.k.a. Perl Kung-Foo Artist) Web Engineer (a.k.a. PHP Ninja) Site Reliability Engineer (a.k.a. Ops Ninja)

Contact me for more info.