SlideShare a Scribd company logo
Database Design Thoughts
Toon Koppelaars
Real-World Performance
Oracle Server Technologies
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, timing, and pricing of any
features or functionality described for Oracle’s products may change and remains at the
sole discretion of Oracle Corporation.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
About Me
• Part of Oracle eco-system since 1987
– Have done and seen quite a lot of application development
– Database design, SQL and PL/SQL
• Big fan of “Using Database As a Processing Engine”
– Not just as a persistence layer
• Member of Oracle’s Real-World Performance Group
3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Terminology
• Data model versus, database design
• Three major data models: hierarchical (60’s), network (70’s) and relational
– https://en.wikipedia.org/wiki/Hierarchical_database_model (IMS)
– https://en.wikipedia.org/wiki/Network_model (CODASYL)
– https://en.wikipedia.org/wiki/Relational_model (SQL)
• This talk is about designing databases within the relational data model
4
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
45 Minutes
• Database design is subjective field
• Mix of “art”, “experience” and “science”
– Art  different tastes
– Experience  everybody his/her own
• I’ll avoid the art and experience aspects, and only provide some science in
this talk
– And even then, I had to choose from many topics that could have been discussed
5
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Contents
1. Dependencies, Keys and Third Normal Form
2. Boyce-Codd Normal Form
3. Some Logic
4. Nulls, Generalization and Specializations
5. Recommended Practices
6. Snack: Something to Think About
6
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Dependencies, Keys and Third Normal Form
• Database design == Coherent set of table designs
• We’re going straight in now...
• With an ORDERLINES table design
7
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Take a Look at This Table
8
Big chunk of table design, is constraint design
Very important one
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Important Dependencies Concept
• Assume ORDERLINES table has following properties:
– If two rows have same PRODUCT#, then PRODUCT_SHORTNAME must also be same
– If two rows have same ORDER#, then CUSTOMER_ID, ORDER_DATE and
DELIVERY_ADDR must also (each) be same
– If two rows have same CUSTOMER_ID, then DISCOUNT_PERC must also be same
9
These are in fact
additional constraints
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Specifying a Dependency Constraint
Using FOPL
FOPL: First Order Predicate Logic
Using SQL
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Table Design and Constraint Design
These should be part of
your database design
documentation
These should be part of
your database design
documentation
https://community.oracle.com/ideas/13028
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Dependencies
• We say, “table has five dependencies”
– PRODUCT# “determines” PRODUCT_SHORTNAME
– CUSTOMER_ID “determines” DISCOUNT_PERC
– ORDER# “determines” CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDR
• Or other way around,
– PRODUCT_SHORTNAME “depends upon” PRODUCT#
– DISCOUNT_PERC “depends upon” CUSTOMER_ID
– CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDR “depend upon” ORDER#
12
Being able to spot dependencies and other types of
constraints in table designs is very important skill
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Can You Spot the Dependency Here?
13
What’s wrong with such table design?
Why do you not want this?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Dependencies
Empno Ename Deptno Salary Dname
101 Mike 30 4200 London
102 Toon 20 1200 Amsterdam
103 Mark 30 1800 London
104 John 40 4000 San Francisco
105 Bob 40 3400 San Francisco
EMPLOYEES table
Dname is not
stored once…
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Moving On To Keys
• Understanding concepts of dependencies and keys are essential
• These two concepts drive:
– What tables you introduce
– What columns go to which table
15
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Keys (Of a Table)
• Primary key dictates that we
do not allow two rows for a
given ORDER# with the same
PRODUCT#
16
Being able to reason about / find
multi-column (concatenated)
keys, is very important skill
What does this mean? (3x)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Third Normal Form (3NF)
• If table has dependencies where left-hand
is not a key in table, then table is not
in 3NF
• So, how do we fix ORDERLINES to be in 3NF?
17
None of these is a key in OrderLines
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fixing ORDERLINES to be in 3NF
• PRODUCT_SHORTNAME: needs to move to table where PRODUCT# is key
• DISCOUNT_PERC: needs to move to table where CUSTOMER_ID is key
• CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDDR:
need to move to table where ORDER# is key
• This potentially leads to introducing new tables
18
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
3NF Table Design
19
If columns were already
present in other tables,
then they were redundant
in ORDERLINES
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Contents
1. Dependencies, Keys and Third Normal Form
2. Boyce-Codd Normal Form
3. Some Logic
4. Nulls, Generalization and Specializations
5. Recommended Practices
20
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Boyce-Codd Normal Form (BCNF)
• BCNF is a slightly stricter form of 3NF
– The way 3NF was formally defined, leaves room for BCNF to exist
– 3NF only talks about dependencies for non-prime attributes (columns)
• Just remember this:
A table might not be in BCNF if and only if, it has overlapping
concatenated candidate keys
– Eg: Primary key (C1,C2) and Unique (C2,C3)
– If “C3 depends on C1”, or “C1 depends on C3 “ not in BCNF
21
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Contents
1. Dependencies, Keys and Third Normal Form
2. Boyce-Codd Normal Form
3. Some Logic
4. Nulls, Generalization and Specializations
5. Recommended Practices
22
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Some Logic You Should Know
• When reasoning about WHERE-clauses or constraints:
– De Morgan Law
– Implication Rewrite Rule
• There is a lot more 
23
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
De Morgan
• Transforming logical-OR into logical-AND, and vice-versa
– Not (A and B) is-equivalent-to not-A or not-B
– Not (A or B) is-equivalent-to not-A and not-B
• Sometimes good to play with this in WHERE-clauses or in CHECK
constraints
24
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Implication Rewrite Into Or (very important!)
• Transforming if-then into logical-OR, and vice-versa
– A implies B is-equivalent-to not-A or B
– A or B is-equivalent-to not-A implies B
or not-B implies A
– A implies B is-equivalent-to not-B implies not-A
• You often require this for multi-column CHECK constraints
25
If A is true then B is true
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Contents
1. Dependencies, Keys and Third Normal Form
2. Boyce-Codd Normal Form
3. Some Logic
4. Nulls, Generalization and Specializations
5. Recommended Practices
26
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
What’s the Issue With Nulls in Database Designs?
• They introduce all kinds of edge-cases and divergent behavior:
– “= null”, is not the same as, “is null”
– Empty string equals null?
– Ordering of nulls
– Aggregate functions ignore nulls
– Predicate with nulls behave differently in WHERE- and CHECK-clauses
27
Simply put: they are a prime source of bugs
in your code on top of database design
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
How Come We See Nulls so Often?
• Because we have nullable columns…
28
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
To Be Applicable or Not
• Nullable columns introduced for three reasons:
1. Data values that could have been stored are inapplicable
2. Data values are not yet, or no longer, applicable  special case of 1
3. Data values are nice to know (if known) or truly optional
• Let’s look at first one
29
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Reason 1: Data Values Are Inapplicable
• In EMP-DEPT language: COMM column is only applicable for salesmen
– You really should have constraints then:
• Check (JOB != ‘salesman’ or COMM is not null)
• Check (JOB = ‘salesman’ or COMM is null)
• Often there are multiple sets of inapplicable columns
– Depending on value in some other column only one of the sets should have values
– That column = the inspection column (JOB above)
30
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Causing Wide Tables
31
Mandatory cols Optional col-set 1 Optional col-set 2 Optional col-set 3
Single, wide, table
Data values Data values
Type 1
rows
Data values Data values
Type 2
rows
Data values Data values
Type 3
rows
Inspection column here
Optional columns
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
What Could Be Alternative Designs?
• Without all the NULLs?
32
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fix: Introduce More Tables
33
Four tables
Col-set 1
Col-set 2
Col-set 3
Including same key-
column(s) of main table
Including same key-
column(s) of main table
Including same key-
column(s) of main table
Type 1
rows
Type 2
rows
Type 3
rows
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Generalization and Specialization Tables
34
Aka: super-type
and sub-type tables
Generalization table
Specialization table 1
Specialization table 2
Specialization table 3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Or, Another Alternative With Three Tables
35
Split into
three tables
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Or, Another Alternative With Three Tables
36
Split into
three tables
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Or, Another Alternative With Three Tables
37
And merge
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Advantages of Alternative Designs
• Minimal number of nullable columns  less bugs
• No bytes wasted storing Nulls
– Often additional columns will have been added…
– Introducing massive row-chaining
38
1 byte/column/row
0 bytes
Now these suddenly also
take 1 byte/column/row
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Advantages of Alternative Design
• Many data access use-cases require columns of one (or two) tables
– Better packed data, quicker access to column  performs better
• New columns can go straight to correct table, without requiring massive
table and index rebuilds due to row-chaining
– Easier maintenance, less impact
39
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
But Then, Don’t Do This
40
Wide view, outer-joining main table with 3 tables
All your SQL accesses
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Do This
41
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Or This
42
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Or This
43
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
And, Yes, Sometimes This
44
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Reason 2: Not Yet (or No Longer) Applicable
• Let’s look at an ORDERS table with STATUS column
45
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Reason 2: Not Yet (or No Longer) Applicable
• DELIVERY_DATE only applicable for Delivered orders
• CANCEL_REASON only applicable for Cancelled orders
46
Constraints…
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Reason 3: Nice to Know or Truly Optional
• If we know release-date of product, lets store it
• Some products have special-handling-instructions
47
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
My View on Nulls
• Nulls due to nice-to-know/truly-optional  don’t worry about them
– Likely not used in SQL logic and/or business logic
• Avoid nulls due to inapplicability
– Introduce generalization/specializations
• Seriously consider avoiding nulls due to not-yet/no-longer applicable
– How? By also introducing new (specialization) tables for these columns
48
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Final Loose Thought on NULL’s
• Not Null should have been the default
• Database designs should have few nullable columns
• They are not constraints…
• Trying to insert NULL should just be a data-type error
49
This is an historic
mistake imho
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Contents
1. Dependencies, Keys and Third Normal Form
2. Boyce-Codd Normal Form
3. Some Logic
4. Nulls, Generalization and Specializations
5. Recommended Practices
50
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Recommended DB Design Practices
• Application should be based on proper DB design
• "Proper" = two-fold
1. It must fit business requirements
2. It must follow sound relational principles
1) Is up to you talking to business owners and understanding their
requirements  experience (and art?)
2) Is about science + objective reasoning on alternative designs
Alternative table designs + alternative constraint designs
51
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Recommended Practices For a Sound Database Design
0. Choose table and column names wisely
1. Use 3rd normal form
Avoid structural redundancies (dependencies)
2. Don't design incidental redundancies
Aka premature optimization
E.g. Total value of order-lines at order level
3. Table has sequence-based primary key?
Then also have 2nd business key, usually concatenated key
4. Put special things in separate tables (subtypes/specializations)
Avoid lists of null-able columns due to non-applicability
5. Never allow nulls in columns involved in data integrity constraints
Includes columns referenced by any SQL assertion you can document
52
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Conclusions
• There is a lot of science to database design
• Database design is also constraint design
• Don’t be afraid of many tables  DBMS’s were born to join
• You might end up with foundation for your application that is easier to
understand, is easier to maintain, and provides better performance
53
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Snack: Something to Think About
54
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
The Value of Sharing Database Design Pictures
• Do you share database design pictures with end-users?
– And point to tables on them while talking about them?
• Why?
– Do you think users understand these?
55
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
The Value of Sharing Meteorological Pictures With Viewers
• Seen these?
• Do you understand them?
– Blue, red, purple lines
– Dashed lines
– Little triangles, half circles
– Sometimes combined
– Capital L’s, capital H’s
– Vague white lines
• Does seeing these, add value to you?
56
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Much Detail in Them That We Don’t Get...
57
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
What You’re Interested In
58
For your location
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
My Opinion: Little Value in Sharing Them
• Like weather pictures:
– Users don’t get the information held within them
– Few users are interested in whole picture
– These pictures miss lot of information (think: constraints/assertions)
• Database design is frame of reference for database designer and
application developer
59
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
What Users Are Interested In
60
For their use-cases
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Thank You For Your Time
61
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 62

More Related Content

PDF
SmartDB Office Hours: Connection Pool Sizing Concepts
PPTX
Database Core performance principles
PDF
Apouc 2014-enterprise-manager-12c
PDF
HTTP/2 Comes to Java - What Servlet 4.0 Means to You
PDF
Node.js and Oracle Database: New Development Techniques
PDF
CON5898 What Servlet 4.0 Means To You
PDF
OOW-TBE-12c-CON7307-Sharable
PDF
Developer day v2
SmartDB Office Hours: Connection Pool Sizing Concepts
Database Core performance principles
Apouc 2014-enterprise-manager-12c
HTTP/2 Comes to Java - What Servlet 4.0 Means to You
Node.js and Oracle Database: New Development Techniques
CON5898 What Servlet 4.0 Means To You
OOW-TBE-12c-CON7307-Sharable
Developer day v2

What's hot (20)

PPTX
Java EE 8: What Servlet 4 and HTTP2 Mean
PPTX
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
PDF
Web protocols for java developers
PDF
Oracle IaaS Overview - AIOUG Hyderabad Chapter
PDF
Using Machine Learning to Debug complex Oracle RAC Issues
PDF
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
PDF
APEX – jak vytvořit jednoduše aplikaci
PDF
MAA Best Practices for Oracle Database 19c
PPTX
Deep Dive - Usage of on premises data gateway for hybrid integration scenarios
PDF
TechEvent 2019: Create a Private Database Cloud in the Public Cloud using the...
PPTX
Approaches for WebLogic Server in the Cloud (OpenWorld, September 2014)
PDF
Spotlight private dns-oraclecloudservices
PDF
Oracle Data Protection - 1. část
PDF
HTTP/2 Comes to Java
PPTX
Storage and-compute-hdfs-map reduce
PDF
Oracle Cloud – Application Performance Monitoring
PDF
Oracle RAC 19c with Standard Edition (SE) 2 - Support Update
PDF
Supercharge your Code to get optimal Database Performance
PDF
Aman sharma hyd_12crac High Availability Day 2015
PDF
Omaha rug customer 2 cloud customer facing hcm ppt aug 2014
Java EE 8: What Servlet 4 and HTTP2 Mean
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
Web protocols for java developers
Oracle IaaS Overview - AIOUG Hyderabad Chapter
Using Machine Learning to Debug complex Oracle RAC Issues
Con7091 sql tuning for expert db as-oow17_oct2_1507314871265001m0x4
APEX – jak vytvořit jednoduše aplikaci
MAA Best Practices for Oracle Database 19c
Deep Dive - Usage of on premises data gateway for hybrid integration scenarios
TechEvent 2019: Create a Private Database Cloud in the Public Cloud using the...
Approaches for WebLogic Server in the Cloud (OpenWorld, September 2014)
Spotlight private dns-oraclecloudservices
Oracle Data Protection - 1. část
HTTP/2 Comes to Java
Storage and-compute-hdfs-map reduce
Oracle Cloud – Application Performance Monitoring
Oracle RAC 19c with Standard Edition (SE) 2 - Support Update
Supercharge your Code to get optimal Database Performance
Aman sharma hyd_12crac High Availability Day 2015
Omaha rug customer 2 cloud customer facing hcm ppt aug 2014
Ad

Similar to Database Design Thoughts (20)

PPTX
Gartner pace and bi-modal models
PDF
How To Model and Construct Graphs with Oracle Database (AskTOM Office Hours p...
PPTX
The Changing Role of a DBA in an Autonomous World
PDF
Everything You Need to Know About Oracle 12c Indexes
PDF
Sloupcové uložení dat a použití in-memory technologií u řešení Exadata
PDF
Cranking It Up - SuiteWorld 2017
PPTX
HOW TO SAVE PILEs of $$$ BY CREATING THE BEST DATA MODEL THE FIRST TIME (Ksc...
PPTX
Perth APAC Groundbreakers tour - The Autonomous Database
PPTX
AMIS Oracle OpenWorld en Code One Review 2018 - Pillar 1: Data
PPTX
AMIS Oracle OpenWorld & CodeOne Review - Pillar 1 - Data (5 november 2018)
PDF
Database Basics with PHP -- Connect JS Conference October 17th, 2015
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
PPTX
PDF
Utilizing BI 11g Reporting To Get The Most Out of P6
PDF
Suiteworld Oracle & Netsuite: IDENT Oil & Gas Solution Case Study
PPTX
Why citizen developers should be your new best friend - Oracle APEX
PDF
Web adi webcast_v3
PDF
Diagnose Your Microservices
PDF
(Oracle) DBA and Other Skills Needed in 2020
PDF
#OOW16 - Introduction to Advanced Access Controls
Gartner pace and bi-modal models
How To Model and Construct Graphs with Oracle Database (AskTOM Office Hours p...
The Changing Role of a DBA in an Autonomous World
Everything You Need to Know About Oracle 12c Indexes
Sloupcové uložení dat a použití in-memory technologií u řešení Exadata
Cranking It Up - SuiteWorld 2017
HOW TO SAVE PILEs of $$$ BY CREATING THE BEST DATA MODEL THE FIRST TIME (Ksc...
Perth APAC Groundbreakers tour - The Autonomous Database
AMIS Oracle OpenWorld en Code One Review 2018 - Pillar 1: Data
AMIS Oracle OpenWorld & CodeOne Review - Pillar 1 - Data (5 november 2018)
Database Basics with PHP -- Connect JS Conference October 17th, 2015
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Utilizing BI 11g Reporting To Get The Most Out of P6
Suiteworld Oracle & Netsuite: IDENT Oil & Gas Solution Case Study
Why citizen developers should be your new best friend - Oracle APEX
Web adi webcast_v3
Diagnose Your Microservices
(Oracle) DBA and Other Skills Needed in 2020
#OOW16 - Introduction to Advanced Access Controls
Ad

Recently uploaded (20)

PDF
[EN] Industrial Machine Downtime Prediction
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
IB Computer Science - Internal Assessment.pptx
PDF
Fluorescence-microscope_Botany_detailed content
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
Introduction to machine learning and Linear Models
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
Introduction to the R Programming Language
PDF
Introduction to Data Science and Data Analysis
PDF
Mega Projects Data Mega Projects Data
[EN] Industrial Machine Downtime Prediction
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Clinical guidelines as a resource for EBP(1).pdf
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
IB Computer Science - Internal Assessment.pptx
Fluorescence-microscope_Botany_detailed content
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
STUDY DESIGN details- Lt Col Maksud (21).pptx
Data_Analytics_and_PowerBI_Presentation.pptx
STERILIZATION AND DISINFECTION-1.ppthhhbx
Introduction to machine learning and Linear Models
Business Ppt On Nestle.pptx huunnnhhgfvu
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
climate analysis of Dhaka ,Banglades.pptx
Introduction to the R Programming Language
Introduction to Data Science and Data Analysis
Mega Projects Data Mega Projects Data

Database Design Thoughts

  • 1. Database Design Thoughts Toon Koppelaars Real-World Performance Oracle Server Technologies
  • 2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation.
  • 3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | About Me • Part of Oracle eco-system since 1987 – Have done and seen quite a lot of application development – Database design, SQL and PL/SQL • Big fan of “Using Database As a Processing Engine” – Not just as a persistence layer • Member of Oracle’s Real-World Performance Group 3
  • 4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Terminology • Data model versus, database design • Three major data models: hierarchical (60’s), network (70’s) and relational – https://en.wikipedia.org/wiki/Hierarchical_database_model (IMS) – https://en.wikipedia.org/wiki/Network_model (CODASYL) – https://en.wikipedia.org/wiki/Relational_model (SQL) • This talk is about designing databases within the relational data model 4
  • 5. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 45 Minutes • Database design is subjective field • Mix of “art”, “experience” and “science” – Art  different tastes – Experience  everybody his/her own • I’ll avoid the art and experience aspects, and only provide some science in this talk – And even then, I had to choose from many topics that could have been discussed 5
  • 6. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Contents 1. Dependencies, Keys and Third Normal Form 2. Boyce-Codd Normal Form 3. Some Logic 4. Nulls, Generalization and Specializations 5. Recommended Practices 6. Snack: Something to Think About 6
  • 7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Dependencies, Keys and Third Normal Form • Database design == Coherent set of table designs • We’re going straight in now... • With an ORDERLINES table design 7
  • 8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Take a Look at This Table 8 Big chunk of table design, is constraint design Very important one
  • 9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Important Dependencies Concept • Assume ORDERLINES table has following properties: – If two rows have same PRODUCT#, then PRODUCT_SHORTNAME must also be same – If two rows have same ORDER#, then CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDR must also (each) be same – If two rows have same CUSTOMER_ID, then DISCOUNT_PERC must also be same 9 These are in fact additional constraints
  • 10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Specifying a Dependency Constraint Using FOPL FOPL: First Order Predicate Logic Using SQL
  • 11. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Table Design and Constraint Design These should be part of your database design documentation These should be part of your database design documentation https://community.oracle.com/ideas/13028
  • 12. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Dependencies • We say, “table has five dependencies” – PRODUCT# “determines” PRODUCT_SHORTNAME – CUSTOMER_ID “determines” DISCOUNT_PERC – ORDER# “determines” CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDR • Or other way around, – PRODUCT_SHORTNAME “depends upon” PRODUCT# – DISCOUNT_PERC “depends upon” CUSTOMER_ID – CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDR “depend upon” ORDER# 12 Being able to spot dependencies and other types of constraints in table designs is very important skill
  • 13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Can You Spot the Dependency Here? 13 What’s wrong with such table design? Why do you not want this?
  • 14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Dependencies Empno Ename Deptno Salary Dname 101 Mike 30 4200 London 102 Toon 20 1200 Amsterdam 103 Mark 30 1800 London 104 John 40 4000 San Francisco 105 Bob 40 3400 San Francisco EMPLOYEES table Dname is not stored once…
  • 15. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Moving On To Keys • Understanding concepts of dependencies and keys are essential • These two concepts drive: – What tables you introduce – What columns go to which table 15
  • 16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Keys (Of a Table) • Primary key dictates that we do not allow two rows for a given ORDER# with the same PRODUCT# 16 Being able to reason about / find multi-column (concatenated) keys, is very important skill What does this mean? (3x)
  • 17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Third Normal Form (3NF) • If table has dependencies where left-hand is not a key in table, then table is not in 3NF • So, how do we fix ORDERLINES to be in 3NF? 17 None of these is a key in OrderLines
  • 18. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fixing ORDERLINES to be in 3NF • PRODUCT_SHORTNAME: needs to move to table where PRODUCT# is key • DISCOUNT_PERC: needs to move to table where CUSTOMER_ID is key • CUSTOMER_ID, ORDER_DATE and DELIVERY_ADDDR: need to move to table where ORDER# is key • This potentially leads to introducing new tables 18
  • 19. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 3NF Table Design 19 If columns were already present in other tables, then they were redundant in ORDERLINES
  • 20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Contents 1. Dependencies, Keys and Third Normal Form 2. Boyce-Codd Normal Form 3. Some Logic 4. Nulls, Generalization and Specializations 5. Recommended Practices 20
  • 21. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Boyce-Codd Normal Form (BCNF) • BCNF is a slightly stricter form of 3NF – The way 3NF was formally defined, leaves room for BCNF to exist – 3NF only talks about dependencies for non-prime attributes (columns) • Just remember this: A table might not be in BCNF if and only if, it has overlapping concatenated candidate keys – Eg: Primary key (C1,C2) and Unique (C2,C3) – If “C3 depends on C1”, or “C1 depends on C3 “ not in BCNF 21
  • 22. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Contents 1. Dependencies, Keys and Third Normal Form 2. Boyce-Codd Normal Form 3. Some Logic 4. Nulls, Generalization and Specializations 5. Recommended Practices 22
  • 23. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Some Logic You Should Know • When reasoning about WHERE-clauses or constraints: – De Morgan Law – Implication Rewrite Rule • There is a lot more  23
  • 24. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | De Morgan • Transforming logical-OR into logical-AND, and vice-versa – Not (A and B) is-equivalent-to not-A or not-B – Not (A or B) is-equivalent-to not-A and not-B • Sometimes good to play with this in WHERE-clauses or in CHECK constraints 24
  • 25. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Implication Rewrite Into Or (very important!) • Transforming if-then into logical-OR, and vice-versa – A implies B is-equivalent-to not-A or B – A or B is-equivalent-to not-A implies B or not-B implies A – A implies B is-equivalent-to not-B implies not-A • You often require this for multi-column CHECK constraints 25 If A is true then B is true
  • 26. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Contents 1. Dependencies, Keys and Third Normal Form 2. Boyce-Codd Normal Form 3. Some Logic 4. Nulls, Generalization and Specializations 5. Recommended Practices 26
  • 27. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | What’s the Issue With Nulls in Database Designs? • They introduce all kinds of edge-cases and divergent behavior: – “= null”, is not the same as, “is null” – Empty string equals null? – Ordering of nulls – Aggregate functions ignore nulls – Predicate with nulls behave differently in WHERE- and CHECK-clauses 27 Simply put: they are a prime source of bugs in your code on top of database design
  • 28. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | How Come We See Nulls so Often? • Because we have nullable columns… 28
  • 29. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | To Be Applicable or Not • Nullable columns introduced for three reasons: 1. Data values that could have been stored are inapplicable 2. Data values are not yet, or no longer, applicable  special case of 1 3. Data values are nice to know (if known) or truly optional • Let’s look at first one 29
  • 30. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Reason 1: Data Values Are Inapplicable • In EMP-DEPT language: COMM column is only applicable for salesmen – You really should have constraints then: • Check (JOB != ‘salesman’ or COMM is not null) • Check (JOB = ‘salesman’ or COMM is null) • Often there are multiple sets of inapplicable columns – Depending on value in some other column only one of the sets should have values – That column = the inspection column (JOB above) 30
  • 31. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Causing Wide Tables 31 Mandatory cols Optional col-set 1 Optional col-set 2 Optional col-set 3 Single, wide, table Data values Data values Type 1 rows Data values Data values Type 2 rows Data values Data values Type 3 rows Inspection column here Optional columns
  • 32. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | What Could Be Alternative Designs? • Without all the NULLs? 32
  • 33. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fix: Introduce More Tables 33 Four tables Col-set 1 Col-set 2 Col-set 3 Including same key- column(s) of main table Including same key- column(s) of main table Including same key- column(s) of main table Type 1 rows Type 2 rows Type 3 rows
  • 34. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Generalization and Specialization Tables 34 Aka: super-type and sub-type tables Generalization table Specialization table 1 Specialization table 2 Specialization table 3
  • 35. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Or, Another Alternative With Three Tables 35 Split into three tables
  • 36. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Or, Another Alternative With Three Tables 36 Split into three tables
  • 37. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Or, Another Alternative With Three Tables 37 And merge
  • 38. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Advantages of Alternative Designs • Minimal number of nullable columns  less bugs • No bytes wasted storing Nulls – Often additional columns will have been added… – Introducing massive row-chaining 38 1 byte/column/row 0 bytes Now these suddenly also take 1 byte/column/row
  • 39. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Advantages of Alternative Design • Many data access use-cases require columns of one (or two) tables – Better packed data, quicker access to column  performs better • New columns can go straight to correct table, without requiring massive table and index rebuilds due to row-chaining – Easier maintenance, less impact 39
  • 40. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | But Then, Don’t Do This 40 Wide view, outer-joining main table with 3 tables All your SQL accesses
  • 41. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Do This 41
  • 42. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Or This 42
  • 43. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Or This 43
  • 44. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | And, Yes, Sometimes This 44
  • 45. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Reason 2: Not Yet (or No Longer) Applicable • Let’s look at an ORDERS table with STATUS column 45
  • 46. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Reason 2: Not Yet (or No Longer) Applicable • DELIVERY_DATE only applicable for Delivered orders • CANCEL_REASON only applicable for Cancelled orders 46 Constraints…
  • 47. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Reason 3: Nice to Know or Truly Optional • If we know release-date of product, lets store it • Some products have special-handling-instructions 47
  • 48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | My View on Nulls • Nulls due to nice-to-know/truly-optional  don’t worry about them – Likely not used in SQL logic and/or business logic • Avoid nulls due to inapplicability – Introduce generalization/specializations • Seriously consider avoiding nulls due to not-yet/no-longer applicable – How? By also introducing new (specialization) tables for these columns 48
  • 49. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Final Loose Thought on NULL’s • Not Null should have been the default • Database designs should have few nullable columns • They are not constraints… • Trying to insert NULL should just be a data-type error 49 This is an historic mistake imho
  • 50. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Contents 1. Dependencies, Keys and Third Normal Form 2. Boyce-Codd Normal Form 3. Some Logic 4. Nulls, Generalization and Specializations 5. Recommended Practices 50
  • 51. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Recommended DB Design Practices • Application should be based on proper DB design • "Proper" = two-fold 1. It must fit business requirements 2. It must follow sound relational principles 1) Is up to you talking to business owners and understanding their requirements  experience (and art?) 2) Is about science + objective reasoning on alternative designs Alternative table designs + alternative constraint designs 51
  • 52. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Recommended Practices For a Sound Database Design 0. Choose table and column names wisely 1. Use 3rd normal form Avoid structural redundancies (dependencies) 2. Don't design incidental redundancies Aka premature optimization E.g. Total value of order-lines at order level 3. Table has sequence-based primary key? Then also have 2nd business key, usually concatenated key 4. Put special things in separate tables (subtypes/specializations) Avoid lists of null-able columns due to non-applicability 5. Never allow nulls in columns involved in data integrity constraints Includes columns referenced by any SQL assertion you can document 52
  • 53. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Conclusions • There is a lot of science to database design • Database design is also constraint design • Don’t be afraid of many tables  DBMS’s were born to join • You might end up with foundation for your application that is easier to understand, is easier to maintain, and provides better performance 53
  • 54. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Snack: Something to Think About 54
  • 55. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | The Value of Sharing Database Design Pictures • Do you share database design pictures with end-users? – And point to tables on them while talking about them? • Why? – Do you think users understand these? 55
  • 56. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | The Value of Sharing Meteorological Pictures With Viewers • Seen these? • Do you understand them? – Blue, red, purple lines – Dashed lines – Little triangles, half circles – Sometimes combined – Capital L’s, capital H’s – Vague white lines • Does seeing these, add value to you? 56
  • 57. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Much Detail in Them That We Don’t Get... 57
  • 58. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | What You’re Interested In 58 For your location
  • 59. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | My Opinion: Little Value in Sharing Them • Like weather pictures: – Users don’t get the information held within them – Few users are interested in whole picture – These pictures miss lot of information (think: constraints/assertions) • Database design is frame of reference for database designer and application developer 59
  • 60. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | What Users Are Interested In 60 For their use-cases
  • 61. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Thank You For Your Time 61
  • 62. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 62