SlideShare a Scribd company logo
10 WAYS
TO WRECK YOUR
DATABASE
   Josh Berkus
   PostgreSQL Experts, Inc.
   For O'Reilly Media, July 2009
REVENGE
against your boss
guarantee yourself
JOB SECURITY
ensure
PROJECT
FAILURE
make sure
NOBODY KNOWS
THE REAL DATA
open a
database
BACK
DOOR
force a
TOTAL
REWRITE
of the code
SLOW
DOWN
the application
to a crawl
make those stupid
DEVELOPERS
MISERABLE
10 Ways
         to:

  Lose Data
  Be Insecure
Become Unmaintainable
  Stife Changes
         and
 Make Everyone
  Miserable
One Big Spreadsheet
2. ranDom_naming(s)
          plural vs. singular
●
    users          ●
                       profle
●
    forum          ●
                       styles
●
    threads        ●
                       images
●
    posts          ●
                       survey
●
    message        ●
                       faqs
2. ranDom_naming(s)
        camel vs. underscore
●
    users            ●
                         userProfle
●
    threadCategory   ●
                         user_styles
●
    threads          ●
                         post_images
●
    posts            ●
                         survey
●
    privateMessage   ●
                         faqs
2. ranDom_naming(s)
                    felds
●
    users             ●
                          users
    ●
        frstName          ●
                              is_active
    ●
        last_name         ●
                              ACL
    ●
        login
    ●
        email
    ●
        crt_date
    ●
        lstLogin
2. ranDom_naming(s)
             Hungarian Notation
●
    tblUsers            ●
                            tblUsers
    ●
        txtFirstName        ●
                                bool_is_active
    ●
        str_last_name       ●
                                int4ACL
    ●
        txtLogin
    ●
        email
    ●
        dtcrt_date
    ●
        tmslstLogin
3&4
We don't need no
 stinking keys
3. No Real Keys
●
    Users
    ●
        userID AUTONUMBER PRIMARY KEY
    ●
        frst_name
    ●
        last_name
    ●
        login
    ●
        email
    ●
        is_active
3. No Real Keys

ID    first_name last_name         email         login password active level
21       Josh     Berkus     josh@pgexperts.com josh       jehosaphat   TRUE    u
47      Joshua    Berkus     josh@pgexperts.com joshb         dbguy     TRUE    u
198      Josh     Berkus      josh@postgresql.org joshb    jehosaphat   FALSE   u
204      Josh     Berkus       josh@agliodbs.com jberkus     joshiam    TRUE    a
4. No Foreign Keys
                     posts.content
                      Josh Berkus
                       What's up?
 users.login
                   I'm going crazy!
Josh Berkus
   jberkus
                  www.pornking.com
  jerkyboy
                          Why?
    selena
                 www.whitehouse.com
                OSCON! It's too much!
                www.whiteslavery.com
                   www.lolcats.com
                    I told you so ...
4. Fun with Orphan Rows
                    posts.content
                    Josh Berkus
                     What's up?
 users.login
                  I'm going crazy!
Josh Berkus
   jberkus

                      Why?
  selena
                www.whitehouse.com
               OSCON! It's too much!

                  www.lolcats.com
                   I told you so ...
4. Fun With Updates
INSERT INTO threads VALUES ( .... );
if $dbh('success') then
   while $these_posts.date > $cutdate
     UPDATE posts SET thread = $newthread
     WHERE id = $these_posts.id;
   if not $dbh('success') then
     while $these_posts.id > $last_id
        UPDATE posts
        SET thread = $oldthread
        WHERE id = $these_posts.id;
   DELETE FROM threads
   WHERE id = $newthread;
5. No Constraints
●
    Users
    ●
        userID AUTONUMBER PRIMARY KEY
    ●
        frst_name TEXT
    ●
        last_name TEXT
    ●
        login TEXT
    ●
        email TEXT
    ●
        is_active TEXT
5. No Constraints
●
    Users
    ●
        userID AUTONUMBER PRIMARY KEY
    ●
        frst_name VARCHAR (30)
    ●
        last_name VARCHAR (30)
    ●
        login VARCHAR (20)
    ●
        email VARCHAR (40)
    ●
        is_active BOOLEAN
