简体   繁体   English

关系数据库设计问题

[英]Relational Database design issue

While designing a relational database I got the questoin in my head if the design I made could be made easier. 在设计一个关系数据库时,如果可以简化我所做的设计,我就会感到困惑。 The design consists of 4 tables: (simplified) 该设计包含4个表:(简化)

  • location 位置
  • office 办公室
  • school 学校
  • group of schools 一群学校

ERD PK = primary key, FK = foreign key PK =主键,FK =外键

Lets say we want to get details about a location. 假设我们要获取有关位置的详细信息。 If the location is a school we want to see the name, amount of students and the name of the group of schools its part of. 如果该地点是一所学校,我们希望查看其名称,学生人数以及所属学校组的名称。 If it is a office we only want to see the name of the office. 如果是办公室,我们只希望看到办公室的名称。 With the current design this is impossible to do in 1 query (with my knowledge). 使用当前的设计,这不可能在1个查询中完成(据我所知)。

question: Is there a better way to design this database so I can get the needed details about a location in 1 query. 问题: 有没有更好的方法来设计此数据库,以便可以在1个查询中获取有关位置的所需详细信息。

Although there may be a way to get these details in 1 query I am more interested in enhancing the database design. 尽管可能有一种方法可以在1个查询中获取这些详细信息,但我对增强数据库设计更感兴趣。

Thanks in advance, Knarfi 在此先感谢,Knarfi

ANSWER: I finally found the term that describes my problem: Polymorphic Association. 解答:我终于找到了描述我的问题的术语:多态关联。 Especially this question gave me a good answer. 特别是这个问题给了我一个很好的答案。

You can try something like this 您可以尝试这样的事情

SELECT location.*,school.*,office.*
FROM location
LEFT JOIN school ON school.locationID=location.locationID
LEFT JOIN office ON office.locationID=location.locationID

In result you will have NULL values for school fields if there will be office and vice versa. 结果,如果将有办公室,则对于学校领域,您将具有NULL值,反之亦然。 But you have to check this in your application and make decision there - database sends you all data. 但是您必须在您的应用程序中进行检查并在此做出决定-数据库将向您发送所有数据。

You can merge School and Office to one Entity, say: Organization with the following attributes: 您可以将School和Office合并为一个实体,例如:具有以下属性的组织:

  • Organization_ID (PM) Organization_ID(PM)
  • Type (1=School, 2=Office) 类型(1 =学校,2 =办公室)
  • Location_ID (FK) Location_ID(FK)
  • Group_ID (FK) (it can have values only for the schools, unless you want to add group info for offices too ex: Accounting, Sales, etc in table Groups) Group_ID(FK) (它只能具有学校的值,除非您也要添加办公室的组信息,例如表组中的“会计”,“销售”等)
  • Name 名称
  • NumOfMembers (it can be applied to both schools and offices) NumOfMembers (可同时应用于学校和办公室)

About your last question: " Although there may be a way to get these details in 1 query I am more interested in enhancing the database design. " I must say that this is not a design question but an implementation one. 关于您的最后一个问题:“ 尽管可能有一种方法可以在1个查询中获取这些详细信息,但我对增强数据库设计更感兴趣。 ”我必须说这不是设计问题,而是实现问题。 Remember that it is a very bad practice to change your design according to your implementation needs... 请记住,根据实现需求更改设计是非常糟糕的做法。

In any case you can use ONE query in both cases (yours and mine) see other people answers. 无论哪种情况,您都可以使用ONE查询(您和我的),看看其他人的答案。 tIp: try to use views... they are usefull in such cases. 提示:尝试使用视图...在这种情况下它们很有用。

The database design doesn't restrict a location from having both a school and office related to it at the same time. 数据库设计不会限制一个location同时让schooloffice与此地点相关。 (Could be multiple school and/or multiple office.) (可以是多个学校和/或多个办公室。)

There's several possible ways to retrieve information related to location . 有几种可能的方法来检索与location相关的信息。

Here's one approach: 这是一种方法:

SELECT 'school' AS source
     , s.name
     , s.students
     , g.name AS school_group_name
  FROM location sl
  JOIN school s
    ON s.location_id = sl.id
  LEFT
  JOIN group_of_schools g
    ON g.id = s.group_id
 UNION ALL
SELECT 'office' AS source
     , o.name
     , NULL
     , NULL
  FROM location ol
  JOIN office o
    ON o.location_id = ol.id

If you need to restrict to a specific set of location, you'd need to add WHERE clause on each SELECT . 如果需要限制在一组特定的位置,则需要在每个SELECT上添加WHERE子句。

Add an ORDER BY if you want the rows returned in a particular order. 如果ORDER BY特定顺序返回行,请添加ORDER BY

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

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