SlideShare una empresa de Scribd logo
1 de 29
Database Design
David Kelleher
www.davidk.net
Database Design
Database Model: Entities and Attributes
Implemented (MySQL): Tables and Fields or Columns
Unnormalized Example
Attribute Record #1 Record #2
Car ID 1382 1399
Make & Model Porsche GT3 Bentley Continental GT3
Year of Manufacture 2005 2014
Miles 16,556 16,556
Color White White
Prior Owner #1 Montgomery Burns
Prior Owner #2 Waylon Smithers
Price $79,990 $190,625
Buyer’s Name Marjorie Bouvier Marge Simpson
Buyer’s Phone 555-3283 555-8707
Primary Keys
Uniquely Identifies Record
Be careful.
Social Security Number isn’t a good primary key:
• Could change after identity theft
• Might be reused over time
• Doesn’t exist for all users
• Has privacy issues
Normalization
Model and normalize your entities to avoid
redundancies, anomalies, and other errors
"Each of the normal forms is defined in a very
cryptic way; even when put into layman’s terms,
they can still be confounding." - Larry Ullman
First Normal Form
• Each attribute contains only 1 value
• Entities cannot have repeating groups of data
First Normal Form
Bad – Is Not 1NF
Car Entity
Car ID
Make & Model
Year of Manufacture
Miles
Color
Prior Owner #1
Prior Owner #2
Price
Buyer’s Name
Buyer’s Phone
First Normal Form
Better – Is 1NF
Car Entity
Car ID
Make
Model
Year of Manufacture
Miles
Color
Price
Buyer’s First Name
Buyer’s Last Name
Buyer’s Phone
Prior Owner Entity
Owner ID
Car ID
First Name
Last Name
Functional Dependency
The value of an attribute should be determined by
the primary key, and only by the primary key
For example, specific information about a car
(make, model, prior owners) can be determined by
looking up the VIN number. So those attributes are
functionally dependent on the VIN number key.
Functional Dependency
If an entity’s attributes are not functionally
dependent on the primary key, these problems
occur:
• Repeating, redundant entries in fields
• Update anomalies
Second Normal Form
• If the primary key is a composite key consisting of
multiple attributes, other attributes must not be
functionally dependent on just a subset of the
primary key
http://www.bkent.net/Doc/simple5.htm
Composite Key
The primary key, identifying a specific card, is a
composite of two attributes – the Rank and Suit.
Attribute Record #1 Record #2 Record #3 Record #4
Card Rank Jack Seven Queen Queen
Card Suit Spades Hearts Hearts Diamonds
Point Value 10 7 10 10
Second Normal Form
Bad – Is Not 2NF
Playing Card Entity
Card Rank
Card Suit
Point Value
Second Normal Form
Better – Is 2NF
Playing Card Entity
Card Rank
Card Suit
Card Rank Entity
Card Rank
Point Value
Note:
The primary key of the playing card entity is
still a composite of the Card Rank ID and
Card Suit
Second Normal Form
Also OK – Is 2NF
Playing Card Entity
Card ID
Rank
Suit
Point Value
MySQL developers usually add
surrogate primary keys (“IDs”)
to every entity instead of using
natural primary keys (real world
attributes like VIN numbers)
While that doesn’t solve functional dependency problems,
entities with a single ID primary key always meet the
conditions for second normal form
Third Normal Form
• Attributes must be functionally dependent on the
primary key, and not on any other attributes
Find repeating entries & update anomalies:
Attribute Record #1 Record #2
Car ID 1382 1399
Make Porsche Bentley
Model GT3 GT3
Year of Manufacture 2005 2014
Miles 16,556 16,556
Color White White
Price $79,990 $190,625
Buyer’s First Name Marjorie Marge
Buyer’s Last Name Bouvier Simpson
Buyer’s Phone 555-3283 555-8707
Model with many-to-many relationships
Relationships
Many to many relationships are not allowed in
MySQL applications.
Create an intermediate entity with one-to-many
relationships between the primary keys of the two
entities.
Third Normal Form
Take a Deep Breath…
Don’t worry so much about whether a functional
dependency problem is a 2nd normal form issue or a
3rd normal form issue.
Just make sure you fix both types of problems.
Functional Dependency Pledge
“Every non-key attribute must provide a fact about
the key, the whole key, and nothing but the key....
so help me Codd*”
*E.F. Codd was the inventor of Relational Databases
and normalization
Indexes Save the Day!
• Where clause
• Order By clause
• Joins
Delete Anomalies
In our car database model, if you delete records
from the make, model, or person table, foreign
keys would be orphaned in other tables.
Delete Anomalies
Foreign Key Constraints
Foreign key constraints can be created in the
database implementation, with two approaches:
• Prevent the delete of a record where its primary
key exists as a foreign key in another table.
• Cascade the delete of a record, removing linked
records from the other tables
Soft Deletes
Instead of deleting records from a database, you
could:
• Add an active attribute. Updating your queries to
only select active records will hide the records
you no longer want to show.
• Move the records to an archival table.
Inner vs Outer Join
Inner joins are done to select records that have
entries linked to records in all joined tables.
Outer joins are done to select all records, whether
or not they are linked to the joined tables.
Database Design
David Kelleher
www.davidk.net

