use separate tables and join them, or include in the column?

tags: learning database programming diff-between

content

question:

  • on the frontend, i never display coffeeId for recipes, only coffeeName
  • but only coffeeId is stored in recipes table
  • that means i have two options:
    1. store coffeeName in recipes table, so one sql query can both in (recipes and coffeName)
    2. do a sql join when querying recipes
  • which one of the options is best practices? what are the pros and cons?
    • are we trading space for speed in this case?
    • bigger recipes table, but fewer computation (one sql read so it might be faster)?

answer: use sql join

  • data consistency
    • coffeeName might change
    • storing coffeeName in two tables means if it changes, we have to update two tables
  • data integrity
    • having coffeName in recipes might lead to same coffeeId having different coffeeName in recipes table in some weird edge cases
  • performance:
    • if indexed properly, sql join table operations would NOT be slow
    • one coffeeName is used for multiple recipe (hundreds? thousands?)
      • so to update coffeeName, instead of just updating one row in coffees table, we need to update potentially huge number of rows
      • sacrificing speed and space in this case
  • NOT including coffeeName in recipes also obey database normalization priciples

up

down

reference