0

I'm currently looking into the best way of saving some data in my database for each user. Lets say each user can save recipes on their account, then what would be the best way of doing this?

I thought of storing a JSON string in a column, but I read that that might not be the best idea, because it's slow to query it?

Another idea was to make a completely new table, where all the recipes that are being saved are stored.

What would you recommend? Thanks.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Anders
  • 513
  • 2
  • 10
  • 32
  • 1
    You want to have a table called users, one called recipes and one table for ingredients. The recipes table would have a foriegn key that linked back to the primary key of the users table and the ingredients table would have a FK that linked back the PK of the recipes table – Jacob Feb 23 '15 at 17:38
  • Do you have something you've tried already that isn't working out for you? A recipe could have any number of ingredients, instructions on how to make, etc. This appears to be a little off-topic and it is definitely too broad. I would start at the conceptual level on thinking of all the different stuff you need to store and then start sorting it into tables. – Crackertastic Feb 23 '15 at 17:39
  • 1
    You have tagged this question with MySQL - does this mean that you are already committed to using a MySQL database? (If so, a normalised relational schema would be a good idea.) –  Feb 23 '15 at 17:40

2 Answers2

2

Edit:

after your comment, I would suggest you create a junction table between users and recipes (i.e. to implement the "one user to many recipes" relation).

That would take full advantage of SQL capabilities and save you a lot of sweat if you ever want to do some fancy queries later.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
  • Thanks for the info, but you got me a bit wrong. First of all, I will be making all the recipes in a table for themselves. The feature here, is that users can save specific recipes to their account. In example, there was a column called "saved_recipes" under the user table, encoded in JSON where it had the id's of the recipes the user had saved. I still might go with creating a new table called "saved_recipes", thanks! – Anders Feb 23 '15 at 18:09
0

Without knowing more about your application's requirements I'll have to generalize a bit here: You should not save it as JSON. That's not only more resource intensive when querying and can't be indexed (very easily) but it also makes it much more difficult for other applications to use the data. You should normalize the data and use PHPs json_encode() to the convert your query results to a JSON object.

Community
  • 1
  • 1
Elliott
  • 2,035
  • 20
  • 23