Más contenido relacionado

Similar a Database Design and Normalization

Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working Together
Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working TogetherGeek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working Together
Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working TogetherIDERA Software
 
Karen Lopez 10 Physical Data Modeling Blunders
Karen Lopez 10 Physical Data Modeling BlundersKaren Lopez 10 Physical Data Modeling Blunders
Karen Lopez 10 Physical Data Modeling BlundersKaren Lopez
 
3 where are my keys - sql explore
3   where are my keys - sql explore3   where are my keys - sql explore
3 where are my keys - sql exploresqlserver.co.il
 
Rulette : A pragmatic business rule management library
Rulette : A pragmatic business rule management libraryRulette : A pragmatic business rule management library
Rulette : A pragmatic business rule management libraryKislay Verma
 
Index the obvious and not so obvious
Index the obvious and not so obviousIndex the obvious and not so obvious
Index the obvious and not so obviousHarry Zheng
 
1RUNNING HEAD Normalization2NormalizationNORM.docx
1RUNNING HEAD Normalization2NormalizationNORM.docx1RUNNING HEAD Normalization2NormalizationNORM.docx
1RUNNING HEAD Normalization2NormalizationNORM.docxdrennanmicah
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfSadiaSharmin40
 
Database models unit 1 part 2
Database models unit 1  part 2Database models unit 1  part 2
Database models unit 1 part 2Ram Paliwal
 
10 things to avoid in data model 09242010
10 things to avoid in data model 0924201010 things to avoid in data model 09242010
10 things to avoid in data model 09242010ERwin Modeling
 
Understanding optistruct & LS-Dyna files using text editor
Understanding optistruct & LS-Dyna files using text editorUnderstanding optistruct & LS-Dyna files using text editor
Understanding optistruct & LS-Dyna files using text editorAkshay Mistri
 
Encryption and Tokenization: Friend or Foe?
Encryption and Tokenization: Friend or Foe?Encryption and Tokenization: Friend or Foe?
Encryption and Tokenization: Friend or Foe?Zach Gardner
 
Advanced data modeling
Advanced data modelingAdvanced data modeling
Advanced data modelingDhani Ahmad
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Citus Data
 
Data Con LA 2022 - Open Source Large Knowledge Graph Factory
Data Con LA 2022 - Open Source Large Knowledge Graph FactoryData Con LA 2022 - Open Source Large Knowledge Graph Factory
Data Con LA 2022 - Open Source Large Knowledge Graph FactoryData Con LA
 
SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksIke Ellis
 
PHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHPPHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHPDave Stokes
 

Similar a Database Design and Normalization (20)

Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working Together
Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working TogetherGeek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working Together
Geek Sync | Looking "Normal" and Seeing "Stars": Table Designs Working Together
 
DBMS:Relational Modal
DBMS:Relational ModalDBMS:Relational Modal
DBMS:Relational Modal
 
Karen Lopez 10 Physical Data Modeling Blunders
Karen Lopez 10 Physical Data Modeling BlundersKaren Lopez 10 Physical Data Modeling Blunders
Karen Lopez 10 Physical Data Modeling Blunders
 
3 where are my keys - sql explore
3   where are my keys - sql explore3   where are my keys - sql explore
3 where are my keys - sql explore
 
Understanding indices
Understanding indicesUnderstanding indices
Understanding indices
 
Rulette : A pragmatic business rule management library
Rulette : A pragmatic business rule management libraryRulette : A pragmatic business rule management library
Rulette : A pragmatic business rule management library
 
Index the obvious and not so obvious
Index the obvious and not so obviousIndex the obvious and not so obvious
Index the obvious and not so obvious
 
1RUNNING HEAD Normalization2NormalizationNORM.docx
1RUNNING HEAD Normalization2NormalizationNORM.docx1RUNNING HEAD Normalization2NormalizationNORM.docx
1RUNNING HEAD Normalization2NormalizationNORM.docx
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdf
 
