Question based on this question MySQL composite unique on FK's
DB MySQL, storage engine: InnoDB.
I have a table Plan:
- ID
- clubber_id (Foreign key开发者_JS百科 to clubbers table)
- event_id (Foreign key to events table)
Each clubber can only create one plan per event. That is clubber_id and event_id should ideally be unique composite key.
Is there any point performance-wise to create such a composite key with clubber_id and event_id. I already have foreign keys in place and uniqueness is checked in my business logic.
It's not really a performance thing, but defining a unique composite key on your foreign keys assures that there's uniqueness in the database. If you do that, you can skip doing the checking in your business logic (as long as you can handle the failure situation of attempting to add a duplicate). In general, the composite key uniqueness validation in MySQL is very fast; having it done in the database is usually not a performance problem, and it can clean up your business logic a bit by removing a redundant check. Essentially, performing this sort of uniqueness check is something that's in the database realm; why not let your database do the validation with the uniqueness constraint?
精彩评论