steam learn: relational database normal forms

36
August, 21st 2014 Relational Database Normal Forms Dreams, inc case study by Romain Francez

Upload: inovia

Post on 15-Jul-2015

104 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Steam Learn: Relational database normal forms

August, 21st 2014

Relational Database Normal FormsDreams, inc case study

by Romain Francez

Page 2: Steam Learn: Relational database normal forms

August, 21st 2014

Disclaimer

● Not only about showing good database design

● About understanding normal forms

Page 3: Steam Learn: Relational database normal forms

August, 21st 2014

My Boss

Page 4: Steam Learn: Relational database normal forms

August, 21st 2014

Started managing with that

Page 5: Steam Learn: Relational database normal forms

August, 21st 2014

Started managing with that

Page 6: Steam Learn: Relational database normal forms

August, 21st 2014

Aim

● Model that manages the business (employees, items,

…)

● Steps that might be needed to do as a business grow

whilst solving some problems

Page 7: Steam Learn: Relational database normal forms

August, 21st 2014

Nomenclature

● Business Rules (BR)

○ Some normal forms take into account your business

logic

● Tables

○ Table name

○ Column name

○ Key

Page 8: Steam Learn: Relational database normal forms

August, 21st 2014

SQL Normal Forms

● What is a Normal Form?

● What is the simplest data model?

● How to remove functional dependency?

● How to deal with join dependency?

Page 9: Steam Learn: Relational database normal forms

August, 21st 2014

What is a Normal Form?

Set of rules for database design to minimise redundancy and to ensure consistency in insert/delete/update

Page 10: Steam Learn: Relational database normal forms

August, 21st 2014

SQL Normal Forms

● What is a Normal Form?

● What is the simplest data model?

● How to remove functional dependency?

● How to deal with join dependency?

Page 11: Steam Learn: Relational database normal forms

August, 21st 2014

1NF

EmployeeEmployee DreamCobb Rainy Downtown, Hotel, Snowy Mountain, LimboArthur Rainy Downtown, Hotel

Ariadne Rainy Downtown, Hotel, Snowy Mountain, Limbo

Eames Rainy Downtown, Hotel, Snowy MountainYusuf Rainy Downtown

BR1.1: Employees work on Dreams

Page 12: Steam Learn: Relational database normal forms

August, 21st 2014

1NF

The domain of each attribute contains only atomic values, and the value of each attribute contains only a single value from that domain

Page 13: Steam Learn: Relational database normal forms

August, 21st 2014

Atomic?

● Something that cannot be subdivided

● String, can be subdivided into substrings, but still

considered as atomic

Page 14: Steam Learn: Relational database normal forms

August, 21st 2014

1NF

EmployeeEmployee DreamCobb Rainy Downtown, Hotel, Snowy Mountain, LimboArthur Rainy Downtown, Hotel

Ariadne Rainy Downtown, Hotel, Snowy Mountain, Limbo

Eames Rainy Downtown, Hotel, Snowy MountainYusuf Rainy Downtown

EmployeeEmployee DreamCobb Rainy DowntownCobb HotelCobb Snowy MountainCobb LimboArthur Rainy DowntownArthur HotelAriadne Rainy DowntownAriadne HotelAriadne Snowy MountainAriadne LimboEames Rainy DowntownEames HotelEames Snowy MountainYusuf Rainy Downtown

BR1.1: Employees work on Dreams

Page 15: Steam Learn: Relational database normal forms

August, 21st 2014

SQL Normal Forms

● What is a Normal Form?

● What is the simplest data model?

● How to remove functional dependency?

● How to deal with join dependency?

Page 16: Steam Learn: Relational database normal forms

August, 21st 2014

2NF

EmployeeEmployee Dream Risk LevelCobb Rainy Downtown MediumCobb Hotel MediumCobb Snowy Mountain HighCobb Limbo ExtremeArthur Rainy Downtown MediumArthur Hotel MediumAriadne Rainy Downtown MediumAriadne Hotel MediumAriadne Snowy Mountain HighAriadne Limbo ExtremeEames Rainy Downtown MediumEames Hotel MediumEames Snowy Mountain HighYusuf Rainy Downtown Medium