5. No Constraints

first_name   last_name        email           login     password active      level
   Josh        Berkus    josh@pgexperts.com   jberkus   jehosaphat   TRUE      a
   NULL        NULL           kelley@ucb         k         NULL      FALSE     u
   Mike         Hunt      www.pornking.com    c34521      c34521     TRUE      I
    S            F           gavin@sf.gov      gavin       twitter   NULL      x
6. Non-Atomic Fields

                   name
                Josh Berkus
SELECT SUBSTR(name,STRPOS(name, ' ')) ...

                    status
                      a

   … WHERE status = 'a' OR status = 'u' ...
6. Non-Atomic Fields

                  name
               Josh Berkus
SELECT SUBSTR(name,STRPOS(name, ' ')) ...

                  status
                     i

         … WHERE status = ??? ...
6. Non-Atomic Fields
●
    Account Type
    ●
        5400 active individual
    ●
        5401 inactive individual
    ●
        5600 active board individual
    ●
        5601 inactive board individual
    ●
        6600 active corporate donor
    ●
        6601 active corporate board member
    ●
        6602 inactive corporate donor
    ●
        6603 inactive corporate board member
7. Magic Numbers



   ID = 0
7. Magic Numbers



        ID = 0
SELECT case_file, count(1)
FROM case_events
WHERE ID > 0
AND scheduled = TRUE
GROUP BY case_file;
7. Magic Numbers



2008-02-30
7. Magic Numbers



      2008-02-30
SELECT user_id, min(date)
FROM user_posts
WHERE NOT (
  extract(month from date) = 2
  and extract (day from date) = 30 )
GROUP BY user_id;
7. Magic Numbers



-1, 1, 2, 3, 4, 5, 100
7. Magic Numbers



-1, 1, 2, 3, 4, 5, 100
 SELECT household, avg(headcount)
 FROM house
 WHERE headcount BETWEEN 0 and 99
 GROUP BY household;
7. Magic Numbers



   $-0.01
7. Magic Numbers



     $-0.01
SELECT *
FROM invoices
WHERE total <> $0.01
  AND sent_on IS NULL;
7. Magic Numbers



 Document them
only in your code!
8. Polymorphic Fields

    Name         AccountType PrefContact     ContactInfo
Josh Berkus      Individual   Email        josh@pgexperts.com
John Dillinger   Board        Phone        415-555-1212
Pearson          Company      NULL         www.pearson.com
Clorox           Company      Jackie       510-555-1111 x202
9. EAV (entity-attribute-value)
         ID    Property   Setting

         407    Eyes      Brown

         407    Height     73in

         407   Married?   TRUE

         408   Married?   FALSE

         408   Smoker     FALSE

         408     Age        37

         409    Height     66in
9. EAV (entity-attribute-value)
●
    Diffcult tasks:
    ●
        Find out how many men have brown hair and are
        over 6 feet?
    ●
        Make marital status and age required, but not
        other things.
    ●
        Apply constraints to feld values.
9. EAV 2.0: E-BLOB
 ID            Properties



 407   <eyes=”brown”><height=”73”>
        <married=”1”><smoker=”1”>

 408    <hair=”brown”><age=”49”>
       <married=”0”><smoker=”0”>

 409    <age=”37”><height=”66”>
        <hat=”old”><teeth=”gold”>
10. Let The ORM
Take Care Of It
The Ten Ways
1.one big           6.non-atomic felds
 spreadsheet        7.magic numbers
2.random naming     8.polymorphic
3.no keys            felds
4.no foreign keys   9.EAV & EBlob
5.no constraints    10.ORM
Ten Ways to Fix Your Database
1.normalization   6.atomic felds
2.consistent      7.atomic felds
 naming           8.atomic felds
3.keys            9.limit EAV &
4.foreign keys     EBlob
5.constraints     10.DB design
More Wreckage
●
    Simplifying Database Design Tutorial
    –   OSCON, Monday 8:30 AM
●
    PostgreSQL Day San Jose
    –   Before OSCON, Sunday 19th, all day
    –   wiki.postgresql.org/wiki/PgDaySanJose2009
