Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Bringing Good Design to the Table

2,635 views

Published on

Basic introduction to database table design, covering a bit of normalization, naming conventions, structure, and a few common anti-patterns.

Published in: Technology
  • Be the first to comment

Bringing Good Design to the Table

  1. 1. Bringing good design to the tablePhotos ©James McAdam 2002
  2. 2. Who am I?Brian FentonSenior Software Engineer at Mashery We’re hiring!Primarily a PHP developerReally enjoy OO and database design
  3. 3. Overview
  4. 4. So you’ve been asked to build a system…
  5. 5. Shadow your users
  6. 6. Primary Keys
  7. 7. Natural keys
  8. 8. Foreign keys
  9. 9. Relationships
  10. 10. Constraints
  11. 11. Define columns strictly
  12. 12. Cleaning data
  13. 13. Database Normalization Edgar F. Codd Database Pimp
  14. 14. But de-normalized is faster!
  15. 15. No duplicate data
  16. 16. Fields should only store a single data value
  17. 17. Don’t store what you can calculate
  18. 18. Dates
  19. 19. Pop Quiz, HotshotID Value1 1 SELECT AVG(Value) = ?2 NULL3 54 65 NULL6 47 88 NULL9 210 2 Total: 28
  20. 20. NULL means Unknown
  21. 21. Naming Conventions
  22. 22. Tables employee is Bad employees is OKpersonnel is Better
  23. 23. Linking Tablescustomer_addressesteacher_coursesemployee_salaries
  24. 24. Columns Yes Noid cust_noaddress_id addrpostal_code zipCodelocality customer_nameregion Reserved words
  25. 25. Normalization exerciseISBN title author price0-943396-04-2 Design Patterns: Erich Gamma, 29.99 Elements of Reusable Richard Helm, Object-Oriented Software Ralph Johnson, John Vlissides0-684-84328-5 Joe Celkos Trees and Joe Celko 32.95 Hierarchies0-85131-041-9 Refactoring: Improving Martin Fowler 19.99 the Design of Existing Code9971-5-0210-0 Code Complete 2nd Steve McConnell 25.50 Edition
  26. 26. Normalization exercisebooks authorsISBN title price id author0-943396-04-2 Design Patterns 29.99 1 Erich Gamma 2 Richard Helm0-684-84328-5 Trees and Hierarchies 32.95 3 Ralph Johnson0-85131-041-9 Refactoring 19.99 4 John Vlissides9971-5-0210-0 Code Complete 2 25.50 5 Joe Celko 6 Martin Fowler 7 Steve McConnell
  27. 27. ISBN title price books0-943396-04-2 Design Patterns 29.99 author0-684-84328-5 Trees and Hierarchies 32.95 id author s0-85131-041-9 Refactoring 19.99 1 Erich Gamma9971-5-0210-0 Code Complete 2 25.50 2 Richard Helm 3 Ralph Johnson ISBN author_id 4 John Vlissides 0-943396-04-2 1 5 Joe Celko 0-943396-04-2 2 6 Martin Fowler 0-943396-04-2 3 7 Steve McConnell 0-943396-04-2 4 0-684-84328-5 5 0-85131-041-9 6 books_authors 9971-5-0210-0 7
  28. 28. New Student OrientationName Address City State Zip Term DateBrian 717 Market San CA 94112 Summer 5-9-99Fenton St Francisco 1999Chris 1600 Washingto DC 20500 Fall 7-10-99Smith Pennsylvania n 1999 AvenueJohn 221B Baker London MO 66614 Spring 11-10-99Andrews St 2000Susan 123 Elm St Anytown MA 00128 Fall 7-8-99Anthony 1999
  29. 29. Storing names
  30. 30. UTF-8
  31. 31. Indexing
  32. 32. Anti-patterns
  33. 33. Multiples of the same columnid teacher course1 course2 course31 Steve Fluty MATH101 BIO 210 CHEM 5312 Bob Levine PSYC 400 CLSX 356 PHYS 1083 Kim Jones FREN 110 NULL NULL
  34. 34. teachers teacher_coursesid name teacher_id course_id1 Steve Fluty 1 12 Bob Levine 2 23 Kim Jones 3 3 1 4 1 5 coursesid department level name description1 MATH 101 College Mathematics …2 PSYC 400 Neural Psychology …3 FREN 110 Intermediate French …4 BIOL 210 Microbiology …5 CHEM 531 Organic Chemistry …
  35. 35. Type codes determining columns usedid name type cost flavor gallons number_of_blades1 Flushmaster toilet 199.99 NULL 1.3 NULL2 Slashmaster tool 35.99 NULL NULL 55 Baby-Gro food 1.29 carrots NULL NULL6 Flushmaster toilet 249.99 NULL 1.5 NULL Deluxe10 Health Muck food 1.29 peas NULL NULL
  36. 36. Solution 1 toiletsid name cost gallons1 Flushmaster 199.99 1.36 Flushmaster Deluxe 249.99 1.5 baby_foodsid name cost flavor5 Baby-Gro 1.29 carrots10 Health Muck 1.29 peas multitoolsid name cost number_of_blades2 Slashmaster 35.99 5
  37. 37. Solution 2products baby_foodsid name cost product_id flavor1 Flushmaster 199.99 5 carrots2 Slashmaster 35.99 10 peas5 Baby-Gro 1.296 Flushmaster 249.99 Deluxe multitools10 Health Muck 1.29 product_id number_of_blades 2 5 toilets product_id gallons 1 1.5 6 1.3
  38. 38. Hierarchical/tree relationships author content bfenton Woohoo, I’m at SunshinePHP!author content author contentadamculp Welcome, Brian! elazar Me too! author content jmather Hey, didn’t know you were here author content elazar Yep, just got in
  39. 39. Solution 1id parent_id author content1 NULL bfenton Woohoo, I’m at SunshinePHP!2 1 elazar Me too!3 1 adamculp Welcome, Brian!4 2 jmather Hey, didn’t know you were here5 4 elazar Yep, just got in
  40. 40. Solution 2 author content bfenton Woohoo, I’m at SunshinePHP! 1 10 author content author content adamculp Welcome, Brian! elazar Me too!2 3 4 9 author content jmather Hey, didn’t know you were here 5 8 author content elazar Yep, just got in 6 7
  41. 41. Solution 2id author left right content1 bfenton 1 10 Woohoo, I’m at SunshinePHP!2 elazar 4 9 Me too!3 adamculp 2 3 Welcome, Brian!4 jmather 5 8 Hey, didn’t know you were here5 elazar 6 7 Yep, just got in
  42. 42. Solution 3id path author content1 1/ bfenton Woohoo, I’m at SunshinePHP!2 1/2/ elazar Me too!3 1/3/ adamculp Welcome, Brian!4 1/2/4/ jmather Hey, didn’t know you were here5 1/2/4/5/ elazar Yep, just got in
  43. 43. Solution 4 parent childid author content 1 11 bfenton Woohoo, I’m at SunshinePHP! 1 22 elazar Me too! 1 33 adamculp Welcome, Brian! 1 44 jmather Hey, didn’t know you were here 1 55 elazar Yep, just got in 2 2 2 4 2 5 3 3 4 4 4 5 5 5
  44. 44. Wrap upGather requirements Text as UTF-8Choose good keys Use a naming conventionDefine relationships Be smart about NULLsBe strict Avoid anti-patternsNo duplicate/calculated data More tables is usually goodNo multi-valued columns Index where you needDates as UTC “It Depends”
  45. 45. ResourcesDatabase Design for Mere Mortals by Michael J. HernandezHow to support full Unicode in MySQL databasesPersonal names around the worldFalsehoods Programmers Believe About NamesFalsehoods programmers believe about timeHow to Design Indexes, Really
  46. 46. Questions? https://joind.in/8026@brianfentonwww.brianfenton.usbfenton@mashery.com

×