BR2.1: Dreams have a risk level

Page 17: Steam Learn: Relational database normal forms

August, 21st 2014

2NF

Every non-prime attribute of the table is dependent on the whole of a candidate key

Page 18: Steam Learn: Relational database normal forms

August, 21st 2014

Candidate Key?Colour

Colour Hex R G BRed FF0000 255 0 0

{Colour, Hex, R, G, B}{Colour}{Hex}{R, G, B}{Colour, Hex}{Colour, R, G, B}{Hex, R, G, B}

Page 19: Steam Learn: Relational database normal forms

August, 21st 2014

Superkey

{Colour}or{Hex}

ColourColour Hex R G BRed FF0000 255 0 0

Page 20: Steam Learn: Relational database normal forms

August, 21st 2014

2NF

EmployeeEmployee Dream Risk LevelCobb Rainy Downtown MediumCobb Hotel MediumCobb Snowy Mountain HighCobb Limbo ExtremeArthur Rainy Downtown MediumArthur Hotel MediumAriadne Rainy Downtown MediumAriadne Hotel MediumAriadne Snowy Mountain HighAriadne Limbo ExtremeEames Rainy Downtown MediumEames Hotel MediumEames Snowy Mountain HighYusuf Rainy Downtown Medium

DreamDream Risk LevelRainy Downtown MediumHotel Medium

Snowy Mountain HighLimbo Extreme

Employee_DreamEmployee DreamCobb Rainy DowntownCobb HotelCobb Snowy MountainCobb LimboArthur Rainy DowntownArthur HotelAriadne Rainy DowntownAriadne HotelAriadne Snowy MountainAriadne LimboEames Rainy DowntownEames HotelEames Snowy MountainYusuf Rainy Downtown

BR2.1: Dreams have a risk level

Page 21: Steam Learn: Relational database normal forms

August, 21st 2014

Why Do Business Rules Matter?

Employee_DreamEmployee Dream Risk LevelCobb Rainy Downtown MediumCobb Hotel MediumCobb Snowy Mountain HighCobb Limbo ExtremeArthur Rainy Downtown MediumArthur Hotel MediumAriadne Rainy Downtown MediumAriadne Hotel MediumAriadne Snowy Mountain HighAriadne Limbo ExtremeEames Rainy Downtown MediumEames Hotel MediumEames Snowy Mountain HighYusuf Rainy Downtown Medium

2NF because the risk level now depends on the Employee and Dream

BR2.2: Each Employee is graded a risk level per Dream

Page 22: Steam Learn: Relational database normal forms

August, 21st 2014

3NF

Dream

Dream Risk Level Minimum Preparation TimeRainy Downtown Medium 10Hotel Medium 10Snowy Mountain High 20Limbo Extreme 50

BR3.1: Each dream must have a minimum of preparation time depending on its risk level

Page 23: Steam Learn: Relational database normal forms

August, 21st 2014

3NF

All the attributes in a table are dependent on the primary key and only on the primary key

Page 24: Steam Learn: Relational database normal forms

August, 21st 2014

3NF

Dream

Dream Risk Level Minimum Preparation TimeRainy Downtown Medium 10Hotel Medium 10Snowy Mountain High 20Limbo Extreme 50

Risk

Risk Level Minimum Preparation TimeMedium 10High 20Extreme 50

DreamDream Risk LevelRainy Downtown MediumHotel Medium

Snowy Mountain HighLimbo Extreme

BR3.1: Each dream must have a minimum of preparation time depending on its risk level

Page 25: Steam Learn: Relational database normal forms

August, 21st 2014

SQL Normal Forms

● What is a Normal Form?

● What is the simplest data model?

● How to remove functional dependency?

● How to deal with join dependency?

Page 26: Steam Learn: Relational database normal forms

August, 21st 2014

4NF