●
    Me
    –   PostgreSQL Experts: www.pgexperts.com
    –   blog: it.toolbox.com/blogs/database-soup
    –   email: josh@pgexperts.com

        This presentation copyright 2009 Josh Berkus, licensed for distribution under the
                            Creative Commons Attribution License.

More Related Content

Similar to Ten Ways to Destroy Your Database

Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
Boulder Java User's Group
 
A practical intro to web development with mongo db and nodejs when, why and how
A practical intro to web development with mongo db and nodejs  when, why and howA practical intro to web development with mongo db and nodejs  when, why and how
A practical intro to web development with mongo db and nodejs when, why and how
jgarifuna
 
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
Dor Tumarkin
 
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
Checkmarx
 
Application Modeling with Graph Databases
Application Modeling with Graph DatabasesApplication Modeling with Graph Databases
Application Modeling with Graph DatabasesJosh Adell
 
mongoDB at Visibiz
mongoDB at VisibizmongoDB at Visibiz
mongoDB at Visibiz
Mike Brocious
 
The breakup
The breakupThe breakup
The breakup
Samantha Billington
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
Lars Marius Garshol
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Jonathan Katz
 
The Breakup - Logically Sharding a Growing PostgreSQL Database
The Breakup - Logically Sharding a Growing PostgreSQL DatabaseThe Breakup - Logically Sharding a Growing PostgreSQL Database
The Breakup - Logically Sharding a Growing PostgreSQL Database
Fred Moyer
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDDSteven Mak
 
Efficient extraction of data using binary search and ordering information
Efficient extraction of data using binary search and ordering informationEfficient extraction of data using binary search and ordering information
Efficient extraction of data using binary search and ordering information
Security B-Sides
 
The disaster of mutable state
The disaster of mutable stateThe disaster of mutable state
The disaster of mutable state
kenbot
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
Brian Hogan
 
Hacking MongoDB at RelateIQ, A Salesforce Company
Hacking MongoDB at RelateIQ, A Salesforce CompanyHacking MongoDB at RelateIQ, A Salesforce Company
Hacking MongoDB at RelateIQ, A Salesforce Company
MongoDB
 
PgREST: Node.js in the Database
PgREST: Node.js in the DatabasePgREST: Node.js in the Database
PgREST: Node.js in the Database
Audrey Tang
 
Ruby 2: some new things
Ruby 2: some new thingsRuby 2: some new things
Ruby 2: some new things
David Black
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
Troy Miles
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScriptpamselle
 

Similar to Ten Ways to Destroy Your Database (20)

Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
55 New Features in Java 7
55 New Features in Java 755 New Features in Java 7
55 New Features in Java 7
 
A practical intro to web development with mongo db and nodejs when, why and how
A practical intro to web development with mongo db and nodejs  when, why and howA practical intro to web development with mongo db and nodejs  when, why and how
A practical intro to web development with mongo db and nodejs when, why and how
 
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
 
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018(De)serial Killers - BSides Las Vegas & AppSec IL 2018
(De)serial Killers - BSides Las Vegas & AppSec IL 2018
 
Application Modeling with Graph Databases
Application Modeling with Graph DatabasesApplication Modeling with Graph Databases
Application Modeling with Graph Databases
 
mongoDB at Visibiz
mongoDB at VisibizmongoDB at Visibiz
mongoDB at Visibiz
 
The breakup
The breakupThe breakup
The breakup
 
JSLT: JSON querying and transformation
JSLT: JSON querying and transformationJSLT: JSON querying and transformation
JSLT: JSON querying and transformation
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
 
The Breakup - Logically Sharding a Growing PostgreSQL Database
The Breakup - Logically Sharding a Growing PostgreSQL DatabaseThe Breakup - Logically Sharding a Growing PostgreSQL Database
The Breakup - Logically Sharding a Growing PostgreSQL Database
 
Sustainable TDD
Sustainable TDDSustainable TDD
Sustainable TDD
 
Efficient extraction of data using binary search and ordering information
Efficient extraction of data using binary search and ordering informationEfficient extraction of data using binary search and ordering information
Efficient extraction of data using binary search and ordering information
 
