简体   繁体   中英

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.

One approach we can have is: We can have a db table

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.

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

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