Employee_DreamEmployee Dream SkillArthur Rainy Downtown DreamsArthur Rainy Downtown GunsArthur Hotel DreamsArthur Hotel GunsAriadne Rainy Downtown ArchitectureAriadne Hotel ArchitectureAriadne Snowy Mountain ArchitectureAriadne Limbo ArchitectureYusuf Rainy Downtown DreamsYusuf Rainy Downtown Sedatives

BR4.1: Employees have various skills

Page 27: Steam Learn: Relational database normal forms

August, 21st 2014

4NF

Every non-trivial multivalued dependency in the table is a dependency on a superkey

Page 28: Steam Learn: Relational database normal forms

August, 21st 2014

4NF

Employee_DreamEmployee Dream SkillArthur Rainy Downtown DreamsArthur Rainy Downtown GunsArthur Hotel DreamsArthur Hotel GunsAriadne Rainy Downtown ArchitectureAriadne Hotel ArchitectureAriadne Snowy Mountain ArchitectureAriadne Limbo ArchitectureYusuf Rainy Downtown DreamsYusuf Rainy Downtown Sedatives

Employee_SkillEmployee SkillArthur DreamsArthur GunsAriadne ArchitectureYusuf DreamsYusuf Sedatives

Employee_DreamEmployee DreamArthur Rainy DowntownArthur HotelAriadne Rainy DowntownAriadne HotelAriadne Snowy MountainAriadne LimboYusuf Rainy Downtown

BR4.1: Employees have various skills

Page 29: Steam Learn: Relational database normal forms

August, 21st 2014

4NF

● {Employee} ->> {Dream}

● {Employee} ->> {Skill}

● Superkey: {Employee, Dream, Skill}

Page 30: Steam Learn: Relational database normal forms

August, 21st 2014

5NF

BR6.1: Dreams have an IDBR6.2: 2 Dreams cannot have the same IDBR6.3: Dreams have a NameBR6.4: Dreams have a DescriptionBR6.5: Dreams have an Aim

BR2.1: Dreams have a risk level

Page 31: Steam Learn: Relational database normal forms

August, 21st 2014

5NFDream_Name

Dream ID Name1 Rainy Downtown2 Hotel3 Snowy Mountain4 Limbo

Dream_RiskDream ID Risk Level

1 Medium2 Medium3 High4 Extreme

Dream_DescriptionDream ID Description

1 A regular city2 A classy Hotel3 A fortress4 Infinite raw subconscious

Dream_AimDream ID Aim

1 Go Deeper2 Go Deeper3 Plant Idea4 Escape

Page 32: Steam Learn: Relational database normal forms

August, 21st 2014

5NF

Every non-trivial join dependency in the table is implied by the superkeys of the table

Page 33: Steam Learn: Relational database normal forms

August, 21st 2014

5NF

DreamDream ID Name Risk Level Description Aim

1 Rainy Downtown Medium A regular city Go Deeper2 Hotel Medium A classy Hotel Go Deeper3 Snowy Mountain High A fortress Plant Idea4 Limbo Extreme Infinite raw subconscious Escape

Dream_NameDream ID Name

1 Rainy Downtown2 Hotel3 Snowy Mountain4 Limbo

Dream_RiskDream ID Risk Level

1 Medium2 Medium3 High4 Extreme

Dream_DescriptionDream ID Description

1 A regular city2 A classy Hotel3 A fortress4 Infinite raw subconscious

Dream_AimDream ID Aim

1 Go Deeper2 Go Deeper3 Plant Idea4 Escape

Page 34: Steam Learn: Relational database normal forms

August, 21st 2014

Conclusion

● Common sense

● It is not about good and bad design: if you business

requires a non-normal form, it is OK

○ Normalise first, denormalisation should be used as

an optimisation

Page 35: Steam Learn: Relational database normal forms

August, 21st 2014

For online questions, please leave a comment on the article.

Questions ?

Page 36: Steam Learn: Relational database normal forms

August, 21st 2014

Join the community !(in Paris)

Social networks :● Follow us on Twitter : https://twitter.com/steamlearn● Like us on Facebook : https://www.facebook.com/steamlearn

SteamLearn is an Inovia initiative : inovia.fr

You wish to be in the audience ? Contact us at [email protected]