开发者

How can I replicate core data model using a traditional relational database?

I have my app using core data with the data model below. Howev开发者_如何学运维er, I'm switching to a standard database with columns and rows. Can anyone help me with setting up this new database schema?

How can I replicate core data model using a traditional relational database?


First of all you need to create tables for each of the Entities and their attributes (note I added "id" to each of the tables for relationships):

  1. Routine (name, timestamp, id)
  2. Exercise - this looks like a duplicate to me, so leaving one only here (muscleGroup, musclePicture, name, timeStamp, id)
  3. Session (timeStamp, id)
  4. Set (reps, timeStamp, unit, weight, id)

Now that you have tables that describe each of the entities, you need to create tables that will describe the relationships between these entities - as before table names are in capitals and their fields are in parenthesis:

  1. RoutineExercises (routine_id, exercise_id)
  2. SessionExercises (session_id, exercise_id)
  3. ExerciseSets (exercise_id, set_id)

That's it! Now if you need to add an exercise to a routine, you simply:

  1. Add an entry into Exercise table
  2. Establish the relationship by adding a tuple into RoutineExercises table where routine_id is your routine ID and exercise_id is the ID of the newly created entry in the Exercise table

This will hold true for all the rest of the relationships.

NOTE: Your core data model has one-to-many and many-to-many relationships. If you want to specifically enforce that a relationship is one-to-many (e.g. Exercise can only have 1 routine), then you will need to make "exercise_id" as the index for the RoutineExercises table. If you want a many-to-many relationships to be allowed (i.e. each exercise is allowed to have multiple routines), then set the tuple of (routine_id, exercise_id) as the index.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