您对mysql workbench中的表有何看法?链接表?

时间:2016-03-19 07:36:03

标签: mysql sql database-design workbench

我正在用Java编写一个小程序,如果我把名字和出生日期放在文本字段中,它会在学期和考试中获取学生的结果。在我创建的表格下方,我有一个问题要链接这些表格:

Click to see the image

1 个答案:

答案 0 :(得分:0)

您的数据库结构没有链接可能性。

没有表格可以将其链接到另一个表格。

您必须识别一对多和多对多关联,并使用适当的方法表达它们。

示例:

我认为表学生和表课程有多对多关系,你应该用以下表格来表达:

ideleve | idcourse

你必须在你的结构上找出所有这些关系来设计任何查询。

根据您的意见,我更新了答案。实际上,我应该投票决定关闭这个问题,因为它太宽泛而且不会过度编码。

问题设定示例:

Each student is identified by:
id | name | surname | born_date

Each course is identified by
id | course_title

Students can enroll in multiple courses

Courses can be held in 1st, 2nd semester or both maintaining the same id and title

There are up to 3 exams per course per semester

Notes of each exam can go from 1 to 5 with 1 being the lowest

Students have to take all the exams, if they do not take an exam their vote is considered 0 for that exam

这将导致一种可能:

DB STRUCTURE

table students
id_student | name | surname

table courses
id_course | course_title

table exams
id_student | id_course | semester | exam | grade

您可以从列名中理解隐式关系。

然后你可以从编码开始......

但在这里,我对学生,课程,考试和天桥及其关系做了很多假设!

要设计数据库,需要对实体和关系进行分析。

你正在尝试制作的程序肯定非常简单,如你所说,这种分析并不复杂,但必须要完成,没有人能为你做到这一点。

根据您最后的评论,最终澄清您可以做的问题:

DB STRUCTURE

table students
id_student | name | surname | birthdate

table courses
id_course | course_title | year | semester

table exams
id_student | id_course | year | semester | test | exam | grade
表格考试的例子可以是:

id_student | id_course | year | semester | test | exam | grade
--------------------------------------------------------------
  2701     |  K409     | 2015 |    1     |   1  | NULL |  4
  2701     |  K409     | 2015 |    1     |   2  | NULL |  4
  2701     |  K409     | 2015 |    1     |   3  | NULL |  4
  2701     |  K409     | 2015 |    0     | NULL | NULL |  3
  2701     |  K405     | 2015 |    1     |   1  | NULL |  4
  2701     |  K405     | 2015 |    1     |   2  | NULL |  4
  2701     |  K405     | 2015 |    1     |   3  | NULL |  4
  2705     |  K405     | 2015 |    0     | NULL | NULL |  3
  2705     |  K409     | 2015 |    2     |   1  | NULL |  4
  2705     |  K409     | 2015 |    2     |   2  | NULL |  4
  2705     |  K409     | 2015 |    2     |   3  | NULL |  4
  2705     |  K409     | 2015 |    0     | NULL | NULL |  3
  2705     |  K407     | 2015 |    2     |   1  | NULL |  4
  2705     |  K407     | 2015 |    2     |   2  | NULL |  4
  2705     |  K407     | 2015 |    2     |   3  | NULL |  4
  2705     |  K407     | 2015 |    0     | NULL | NULL |  3

您可以使用多列密钥将课程链接到考试/考试:

 id_course-year-semester

将学生与考试联系起来/测试:

 id_student

通过考试/考试表将学生与课程联系起来:

id_student | id_course-year-semester

这意味着当学生注册课程时,这样的记录将出现在考试/测试表中:

id_student | id_course | year | semester | test | exam | grade
--------------------------------------------------------------
  2701     |  K409     | 2015 |    1     | NULL | NULL |  NULL

此致