简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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