简体   繁体   English

将复杂对象列表插入数据库

[英]Inserting a list of complex objects to the database

In my application I'm going to have some list of a complex object I'm going to insert into a database. 在我的应用程序中,我将具有一些要插入数据库的复杂对象的列表。
The list is attached to a user, so I thought about adding a column with a list of IDs to the Users table, and then have the app convert the list back into complex objects using the list of IDs. 该列表是附加到用户的,因此我考虑将一个包含ID列表的列添加到Users表,然后让该应用使用ID列表将该列表转换回复杂的对象。

For example; 例如; A user have 3 Subjects IDs: "2,3,23" . 用户具有3个Subjects ID: "2,3,23"
Each subject has a name, grade, etc... 每个subject都有名称,年级等。

On runtime, the app would go through each ID and get its properties from the database and add the complete subject to the list. 在运行时,该应用将遍历每个ID并从数据库中获取其属性,并将完整的主题添加到列表中。

The problem is that sounds quite inefficient to be honest. 问题是,说实话,听起来效率很低。
And therefore, my question is; 因此,我的问题是; is there a way to do it more efficiently? 有没有办法更有效地做到这一点?

The normal way to do this would be to have a separate table for the Subjects and that table has a UserID which is a foreign key back to the owning User record. 这样做的通常方法是为主题创建一个单独的表,并且该表具有一个UserID,该ID是返回拥有用户记录的外键。

Then you don't have to mess about with editing a comma-separated string. 这样一来,您就不必为编辑逗号分隔的字符串而烦恼了。 It also allows you define a foreign-key constraint (as long as your DB can it) which will stop you from inserting a Subjects record with a UserID that doesn't exist in the User table. 它还允许您定义一个外键约束(只要您的数据库可以这样做),这将阻止您插入User表中不存在的UserID的Subjects记录。

To load the Subjects for a User, you just retrieve all the records from the Subjects table with the Id of the User. 要为用户加载主题,您只需使用用户ID从主题表中检索所有记录即可。

If a many-to-many relationship is more appropriate, where a User can be linked to many Subjects and a Subject can be linked to may Users, then you would use a join table, say UserSubjectJoin which contains each UserID and SubjectID pairing. 如果多对多关系更合适,其中一个用户可以链接到许多主题,而一个主题可以链接到可能的用户,那么您将使用一个联接表,即UserSubjectJoin,其中包含每个UserID和SubjectID配对。 Again, this can have constraints to ensure that the IDs must exist in the Users and Subjects tables. 同样,这可能会有约束,以确保ID必须存在于“用户”和“主题”表中。

In this scenario, you need to join your UserSubjectJoin table to the Subjects table to retrieve all the Subjects records for the UserID you specify. 在这种情况下,您需要将UserSubjectJoin表联接到Subjects表,以检索您指定的UserID的所有Subjects记录。

To answer your question fully, this would be more efficient because you don't have to maintain a column of strings in the Users table as you just add/delete rows from the Subjects table and the associations are automatically updated. 要完全回答您的问题,这将更有效率,因为您不必在Users表中维护一列字符串,只需在Subjects表中添加/删除行,并且关联会自动更新。

And if you actually have a many-to-many relationship, then under your initial design you would also need to have a column of UserIDs on the Subjects table which then doubles the maintenance, and risk of getting it wrong. 而且,如果您实际上存在多对多关系,那么在您的初始设计下,您还需要在“主题”表上有一列UserID,这会使维护增加一倍,并且有出错的风险。

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

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