简体   繁体   English

MySQL-链接多个表的建议-对吗?

[英]MySQL - Advice linking Multiple Tables - Is this right?

Would someone mind advising me please regarding this table setup. 有人介意给我关于此表设置的建议。

Its the first time designing a database. 这是第一次设计数据库。 This will be a part of it. 这将是其中的一部分。

Its a report writing application. 它是一个报告编写应用程序。 Multiple Engineers can be assigned to attend any job/report and multiple engineers can author the report as well as attending. 可以分配多位工程师参加任何工作/报告,多位工程师可以撰写报告和参加会议。

Is this the best way to do this. 这是做到这一点的最佳方法。 I would need to be able to search attendees and authors separately in the application. 我将需要能够在应用程序中分别搜索参与者和作者。

Thanks very much for the assistance. 非常感谢您的协助。

链接到表格图片

You have, I believe, two tables containing entities. 我相信您有两个包含实体的表。 The entities are employee and report . 实体是employeereport

These entities have two different many-to-many relationships: author and attendee . 这些实体具有两种不同的多对多关系: authorattendee

So your tables are these 所以你的桌子是这些

employee                report
--------                -----
employee_id (PK)        report_id (PK)
surname                 title
givenname               releasedate
whatever                whatever

Then you have two many:many relationship tables with the same columns as each other. 然后,您将获得两个多:许多关系表,它们彼此之间具有相同的列。 One is author and the other is attendee . 一个是author ,另一个是attendee

author / attendee
------
employee_id PK, FK to employee.employee_id
report_id   PK, FK to report.report_id

Notice the compound (two-column) primary keys. 注意复合(两列)主键。

+---------------------+\   /+-------------+\   /+-----------------------+
|                     +-----+   author    +-----+                       |
|                     |/   \+-------------+/   \|                       |
|    employee         |                         |     report            |
|                     |                         |                       |
|                     |\   /+-------------+\   /|                       |
|                     +-----+  attendee   +-----+                       |
+---------------------+/   \+-------------+/   \+-----------------------+


           \   /
           -----    means a many-to-many relationship
           /   \

When you determine an employee is an attendee for a certain report, you insert a row into the attendee table with the correct employee and report. 当您确定某个员工是某个报表的参与者时,请在带有正确员工和报表的参与者表中插入一行。

If you want, for example, all authors for each report you can do this sort of thing: 例如,如果您想要每个报告的所有作者,则可以执行以下操作:

  SELECT r.title, r.releasedate, 
         GROUP_CONCAT(e.surname ORDER BY e.surname SEPARATED BY ',')surnames
    FROM report r
    LEFT JOIN author a ON r.report_id = a.report_id
    LEFT JOIN employee e ON a.report_id = e.report_id
   GROUP BY r.title, r.releasedate
   ORDER BY r.releasedate DESC

The LEFT JOIN operations allow your query to find reports that have no authors. LEFT JOIN操作允许您的查询查找没有作者的报告。 Ordinary inner JOIN operations would suppress those rows from your result set. 普通的内部JOIN操作会从结果集中隐藏那些行。

There is a limitation with this strict E:R design. 这种严格的E:R设计存在局限性。 For many kinds of reports, (scientific papers for example) the order of authors is critically important. 对于许多类型的报告(例如科学论文),作者的顺序至关重要。 (You want to start an academic food fight? List the authors of a paper in the wrong order.) (您想发起一场学术性的食品大战?以错误的顺序列出论文的作者。)

So you author table might also contain an ordinal value. 因此,您的作者表可能还包含一个序数值。

author
------
employee_id PK, FK to employee.employee_id
report_id   PK, FK to report.report_id
ordinal     INT

and your report query would contain this line. 并且您的报告查询将包含此行。

         GROUP_CONCAT(e.surname ORDER BY e.ordinal SEPARATED BY ',')surnames

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

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