[英]Multi attributed database design confusion
Hi dear community members I need help on designing a simple database. 尊敬的社区成员,我需要设计简单数据库的帮助。 It is a database of student and schools. 它是学生和学校的数据库。 Same schools have multiple students and one student can be affiliated with multiple schools. 同一所学校有多名学生,一个学生可以隶属于多所学校。 I am wondering what is the best way to do it. 我想知道什么是最好的方法。
Sample scenario: User john went BI school as high school and MIT as University, user harry went to MIT as university. 示例场景:用户john在高中时进入了BI学校,在MIT时进入了大学,用户Harry则在MIT时进入了大学。
One approach we can have is: We can have a db table 我们可以使用的一种方法是:我们可以使用db表
User Table 用户表
+--------+-------+-----------+ | usr_id | name | school_id | +--------+-------+-----------+ | 7 | john | 1 | | 7 | john | 2 | | 6 | harry | 1 | +--------+-------+-----------+
School Table 学校桌
+-----------+-----------+ | school_id | name | +-----------+-----------+ | 1 | MIT | | 2 | BI School | +-----------+-----------+
And use school_id as a foreign key in user table. 并使用school_id作为用户表中的外键。
Another approach is: 另一种方法是:
User Table 用户表
+--------+-------+ | usr_id | name | +--------+-------+ | 7 | john | | 7 | john | | 6 | harry | +--------+-------+
School Table 学校桌
+-----------+-----------+ | school_id | name | +-----------+-----------+ | 1 | MIT | | 2 | BI School | +-----------+-----------+
Users to schools table 用户到学校表
+----+---------+-----------+ | id | user_id | school_id | +----+---------+-----------+ | 1 | 7 | 1 | | 2 | 7 | 2 | | 3 | 6 | 1 | +----+---------+-----------+
Which approach would be best? 哪种方法最好? Is there any other approach we can try here. 我们还有其他方法可以尝试吗?
Based on normalization rules you have a many to many relationship(one student can attend one or more school and one school can have one or more students) the second approach is the correct one. 根据规范化规则,您具有多对多关系(一个学生可以上一所或多所学校,而一所学校可以有一个或多于一名学生),第二种方法是正确的方法。
More info here StackOverflow - Normalization in Plain English 更多信息在这里StackOverflow-普通英语的规范化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.