[英]Should I use Table Per Type or Table Per Hierarchy?
If I have a database with courses and course templates, and company course templates, what is the best way to store them in a SQL database? 如果我有一个包含课程和课程模板以及公司课程模板的数据库,那么将它们存储在SQL数据库中的最佳方法是什么? Course will probably have some other metadata about score, time-taken, and perhaps some other info.
课程中可能会包含有关得分,花费的时间以及其他一些信息的其他元数据。
Table Per Hierarchy 每个层次表
// Classes
public class CourseTemplate { }
public class CompanyCourseTemplate { }
public class Course { }
Table Per Type 每种类型的表
public class CourseTemplate { }
public class CompanyCourseTemplate : CourseTemplate { }
public class Course : CompanyCourseTemplate { }
// Also for Course what if there are Course that belong to
// either CompanyCourseTemplate or else CourseTemplate
Hybrid Using Both - TPH 两者同时使用-TPH
public class CourseTemplate { }
public class CompanyCourseTemplate : CourseTemplate { }
And - TPT 和-TPT
public class Course { }
It seems like the querying always gets messy when you need to differentiate the types within a table. 当您需要区分表中的类型时,查询似乎总是一团糟。 As well, if you create a table per type the database gets cluttered with similar named tables as well.
同样,如果您为每种类型创建一个表,则数据库也会被类似的命名表所困扰。 I am leaning towards the hybrid since it seems to be more natural with the types.
我倾向于混合动力,因为它在类型上似乎更自然。
However, let's say we are storing a reference to the templates and we are using table per type what is the best way to manage this relationship? 但是,假设我们正在存储对模板的引用,并且我们使用每种类型的表来管理这种关系的最佳方法是什么?
In the exam table I could have two foreign keys to the template tables. 在检查表中,我可以有两个到模板表的外键。
public class ExamTemplate { }
public class CompanyExamTempalte { }
public class Exam {
public Int32 ? ExamTemplateId { get;set; }
public Int32 ? CompanyExamTemplateId { get;set; }
The problem with this is that my code gets really ugly when I have to get an template from an exam template reference. 问题在于,当我必须从考试模板参考中获取模板时,我的代码变得非常难看。 I haven't found the best way to really take care of this problem.
我还没有找到真正解决这个问题的最佳方法。 What is the best way to inherit from one of two templates.
从两个模板之一继承的最佳方法是什么。 I am thinking that a hierarchy needs to be created where all exam instances must have a company exam template as well as a master template and the exam can hold references to both.
我认为需要创建一个层次结构,其中所有考试实例都必须具有公司考试模板和主模板,并且考试可以包含对两者的引用。 The change would look like this.
更改看起来像这样。
public class Exam {
public Int32 CompanyExamTemplateId { get;set; }
public CompanyExamTemplate { get; set; }
public Int32 ExamTemplateId { get; set; }
public ExamTemplate { get; set; }
}
NOTE the nullables are gone since the hierarchy is always required. 注意由于总是需要层次结构,因此可为空的对象消失了。
Seems like you were initially leaning toward a one-to-many parent-child table structure something like this: 似乎您最初倾向于一对多的父子表结构,如下所示:
tblCourseTemplate: tblCourseTemplate:
CourseTemplateID CourseTemplateID
CourseTemplateName CourseTemplateName
CourseTemplateIsCompanyTemplate CourseTemplateIsCompanyTemplate
CourseTemplateCompanyNameIfApplies CourseTemplateCompanyNameIfApplies
tblCourse: 课程:
CourseID 课程编号
CourseTemplateID (ie parent record) CourseTemplateID(即父记录)
CourseName 课程名
CourseScore 课程分数
CourseDateTaken CourseDateTaken
CourseTimeTaken 课程时间
But then you said that a course could have more than one "parent" record so perhaps use an intermediate table to handle a many-to-many relationship: 但是然后您说一门课程可能有多个“父”记录,因此也许使用一个中间表来处理多对多关系:
tblCourseTemplate: tblCourseTemplate:
CourseTemplateID CourseTemplateID
CourseTemplateName CourseTemplateName
CourseTemplateIsCompanyTemplate CourseTemplateIsCompanyTemplate
CourseTemplateCompanyNameIfApplies CourseTemplateCompanyNameIfApplies
tblCourseTemplateAndCourse: tblCourseTemplateAndCourse:
CourseTemplateAndCourseID CourseTemplateAndCourseID
CourseTemplateID CourseTemplateID
CourseID 课程编号
tblCourse: 课程:
CourseID 课程编号
CourseName 课程名
CourseScore 课程分数
CourseDateTaken CourseDateTaken
CourseTimeTaken 课程时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.