duanguane1670 2015-05-22 12:13
浏览 31
已采纳

用于将主题分配给类的Mysql表设计

I was going to make a table where I can assign subjects to a class. I have various tasks related to this table like inserting marks for each subject,to know which subjects the class is studying,taking attendance for each subject etc.I am confused in deciding the table design.

First design is that i will make 3 columns and insert the values. But here i have to insert the same class many times For Eg.

ID - CLASS - SUBJECT

1 - 8th - English 2 - 8th - Maths 3 - 8th - Science

The second is the i will make separate column for each subject. But here the problem is what if i ever need to assign more subjects to a class.

ID - CLASS - SUBJECT1 - SUBJECT2 - SUBJECT3 - SUBJECT4

1 8th English Maths Computer Science 2 9th Maths Computer Science English

Please help me in deciding correct table design. Thanks

  • 写回答

1条回答 默认 最新

  • douwen9343 2015-05-22 12:21
    关注

    The occasion that you need to enter the same name more than once will cause some anomalies, such as update anomaly and deletion anomaly.

    To overcome that, you need to define classes and subjects on different tables, then create a many-to-many relation.

    For example:

    Classes(ID*, ClassName)
    Subjects(SubjectID*, SubjectName)
    SubjectinClasses(ID*, SubjectID*)
        ID is foreign key to Classes
        SubjectID is foreign key to Subjects
    
    * = (component of) PRIMARY KEY
    

    This design will help you normalize your tables. I suggest you to seek information on "referential integrity" topic if term "foreign key" is not familiar to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?