开发者

MySQL composite unique on FK's

开发者 https://www.devze.com 2023-03-22 16:01 出处:网络
Question based on this question MySQL composite unique on FK's DB MySQL, storage engine: InnoDB. I have a table Plan:

Question based on this question MySQL composite unique on FK's

DB MySQL, storage engine: InnoDB.

I have a table Plan:

  1. ID
  2. clubber_id (Foreign key开发者_JS百科 to clubbers table)
  3. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消