简体   繁体   English

具有树状数据的表的数据库规范化

[英]Database Normalization For Table With Tree Like Data

How do I Normalize this table. 我如何规范该表。 It has a tree like structure which is expected to grow like a tree. 它具有像树一样的结构,预计会像树一样生长。 在此处输入图片说明

By tree like structure I mean that new students, subjects, levels and chapters will be constantly added or updated or removed 通过树状结构,我的意思是新学生,科目,水平和章节将不断添加或更新或删除

I want to store the result of a quiz in this table. 我想将测验的结果存储在此表中。 the quiz has multiple subjects under which there are multiple levels under which there are mutliple chapter. 测验有多个科目,在多个科目下有多个章节。 and Every students can take different subjects. 每个学生都可以选择不同的科目。 So is this table good for storing the results or I need to do something with this table? 那么,此表是否适合存储结果,或者我需要对此表进行某些操作?

In this particular case you need to create several independent tables: 在这种特殊情况下,您需要创建几个独立的表:

Table "Student"

ID, Name

1, John
2, Jack
Table "Subject"

ID, Name

1, Math
2, Science
3, Geography
4, History
5, English
Table "Levels"

ID, Name

1, Intermediate
2, Moderate
3, Difficult
Table "Chapters"

ID, Name

1, Chapter 1
2, Chapter 2
3, Chapter 3

And so on and so on. 等等等等。

Then you define the relations between the tables, like this: 然后定义表之间的关系,如下所示:

Table "student_subject_level"

ID, student_id, subject_id, level_id

1, 1, 1, 1 (John, Math, Intermediate)
2, 1, 2, 2 (John, Science, Moderate)

So far you have the student, the corresponding subejct and the subject's level. 到目前为止,您已经有了学生,相应的科目和科目的水平。 Since we may have multiple chapters for each level , we need another relation: 由于每个级别可能都有多个章节 ,因此我们需要另一个关系:

Table "student_subject_level_chapter" (or use simpler name)

student_subject_level_id, chapter_id
1, 1 (John, Math, Intermediate, Chapter 1)
1, 2 (John, Math, Intermediate, Chapter 2)
2, 1 (John, Science, Moderate, Chapter 1)

And so on and so on. 等等等等。 Start by isolating the individual tables and then figure out how you'd like to achieve the actual relation. 首先隔离各个表,然后弄清楚如何实现实际关系。 Fore each new relation where you have redundant data, you'd like to have new table which keeps the relation you need. 在每个具有冗余数据的新关系之前,您都希望有一个新表来保留您需要的关系。 It's much easier once you have ID's to refer to, so start with the individual tables and figure your way through. 一旦拥有ID即可轻松地进行引用,因此请从各个表开始并逐步解决。

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

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