Question

I am developing my tables for a website that I am working on, and after not doing much SQL in the past couple of years, I am a little bit rusty. I understand the concept of normalization and such, but this is a little bit confusing:

I have a few sections in the website where contacts will be listed, primarily on an Events page and on the website's Contact page. I would have a relation table that would be something like (just as an example) eventlisting hasmany contacts, which is fine because at least I could have a number of contacts attached to an event listing.

Would it be fine to have a table that just has a foreign key that points to my primary contacts table, and use that for listing my primary contacts?

Was it helpful?

Solution

Generally in a one-to-many relationship, it's the "many" that contains the FK. That way many event listings can be mapped to the same contact listing.

So in your case, the contacts table would have a FK to your eventlisting table.

If for some reason you need to have a many-to-many relationship, you'd want to create a join table that both your eventlisting and contacts table would have FKs to.

OTHER TIPS

If an event can only have one primary contact, you can do it in two ways:

  1. Add a primary_contact_id column in the Events table, as a FK to the Contacts table.

  2. Use a relation table Event_Primary_Contacts, containing two FK's: Event_id and Contact_id. These should be a unique key in the table.

You could also add a boolean Primary column to the Event_Contacts table. However, this doesn't allow the DB to implement the constraint that there can be only one primary contact for each event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top