Database models unit 1 part 2
Database models unit 1  part 2Database models unit 1  part 2
Database models unit 1 part 2
 
10 things to avoid in data model 09242010
10 things to avoid in data model 0924201010 things to avoid in data model 09242010
10 things to avoid in data model 09242010
 
Understanding optistruct & LS-Dyna files using text editor
Understanding optistruct & LS-Dyna files using text editorUnderstanding optistruct & LS-Dyna files using text editor
Understanding optistruct & LS-Dyna files using text editor
 
Encryption and Tokenization: Friend or Foe?
Encryption and Tokenization: Friend or Foe?Encryption and Tokenization: Friend or Foe?
Encryption and Tokenization: Friend or Foe?
 
Advanced data modeling
Advanced data modelingAdvanced data modeling
Advanced data modeling
 
Metadata Matters
Metadata MattersMetadata Matters
Metadata Matters
 
Oscar Engineering part 1
Oscar Engineering part 1Oscar Engineering part 1
Oscar Engineering part 1
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
 
Data Con LA 2022 - Open Source Large Knowledge Graph Factory
Data Con LA 2022 - Open Source Large Knowledge Graph FactoryData Con LA 2022 - Open Source Large Knowledge Graph Factory
Data Con LA 2022 - Open Source Large Knowledge Graph Factory
 
SQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & TricksSQL Pass Architecture SQL Tips & Tricks
SQL Pass Architecture SQL Tips & Tricks
 
PHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHPPHP Database Programming Basics -- Northeast PHP
PHP Database Programming Basics -- Northeast PHP
 

Más de Dave Kelleher

Uml Diagrams for Web Developers
Uml Diagrams for Web DevelopersUml Diagrams for Web Developers
Uml Diagrams for Web DevelopersDave Kelleher
 
Semantic HTML5 and JSON-LD
Semantic HTML5 and JSON-LDSemantic HTML5 and JSON-LD
Semantic HTML5 and JSON-LDDave Kelleher
 
Really Fast HTML5 Game Development with CreateJS
Really Fast HTML5 Game Development with CreateJSReally Fast HTML5 Game Development with CreateJS
Really Fast HTML5 Game Development with CreateJSDave Kelleher
 
Semantic HTML5 and Microdata
Semantic HTML5 and MicrodataSemantic HTML5 and Microdata
Semantic HTML5 and MicrodataDave Kelleher
 
Reverse Card Sort for UX Testing
Reverse Card Sort for UX TestingReverse Card Sort for UX Testing
Reverse Card Sort for UX TestingDave Kelleher
 

Más de Dave Kelleher (9)

Uml Diagrams for Web Developers
Uml Diagrams for Web DevelopersUml Diagrams for Web Developers
Uml Diagrams for Web Developers
 
Semantic HTML5 and JSON-LD
Semantic HTML5 and JSON-LDSemantic HTML5 and JSON-LD
Semantic HTML5 and JSON-LD
 
Really Fast HTML5 Game Development with CreateJS
Really Fast HTML5 Game Development with CreateJSReally Fast HTML5 Game Development with CreateJS
Really Fast HTML5 Game Development with CreateJS
 
Debugging PHP Code
Debugging PHP CodeDebugging PHP Code
Debugging PHP Code
 
Cinematic UX Design
Cinematic UX DesignCinematic UX Design
Cinematic UX Design
 
Cryptocurrencies
CryptocurrenciesCryptocurrencies
Cryptocurrencies
 
Cinematic UX Design
Cinematic UX DesignCinematic UX Design
Cinematic UX Design
 
Semantic HTML5 and Microdata
Semantic HTML5 and MicrodataSemantic HTML5 and Microdata
Semantic HTML5 and Microdata
 
Reverse Card Sort for UX Testing
Reverse Card Sort for UX TestingReverse Card Sort for UX Testing
Reverse Card Sort for UX Testing
 

Último

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 

