简体   繁体   中英

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". Naturally, each species can be found in multiple sub regions. The subRegions table had "id", "name", and "coordinates" as it fields.

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?

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. The table will contain species_id and subRegion_id as a composite primary key.

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."
  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
  3. For each species add the primary key of each associated sub region in the "associated_sub_regions" text field. After each primary key, add a carriage return. So it should look like ID return ID return ID return.

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.

  • 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. 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.
    2. Establish relationship control::current_species_id = species2::id
    3. Establish relationship species2::id = subRegions2::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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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