简体   繁体   English

如何使用传统的关系数据库复制核心数据模型?

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

I have my app using core data with the data model below. 我的应用使用下面的数据模型使用核心数据。 However, I'm switching to a standard database with columns and rows. 但是,我将切换到具有列和行的标准数据库。 Can anyone help me with setting up this new database schema? 谁能帮助我设置这个新的数据库架构?

在此处输入图片说明

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): 首先,您需要为每个实体及其属性创建表(请注意,我在每个表中为关系添加了“ id”):

  1. Routine (name, timestamp, id) 例程(名称,时间戳,ID)
  2. Exercise - this looks like a duplicate to me, so leaving one only here (muscleGroup, musclePicture, name, timeStamp, id) 练习-对我来说这似乎是重复的练习,因此只在此处留一个(muscleGroup,musicPicture,名称,timeStamp,id)
  3. Session (timeStamp, id) 会话(timeStamp,ID)
  4. Set (reps, timeStamp, unit, weight, id) 设置(代表,时间戳,单位,重量,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) RoutineExercises(routine_id,exercise_id)
  2. SessionExercises (session_id, exercise_id) SessionExercises(session_id,exercise_id)
  3. ExerciseSets (exercise_id, set_id) 练习集(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 通过在RoutineExercises表中添加一个元组来建立关系,其中Routine_id是您的例程ID,exercise_id是Exercise表中新创建的条目的ID

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 (eg Exercise can only have 1 routine), then you will need to make "exercise_id" as the index for the RoutineExercises table. 如果要特别要求关系是一对多的(例如,练习只能有1个例程),则需要将“ exercise_id”作为RoutineExercises表的索引。 If you want a many-to-many relationships to be allowed (ie each exercise is allowed to have multiple routines), then set the tuple of (routine_id, exercise_id) as the index. 如果要允许多对多关系(即每个练习都允许有多个例程),则将(routine_id,exercise_id)的元组设置为索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM