Many-to-many Mapping table
suggest changeRemarks
- Lack of an
AUTO_INCREMENTid for this table – The PK given is the ‘natural’ PK; there is no good reason for a surrogate. MEDIUMINT– This is a reminder that allINTsshould be made as small as is safe (smaller ⇒ faster). Of course the declaration here must match the definition in the table being linked to.UNSIGNED– Nearly all INTs may as well be declared non-negativeNOT NULL– Well, that’s true, isn’t it?InnoDB– More efficient than MyISAM here because thePRIMARY KEYis clustered with the data.INDEX(y_id, x_id)– ThePRIMARY KEYmakes one direction efficient; this index makes the other direction efficient. Do not also declare itUNIQUE; that extra check slowsINSERTs.INDEX(y_id)alone would still includex_id, because InnoDB appends the primary key to secondary indexes. Naming both columns makes the covering index obvious.
You may want to add more columns to the table; this is rare. The extra columns could provide information about the relationship that the table represents.
You may want to add FOREIGN KEY constraints.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents