sqlforeign-key-relationshipsurrogate-key

Database design - children of join tables key choice


I searched for answers to the following, but got answers mostly related to whether or not to use a surrogate at all, not about whether or not to then use compound keys on children of join tables.

I have a table attendees and a table events.

The join table is event_attendees, containing two foreign keys attendee_id and event_id, and also has a surrogate primary key id

let's now say attendees in an event can have schedule_items.

I would normally simply use the columns event_attendee_id, schedule_item_id in the table event_attendee_schedule_items.

What drawbacks, if any, would using the following compound foreign key set up have over the above?

attendee_id, event_id, schedule_item_id (compound key instead of a direct link to the event_attendees table)?


Solution

  • I would recommend the first approach if:

    The last reason gives us an additional hint: it depends on amount of data in the tables and the queries you make! In some cases your performance will benefit from composite keys (and composite indexes), in other cases not. See this question for a detailed discussion.