MySQL Forums
Forum List  »  Newbie

Re: best practices in many:many relationship
Posted by: Rick James
Date: January 27, 2013 11:40AM

re table c(cid int pk, aid references a, bid references b)
Skip cid, assuming the table has only the two fields.
CREATE TABLE AB (
aid ...
bid ...
PRIMARY KEY (aid, bid),
INDEX(bid, aid)
) ENGINE=InnoDB;

Rationale:
* Adding cid adds nothing.
* (aid, bid), or the opposite order is the 'natural PK'.
* A PK is UNIQUE, so PK(aid,bid) prevents dups.
* (bid, aid) lets you to go the other direction efficiently.
* All uses will say "Using index" in EXPLAIN, hence be efficient.
* Because of the differences in InnoDB and MyISAM indexes, InnoDB is more efficient than MyISAM.
* Even if aid and bid are bulky fields (ie, not simple INTs), there is no advantage of further normalization of such values.
* You can add FOREIGN KEY constraints if you like (I don't like).

Options: ReplyQuote


Subject
Written By
Posted
Re: best practices in many:many relationship
January 27, 2013 11:40AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.