The disaster of mutable state
The disaster of mutable stateThe disaster of mutable state
The disaster of mutable state
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Hacking MongoDB at RelateIQ, A Salesforce Company
Hacking MongoDB at RelateIQ, A Salesforce CompanyHacking MongoDB at RelateIQ, A Salesforce Company
Hacking MongoDB at RelateIQ, A Salesforce Company
 
PgREST: Node.js in the Database
PgREST: Node.js in the DatabasePgREST: Node.js in the Database
PgREST: Node.js in the Database
 
Ruby 2: some new things
Ruby 2: some new thingsRuby 2: some new things
Ruby 2: some new things
 
React Native Evening
React Native EveningReact Native Evening
React Native Evening
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
 

More from PostgreSQL Experts, Inc.

Shootout at the PAAS Corral
Shootout at the PAAS CorralShootout at the PAAS Corral
Shootout at the PAAS Corral
PostgreSQL Experts, Inc.
 
Shootout at the AWS Corral
Shootout at the AWS CorralShootout at the AWS Corral
Shootout at the AWS Corral
PostgreSQL Experts, Inc.
 
Fail over fail_back
Fail over fail_backFail over fail_back
Fail over fail_back
PostgreSQL Experts, Inc.
 
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Replication in 10  Minutes - SCALEPostgreSQL Replication in 10  Minutes - SCALE
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Experts, Inc.
 
HowTo DR
HowTo DRHowTo DR
Give A Great Tech Talk 2013
Give A Great Tech Talk 2013Give A Great Tech Talk 2013
Give A Great Tech Talk 2013
PostgreSQL Experts, Inc.
 
Pg py-and-squid-pypgday
Pg py-and-squid-pypgdayPg py-and-squid-pypgday
Pg py-and-squid-pypgday
PostgreSQL Experts, Inc.
 
7 Ways To Crash Postgres
7 Ways To Crash Postgres7 Ways To Crash Postgres
7 Ways To Crash Postgres
PostgreSQL Experts, Inc.
 
Elephant Roads: PostgreSQL Patches and Variants
Elephant Roads: PostgreSQL Patches and VariantsElephant Roads: PostgreSQL Patches and Variants
Elephant Roads: PostgreSQL Patches and VariantsPostgreSQL Experts, Inc.
 

More from PostgreSQL Experts, Inc. (20)

Shootout at the PAAS Corral
Shootout at the PAAS CorralShootout at the PAAS Corral
Shootout at the PAAS Corral
 
Shootout at the AWS Corral
Shootout at the AWS CorralShootout at the AWS Corral
Shootout at the AWS Corral
 
Fail over fail_back
Fail over fail_backFail over fail_back
Fail over fail_back
 
PostgreSQL Replication in 10 Minutes - SCALE
PostgreSQL Replication in 10  Minutes - SCALEPostgreSQL Replication in 10  Minutes - SCALE
PostgreSQL Replication in 10 Minutes - SCALE
 
HowTo DR
HowTo DRHowTo DR
HowTo DR
 
Give A Great Tech Talk 2013
Give A Great Tech Talk 2013Give A Great Tech Talk 2013
Give A Great Tech Talk 2013
 
Pg py-and-squid-pypgday
Pg py-and-squid-pypgdayPg py-and-squid-pypgday
Pg py-and-squid-pypgday
 
92 grand prix_2013
92 grand prix_201392 grand prix_2013
92 grand prix_2013
 
Five steps perform_2013
Five steps perform_2013Five steps perform_2013
Five steps perform_2013
 
7 Ways To Crash Postgres
7 Ways To Crash Postgres7 Ways To Crash Postgres
7 Ways To Crash Postgres
 
PWNage: Producing a newsletter with Perl
PWNage: Producing a newsletter with PerlPWNage: Producing a newsletter with Perl
PWNage: Producing a newsletter with Perl
 
10 Ways to Destroy Your Community
10 Ways to Destroy Your Community10 Ways to Destroy Your Community
10 Ways to Destroy Your Community
 
