That's a great question! You're right that if you enforce UNIQUE (workout_plan, exercise_order), that combination already uniquely identifies each row within the context of a specific plan.
The argument for adding a synthetic primary key (like an auto-incrementing id) even when you have a strong natural key or a unique combination like (workout_plan, exercise_order) often boils down to:
-
Stability:
- Your
exercise_order column is likely to change. If a user reorders the exercises in a workout plan, the values in the exercise_order column for several rows will need to be updated.
- If
(workout_plan, exercise_order) were your primary key, changing the primary key value is a more complex and potentially less performant operation than updating a non-key column.
- If you have other tables that need to reference a specific step in a workout plan (e.g., a
user_workout_log table that logs user performance for a particular exercise in a specific workout plan at a specific step), they would need to store (workout_plan, exercise_order) as a foreign key. If the order changes, all those foreign keys would also need to be updated via cascading updates, which can be tricky and computationally expensive.
- A synthetic
id (e.g., SERIAL in PostgreSQL) is assigned once when the row is created and never changes during the row's lifetime. This provides a stable, immutable reference to that specific "workout step instance."
-
Simplicity of Foreign Keys:
- If other tables need to reference a specific entry in
workout_details, it's often simpler and more efficient to reference a single integer column (workout_details_id) rather than a composite key ((workout_plan, exercise_order) or even (workout_plan, exercise) if that were your natural key). This makes defining foreign keys easier and potentially slightly faster for lookups.
-
Flexibility:
- While currently
(workout_plan, exercise) might also be unique (an exercise only appears once in a plan), future requirements could change. Maybe a plan includes the same exercise multiple times at different points (e.g., warm-up push-ups, main set push-ups). If you relied solely on (workout_plan, exercise) as a key, this wouldn't be possible. A synthetic key provides a unique identifier for each individual row, regardless of the combination of its attributes.
-
Standard Practice:
- Using a single-column surrogate primary key for junction or detail tables like this is a very common and well-understood database design pattern. It provides a clean, stable handle for each record.
In summary:
While (workout_plan, exercise_order) is unique and could technically serve as a primary key (along with (workout_plan, exercise)), using a synthetic id is often preferred in practice because:
- It provides a stable identifier that doesn't change when the
exercise_order (or other attributes like reps) are updated.
- It simplifies foreign key references from other tables.
- It offers flexibility for potential future schema changes.
You would still want to keep the UNIQUE (workout_plan, exercise_order) constraint to enforce the rule that each order number is used only once per plan. You might also want UNIQUE (workout_plan, exercise) if an exercise should indeed only appear once in the list for a given plan (though the synthetic key allows you to relax this constraint later if needed).
So, adding a SERIAL PRIMARY KEY column named id to workout_details is generally a good design pattern, even with the unique constraints you mentioned.