Último (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 

Database Design and Normalization

  • 2. Database Design Database Model: Entities and Attributes Implemented (MySQL): Tables and Fields or Columns
  • 3. Unnormalized Example Attribute Record #1 Record #2 Car ID 1382 1399 Make & Model Porsche GT3 Bentley Continental GT3 Year of Manufacture 2005 2014 Miles 16,556 16,556 Color White White Prior Owner #1 Montgomery Burns Prior Owner #2 Waylon Smithers Price $79,990 $190,625 Buyer’s Name Marjorie Bouvier Marge Simpson Buyer’s Phone 555-3283 555-8707
  • 4. Primary Keys Uniquely Identifies Record Be careful. Social Security Number isn’t a good primary key: • Could change after identity theft • Might be reused over time • Doesn’t exist for all users • Has privacy issues
  • 5. Normalization Model and normalize your entities to avoid redundancies, anomalies, and other errors "Each of the normal forms is defined in a very cryptic way; even when put into layman’s terms, they can still be confounding." - Larry Ullman
  • 6. First Normal Form • Each attribute contains only 1 value • Entities cannot have repeating groups of data
  • 7. First Normal Form Bad – Is Not 1NF Car Entity Car ID Make & Model Year of Manufacture Miles Color Prior Owner #1 Prior Owner #2 Price Buyer’s Name Buyer’s Phone
  • 8. First Normal Form Better – Is 1NF Car Entity Car ID Make Model Year of Manufacture Miles Color Price Buyer’s First Name Buyer’s Last Name Buyer’s Phone Prior Owner Entity Owner ID Car ID First Name Last Name
  • 9. Functional Dependency The value of an attribute should be determined by the primary key, and only by the primary key For example, specific information about a car (make, model, prior owners) can be determined by looking up the VIN number. So those attributes are functionally dependent on the VIN number key.
  • 10. Functional Dependency If an entity’s attributes are not functionally dependent on the primary key, these problems occur: • Repeating, redundant entries in fields • Update anomalies
  • 11. Second Normal Form • If the primary key is a composite key consisting of multiple attributes, other attributes must not be functionally dependent on just a subset of the primary key http://www.bkent.net/Doc/simple5.htm
  • 12. Composite Key The primary key, identifying a specific card, is a composite of two attributes – the Rank and Suit. Attribute Record #1 Record #2 Record #3 Record #4 Card Rank Jack Seven Queen Queen Card Suit Spades Hearts Hearts Diamonds Point Value 10 7 10 10
  • 13. Second Normal Form Bad – Is Not 2NF Playing Card Entity Card Rank Card Suit Point Value
  • 14. Second Normal Form Better – Is 2NF Playing Card Entity Card Rank Card Suit Card Rank Entity Card Rank Point Value Note: The primary key of the playing card entity is still a composite of the Card Rank ID and Card Suit
  • 15. Second Normal Form Also OK – Is 2NF Playing Card Entity Card ID Rank Suit Point Value MySQL developers usually add surrogate primary keys (“IDs”) to every entity instead of using natural primary keys (real world attributes like VIN numbers) While that doesn’t solve functional dependency problems, entities with a single ID primary key always meet the conditions for second normal form
  • 16. Third Normal Form • Attributes must be functionally dependent on the primary key, and not on any other attributes
  • 17. Find repeating entries & update anomalies: Attribute Record #1 Record #2 Car ID 1382 1399 Make Porsche Bentley Model GT3 GT3 Year of Manufacture 2005 2014 Miles 16,556 16,556 Color White White Price $79,990 $190,625 Buyer’s First Name Marjorie Marge Buyer’s Last Name Bouvier Simpson Buyer’s Phone 555-3283 555-8707
  • 18. Model with many-to-many relationships
  • 19. Relationships Many to many relationships are not allowed in MySQL applications. Create an intermediate entity with one-to-many relationships between the primary keys of the two entities.
  • 21. Take a Deep Breath… Don’t worry so much about whether a functional dependency problem is a 2nd normal form issue or a 3rd normal form issue. Just make sure you fix both types of problems.
  • 22. Functional Dependency Pledge “Every non-key attribute must provide a fact about the key, the whole key, and nothing but the key.... so help me Codd*” *E.F. Codd was the inventor of Relational Databases and normalization
  • 23. Indexes Save the Day! • Where clause • Order By clause • Joins
  • 24. Delete Anomalies In our car database model, if you delete records from the make, model, or person table, foreign keys would be orphaned in other tables.
  • 26. Foreign Key Constraints Foreign key constraints can be created in the database implementation, with two approaches: • Prevent the delete of a record where its primary key exists as a foreign key in another table. • Cascade the delete of a record, removing linked records from the other tables
  • 27. Soft Deletes Instead of deleting records from a database, you could: • Add an active attribute. Updating your queries to only select active records will hide the records you no longer want to show. • Move the records to an archival table.
  • 28. Inner vs Outer Join Inner joins are done to select records that have entries linked to records in all joined tables. Outer joins are done to select all records, whether or not they are linked to the joined tables.