steam learn: relational database normal forms
Post on 15-Jul-2015
104 Views
Preview:
TRANSCRIPT
August, 21st 2014
Relational Database Normal FormsDreams, inc case study
by Romain Francez
August, 21st 2014
Disclaimer
● Not only about showing good database design
● About understanding normal forms
August, 21st 2014
My Boss
August, 21st 2014
Started managing with that
August, 21st 2014
Started managing with that
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
August, 21st 2014
Nomenclature
● Business Rules (BR)
○ Some normal forms take into account your business
logic
● Tables
○ Table name
○ Column name
○ Key
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?
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
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?
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
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
August, 21st 2014
Atomic?
● Something that cannot be subdivided
● String, can be subdivided into substrings, but still
considered as atomic
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
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?
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
August, 21st 2014
2NF
Every non-prime attribute of the table is dependent on the whole of a candidate key
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}
August, 21st 2014
Superkey
{Colour}or{Hex}
ColourColour Hex R G BRed FF0000 255 0 0
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
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
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
August, 21st 2014
3NF
All the attributes in a table are dependent on the primary key and only on the primary key
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
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?
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
August, 21st 2014
4NF
Every non-trivial multivalued dependency in the table is a dependency on a superkey
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
August, 21st 2014
4NF
● {Employee} ->> {Dream}
● {Employee} ->> {Skill}
● Superkey: {Employee, Dream, Skill}
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
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
August, 21st 2014
5NF
Every non-trivial join dependency in the table is implied by the superkeys of the table
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
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
August, 21st 2014
For online questions, please leave a comment on the article.
Questions ?
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 learn@steamhouse.fr
top related