HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Best way to linking different entities to a single table

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
linkingwaydifferentsingleentitiestablebest

Problem

I'm running for office and have created a web app for tracking my door knocks to voters at their homes. The database contains a table called voters that contains all the necessary information about voters in my community. I'm using mysql.

I'd like to add a new feature to track donors to my campaign. Not all of these donors live in the community and do not vote in the district where I'm running for office. I do not need to track the same kind of info for these individuals as I do for the voters so I'm going to place these individuals into a table called nonvoters.

Now, the individuals in my voters table can also make donations, and I want to track those as well.

Finally, I'd also like to track donations from organizations, so I also need an organizations table for storing information about the organizations.

To track the donations from both voters, nonvoters and organizations, I want to set up a new table called donations. This table would contain the appropriate details about each donation.

But I'm uncertain as to what the best structure is for linking the donations table to the voters, nonvoters, and organizations table should be. If I create a column called donor_id in the table to key it to the donors information, there's no way to know which table that ID refers to. So do I set up three columns, nonvoter_id, voter_id and org_id and insert the ID into the applicable column depending on whether the donor is a voter? This seems weird.

Or maybe I create a new column in each of the three tables called donor_id that I can use to link my data to the donations table. If I went this route, it seems like I'd have to do a some behind the scenes work to ensure the donor_id was unique and was keyed to the data inside the donations table.

Or maybe there are other approaches I'm not familiar with. Any guidance is appreciated.

Solution

Good question, you have provided a quite engaging context and analysis.

According to my understanding of your scenario description, some important aspects can be handled by means of (Super)Type-Subtype relationships. In this regard, this post provides relevant information.

Assumed business rules

As per your current specifications, the following assertions appear valid:

  • A Party[1] is either a Person or an Organization.



  • A Party grants zero-one-or-many Donations.



  • A Person can be a Voter and a NonVoter.



Preliminary logical data model

Therefore, I have derived the IDEF1X[2] logical data model presented in Figure 1.

Party and Donation

As you can see, the incorporation of the Party entity as an exclusive supertype provides the possibility for a Person or an Organization to be able to grant Donations, since the PartyId attribute migrates[3] from Party to Donation, but I have assigned a role name[4] (i.e., DonorId) to such attribute in order to make it more meaningful in the context of the Donation entity.

Person and Organization

In the same way, PartyId migrates to Person as PersonId, and to Organization as OrganizationId, and these two entities, in turn, can hold their specific attributes.

Voter and NonVoter

I consider that, in your business context, a given Person can be a Voter and a NonVoter over time, depending on the particular District in which said Person is located, thus I have temporarily depicted this circumstances in a nonexclusive supertype cluster, but I consider that we need to clarify some points in this respect.

Aspects that require clarification

The goal of this data modeling excersise is to capture the things of interest with respect to your business context requirements, and one can serve that purpose by determining the entities involved along with their respective attributes (including, with especial attention, the ones that uniquely identify each entity ocurrence) and the way in which such entities are interconected. In this manner, the semantic value that said aspects hold for you, and your data users, is paramount. The better these factors are comprehended, the better they can be depicted in a data model.

Having said that, I have created three more models (also preliminary) in order to have a vehicule to exemplify some ideas about your scenario. They are contained in this PDF, which you can download from Dropbox.

Then, here are the concrete aspects that need clarification:

  • How do you plan to inscribe people in your system? (a) Do you have access to the electoral register (or electoral roll)? (b) Will you enter this data once you have door knocked their house?



  • Which attributes do you want to store about a given DoorKnock?



  • It seems reasonable to state that a Person may receive the status of Voter or NonVoter in the context of —as mentioned above— (a) the District in which such Person resides and (b) a determined Election instance. What do you think about it?



  • Maybe, a person must live within a certain period in a given district in order to be able to vote in a determined election.



  • What are the attributes of interest in relation to a Person?



  • Which are the particular attributes that you want to retain regarding a Voter?



  • What are the specific attributes that you wish to store about a NonVoter?



  • Perhaps, they are personal attributes that belong in the Person entity.



  • It also appears fair to say that a Donation can be granted only in the context of an Election occurrence, is this correct?



  • This possibility is expressed in all the three models.



  • With respect to Donations granted by Organizations, (a) are organizations that reside outside your election district allowed to make donations, or (b) is this only permitted for organizations located in your specific district?



  • If this is allowed only to local Organizations, it would be necessary to move the District PK from Person to Party. I am thinking that, actually, that could be the right place for this attribute, but let me know your ideas about it. I have illustrated this option in Model C.



  • How much Donations are allowed by each Party? (a) One, (b) a fixed number, or (c) an undefined quantity?



  • Option (c) is also currently presented in the three models.



  • Are there restrictions related to (a) a MinimumAmount and (b) a MaximumAmount per individual Donation?



  • Do you have restrictions associated to (a) a MinimumAmount and (b) a MaximumAmount per the total set of Donations granted by a certain Party?



  • How do you identify an Election instance?



  • I think that a given election can be uniquely identified by means of the date in which it is celebrated (as depicted in Model A) and, perhaps, by a combination of the date and the district where it is held (as depicted in Model B).



  • What attribute (or group or attributes) uniquely identify a District occurrence?



  • As you know, I hav

Context

StackExchange Database Administrators Q#117856, answer score: 3

Revisions (0)

No revisions yet.