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
coffeeIdforrecipes, onlycoffeeName - but only
coffeeIdis stored inrecipestable - that means i have two options:
- store
coffeeNameinrecipestable, so one sql query can both in (recipesandcoffeName) - do a sql join when querying
recipes
- store
- which one of the options is best practices? what are the pros and cons?
- are we trading space for speed in this case?
- bigger
recipestable, but fewer computation (one sql read so it might be faster)?
answer: use sql join
- data consistency
coffeeNamemight change- storing
coffeeNamein two tables means if it changes, we have to update two tables
- data integrity
- having
coffeNameinrecipesmight lead to samecoffeeIdhaving differentcoffeeNameinrecipestable in some weird edge cases
- having
- performance:
- if indexed properly, sql join table operations would NOT be slow
- one
coffeeNameis used for multiplerecipe(hundreds? thousands?)- so to update
coffeeName, instead of just updating one row incoffeestable, we need to update potentially huge number of rows - sacrificing speed and space in this case
- so to update
- NOT including
coffeeNameinrecipesalso obey database normalization priciples