So, not having come from a database design background, I’ve been tasked with designing a web app where the end user will be entering products, and specs for their products. Normally I think I would just create rows for each of the types of spec that they would be entering. Instead, they have a variety of products that don’t share the same spec types, so my question is, what’s the most efficient and future-proof way to organize this data? I was leaning towards pushing a serialized object into a generic “data” row, but then are you able to do full-text searches on this data? Any other avenues to explore?
So, not having come from a database design background, I’ve been tasked with designing
Share
split products and specifications into two tables like this:
get all the specifations of a product when you know the product id:
add a specification to a product when you know the product id, the specification’s name and the value of said specification:
so before you can add specifications to a product, this product must exist. also, you can’t reuse specifications for several products. that would require a somewhat more complex solution 🙂 namely…
three tables this time:
get all the specifations of a product when you know the product id:
now, adding a specification becomes a little bit more tricky, cause you have to check if that specification already exists. so this will be a little heavier than the first way of doing this, since there are more queries on the db, and there’s more logic in the application.
first, find the id of the specification:
if no id is returned, this means that said specification doesn’t exist, so it must be created:
next, either use the id from the select query, or get the last insert id to find the id of the newly created specification. use that id together with the id of the product that’s getting the new specification, and link the two together:
however, this means that you have to create one row for every specific specification. e.g. if you have size for shoes, there would be one row for every known shoe size
and so on. i think this should be enough though.