Open Source Press Relations
Open Source Press RelationsOpen Source Press Relations
Open Source Press Relations
 
5 (more) Ways To Destroy Your Community
5 (more) Ways To Destroy Your Community5 (more) Ways To Destroy Your Community
5 (more) Ways To Destroy Your Community
 
Preventing Community (from Linux Collab)
Preventing Community (from Linux Collab)Preventing Community (from Linux Collab)
Preventing Community (from Linux Collab)
 
Development of 8.3 In India
Development of 8.3 In IndiaDevelopment of 8.3 In India
Development of 8.3 In India
 
PostgreSQL and MySQL
PostgreSQL and MySQLPostgreSQL and MySQL
PostgreSQL and MySQL
 
50 Ways To Love Your Project
50 Ways To Love Your Project50 Ways To Love Your Project
50 Ways To Love Your Project
 
8.4 Upcoming Features
8.4 Upcoming Features 8.4 Upcoming Features
8.4 Upcoming Features
 
Elephant Roads: PostgreSQL Patches and Variants
Elephant Roads: PostgreSQL Patches and VariantsElephant Roads: PostgreSQL Patches and Variants
Elephant Roads: PostgreSQL Patches and Variants
 

Recently uploaded

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 

Recently uploaded (20)

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 

Ten Ways to Destroy Your Database

  • 1. 10 WAYS TO WRECK YOUR DATABASE Josh Berkus PostgreSQL Experts, Inc. For O'Reilly Media, July 2009
  • 10. 10 Ways to: Lose Data Be Insecure Become Unmaintainable Stife Changes and Make Everyone Miserable
  • 12. 2. ranDom_naming(s) plural vs. singular ● users ● profle ● forum ● styles ● threads ● images ● posts ● survey ● message ● faqs
  • 13. 2. ranDom_naming(s) camel vs. underscore ● users ● userProfle ● threadCategory ● user_styles ● threads ● post_images ● posts ● survey ● privateMessage ● faqs
  • 14. 2. ranDom_naming(s) felds ● users ● users ● frstName ● is_active ● last_name ● ACL ● login ● email ● crt_date ● lstLogin
  • 15. 2. ranDom_naming(s) Hungarian Notation ● tblUsers ● tblUsers ● txtFirstName ● bool_is_active ● str_last_name ● int4ACL ● txtLogin ● email ● dtcrt_date ● tmslstLogin
  • 16. 3&4 We don't need no stinking keys
  • 17. 3. No Real Keys ● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name ● last_name ● login ● email ● is_active
  • 18. 3. No Real Keys ID first_name last_name email login password active level 21 Josh Berkus josh@pgexperts.com josh jehosaphat TRUE u 47 Joshua Berkus josh@pgexperts.com joshb dbguy TRUE u 198 Josh Berkus josh@postgresql.org joshb jehosaphat FALSE u 204 Josh Berkus josh@agliodbs.com jberkus joshiam TRUE a
  • 19. 4. No Foreign Keys posts.content Josh Berkus What's up? users.login I'm going crazy! Josh Berkus jberkus www.pornking.com jerkyboy Why? selena www.whitehouse.com OSCON! It's too much! www.whiteslavery.com www.lolcats.com I told you so ...
  • 20. 4. Fun with Orphan Rows posts.content Josh Berkus What's up? users.login I'm going crazy! Josh Berkus jberkus Why? selena www.whitehouse.com OSCON! It's too much! www.lolcats.com I told you so ...
  • 21. 4. Fun With Updates INSERT INTO threads VALUES ( .... ); if $dbh('success') then while $these_posts.date > $cutdate UPDATE posts SET thread = $newthread WHERE id = $these_posts.id; if not $dbh('success') then while $these_posts.id > $last_id UPDATE posts SET thread = $oldthread WHERE id = $these_posts.id; DELETE FROM threads WHERE id = $newthread;
  • 22. 5. No Constraints ● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name TEXT ● last_name TEXT ● login TEXT ● email TEXT ● is_active TEXT
  • 23. 5. No Constraints ● Users ● userID AUTONUMBER PRIMARY KEY ● frst_name VARCHAR (30) ● last_name VARCHAR (30) ● login VARCHAR (20) ● email VARCHAR (40) ● is_active BOOLEAN
  • 24. 5. No Constraints first_name last_name email login password active level Josh Berkus josh@pgexperts.com jberkus jehosaphat TRUE a NULL NULL kelley@ucb k NULL FALSE u Mike Hunt www.pornking.com c34521 c34521 TRUE I S F gavin@sf.gov gavin twitter NULL x
  • 25. 6. Non-Atomic Fields name Josh Berkus SELECT SUBSTR(name,STRPOS(name, ' ')) ... status a … WHERE status = 'a' OR status = 'u' ...
  • 26. 6. Non-Atomic Fields name Josh Berkus SELECT SUBSTR(name,STRPOS(name, ' ')) ... status i … WHERE status = ??? ...
  • 27. 6. Non-Atomic Fields ● Account Type ● 5400 active individual ● 5401 inactive individual ● 5600 active board individual ● 5601 inactive board individual ● 6600 active corporate donor ● 6601 active corporate board member ● 6602 inactive corporate donor ● 6603 inactive corporate board member
  • 29. 7. Magic Numbers ID = 0 SELECT case_file, count(1) FROM case_events WHERE ID > 0 AND scheduled = TRUE GROUP BY case_file;
  • 31. 7. Magic Numbers 2008-02-30 SELECT user_id, min(date) FROM user_posts WHERE NOT ( extract(month from date) = 2 and extract (day from date) = 30 ) GROUP BY user_id;
  • 32. 7. Magic Numbers -1, 1, 2, 3, 4, 5, 100
  • 33. 7. Magic Numbers -1, 1, 2, 3, 4, 5, 100 SELECT household, avg(headcount) FROM house WHERE headcount BETWEEN 0 and 99 GROUP BY household;
  • 35. 7. Magic Numbers $-0.01 SELECT * FROM invoices WHERE total <> $0.01 AND sent_on IS NULL;
  • 36. 7. Magic Numbers Document them only in your code!
  • 37. 8. Polymorphic Fields Name AccountType PrefContact ContactInfo Josh Berkus Individual Email josh@pgexperts.com John Dillinger Board Phone 415-555-1212 Pearson Company NULL www.pearson.com Clorox Company Jackie 510-555-1111 x202
  • 38. 9. EAV (entity-attribute-value) ID Property Setting 407 Eyes Brown 407 Height 73in 407 Married? TRUE 408 Married? FALSE 408 Smoker FALSE 408 Age 37 409 Height 66in
  • 39. 9. EAV (entity-attribute-value) ● Diffcult tasks: ● Find out how many men have brown hair and are over 6 feet? ● Make marital status and age required, but not other things. ● Apply constraints to feld values.
  • 40. 9. EAV 2.0: E-BLOB ID Properties 407 <eyes=”brown”><height=”73”> <married=”1”><smoker=”1”> 408 <hair=”brown”><age=”49”> <married=”0”><smoker=”0”> 409 <age=”37”><height=”66”> <hat=”old”><teeth=”gold”>
  • 41. 10. Let The ORM Take Care Of It
  • 42. The Ten Ways 1.one big 6.non-atomic felds spreadsheet 7.magic numbers 2.random naming 8.polymorphic 3.no keys felds 4.no foreign keys 9.EAV & EBlob 5.no constraints 10.ORM
  • 43. Ten Ways to Fix Your Database 1.normalization 6.atomic felds 2.consistent 7.atomic felds naming 8.atomic felds 3.keys 9.limit EAV & 4.foreign keys EBlob 5.constraints 10.DB design
  • 44. More Wreckage ● Simplifying Database Design Tutorial – OSCON, Monday 8:30 AM ● PostgreSQL Day San Jose – Before OSCON, Sunday 19th, all day – wiki.postgresql.org/wiki/PgDaySanJose2009 ● Me – PostgreSQL Experts: www.pgexperts.com – blog: it.toolbox.com/blogs/database-soup – email: josh@pgexperts.com This presentation copyright 2009 Josh Berkus, licensed for distribution under the Creative Commons Attribution License.