简体   繁体   English

mysql设计表中的主从关系

[英]Master-Detail relationship in design tables in mysql

I need to create 2 tables rosters and records, whereby each roster has many records. 我需要创建2个表名册和记录,其中每个名册都有许多记录。 A roster is based on class and there is maximum 1 roster for each class per day. 一个名册是基于班级的,每天每个班级最多有1个名册。 A record can have many students. 一条记录可以有很多学生。

I wonder that this master-detail relationship design is effective? 我想知道这种主从关系设计是否有效? My friend suggests that everything could be done in record table, since we want to display a roster of all the students in a class in a specific day. 我的朋友建议,所有事情都可以在记录表中完成,因为我们希望显示特定日期某班所有学生的花名册。 So in the master-detail approach, it takes query on 2 tables to accomplish the task, in contrast, it take only 1 query if there is only one record table. 因此,在主从方法中,需要对2个表进行查询才能完成任务,相反,如果只有一个记录表,则只需进行1个查询
Please advise. 请指教。

If you have attributes of ROSTER that you need to track over and above a primary key value, then database normalization rules require you to split out the ROSTER table. 如果您具有需要在主键值之上跟踪的ROSTER属性,则数据库规范化规则要求您拆分出ROSTER表。 Of course, you can always choose to denormalize, but that decision must be made with your eyes wide open. 当然,您始终可以选择非正规化,但是必须睁大眼睛做出决定。

If you don't have columns for ROSTER other than a key, then your friend is right. 如果您没有除键以外的ROSTER栏,那么您的朋友是对的。 Otherwise you should remind your friend that just because something could be done, doesn't mean it should be done. 否则,您应该提醒您的朋友,仅仅因为可以做某事,并不意味着就应该做。

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

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