简体   繁体   English

关系数据库结构要求

[英]Relational database structure request

I am developing an relational database and am stuck on how to accomplish something. 我正在开发一个关系数据库,并且坚持如何完成某件事。

I have many tables, but two of which are the issue. 我有很多表,但是其中两个是问题。 I have a table of several thousand marine species, called "species". 我有一个表,有数千种海洋物种,称为“物种”。 I have another table that contains the global oceans divided into 188 different sub regions, called "subRegions". 我有另一个表格,其中包含被划分为188个不同子区域的全球海洋,称为“ subRegions”。 Naturally, each species can be found in multiple sub regions. 自然地,每个物种都可以在多个子区域中找到。 The subRegions table had "id", "name", and "coordinates" as it fields. subRegions表具有“ id”,“ name”和“ coordinates”字段。

It is easy to set a foreign key to the subRegions id, from the species id, but how to set this so that multiple regions can be displayed, per species, at once? 从物种ID到subRegions ID设置外键很容易,但是如何设置它以便一次显示每个物种多个区域?

Any ideas? 有任何想法吗?

So what you have is a many-to-many relationship with optional participation in each. 因此,您所拥有的是多对多关系,每个关系中都有可选的参与。 So you'll find your species in none, one or many subRegions; 因此,您将在一个,一个或多个子区域中找到您的物种; each subRegion may contain none, one or many species. 每个子区域可能不包含一个物种,也可以包含多个物种。

Implementing this, you would have an intermediary table to record the relationship. 实现这一点,您将有一个中介表来记录关系。 That is your new table would be called speciesSubRegions. 那就是您的新表将被称为speciesSubRegions。 The table will contain species_id and subRegion_id as a composite primary key. 该表将包含species_id和subRegion_id作为复合主键。

The relationship then looks like species has a one-to-many relationship with speciesSubRegions with optional participation on the species side and mandatory participation on the speciesSubRegions side. 这样,该关系看起来就像物种与物种子区域具有一对多关系,在物种方面具有可选参与关系,而在物种子区域方面具有强制参与关系。

Then subRegions has a one-to-many relationship with speciesSubRegions which is optional on the subRegions side and mandatory on speciesSubRegions. 然后,子区域与物种子区域具有一对多关系,这在子区域侧是可选的,而在物种子区域是强制性的。

Clear as mud? 像泥一样清澈?

Relating species and sub regions via an intermediary table offers a few advantages, however I believe for some applications it may be a bit excessive to create another table. 通过中间表关联物种和子区域提供了一些优势,但是我认为对于某些应用程序来说,创建另一个表可能有点多余。 I realize this question has already been addressed, however below I have included a different way to accomplish the objective without needing to create an additional table. 我意识到这个问题已经解决,但是下面我提供了一种无需创建额外表即可实现目标的不同方法。

A simple and quick way to accomplish relating species with multiple sub regions. 一种简单快速的方法来完成具有多个子区域的相关物种。

  1. Create a new text field in the species table. 在种类表中创建一个新的文本字段。 You could call this "associated_sub_regions." 您可以将其称为“ associated_sub_regions”。
  2. Establish a relationship between associated sub regions and the primary key in the sub regions table. 在关联的子区域和子区域表中的主键之间建立关系。 Should look like species::associated_sub_regions = subRegions::id 应该看起来像是物种:: associated_sub_regions = subRegions :: id
  3. For each species add the primary key of each associated sub region in the "associated_sub_regions" text field. 对于每个种类,在“ associated_sub_regions”文本字段中添加每个关联子区域的主键。 After each primary key, add a carriage return. 在每个主键之后,添加回车符。 So it should look like ID return ID return ID return. 因此它看起来应该像ID返回ID返回ID返回ID。

Filemaker treats each return as a different value for the relationship, so at this point you could do a number of things to display the related data depending on the setup of your application. Filemaker将每个返回值视为关系的不同值,因此,根据您的应用程序的设置,此时您可以做很多事情来显示相关数据。

  • If you create a portal based on subRegions that only shows related values from species then it will display all sub regions that are related to the current species. 如果您基于subRegions创建一个仅显示物种相关值的门户,那么它将显示与当前物种相关的所有子区域。 This will only work on a table based on species, because the relationship evaluates from the context of species. 这仅适用于基于物种的表,因为这种关系是根据物种的上下文进行评估的。
  • If you have a main or control table meant only for navigation then you could follow the steps below to display the related sub regions on any layout, after using a script to set a value. 如果您有一个仅用于导航的主表或控制表,则可以在使用脚本设置值之后,按照以下步骤在任何布局上显示相关的子区域。
    1. Create a new text field on the control table for "current_species_id" that will contain the active species a person is viewing, or has selected. 在控制表上为“ current_species_id”创建一个新的文本字段,其中将包含一个人正在查看或已选择的活动物种。
    2. Establish relationship control::current_species_id = species2::id 建立关系控制::: current_species_id = species2 :: id
    3. Establish relationship species2::id = subRegions2::ID 建立关系物种2 :: id =子区域2 :: ID

If you are selecting a species from a portal you could set a script trigger to set the "current_species_id" field to the ID of the selected portal row, and then have another portal based on subRegions2 to display associated sub regions. 如果要从门户网站中选择物种,则可以设置脚本触发器以将“ current_species_id”字段设置为所选门户网站行的ID,然后使另一个基于subRegions2的门户网站显示关联的子区域。 You would then be able to do whatever with those sub regions. 然后,您将可以对那些子区域执行任何操作。 Select for more information, or to create a new window. 选择以获取更多信息,或创建一个新窗口。 This is just another way to accomplish the same objective, but more dependent on relationships than a new table. 这只是实现同一目标的另一种方式,但是与新表相比,它更依赖于关系。 Different tools for the same job. 用于同一工作的不同工具。

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

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