Clé primaire composée et index
Bonjour,
Dans une table, est ce qu'il est utile de rajouter un index sur une clé étrangère qui est une partie de la clé primaire composée ?
J'ai trois tables :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| CREATE TABLE IF NOT EXISTS client (
id_client INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
nom VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (id_client)
)
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS voiture (
id_voiture INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
nom VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (id_voiture)
)
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS client_voiture (
fk_client INTEGER UNSIGNED NOT NULL,
fk_voiture INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (fk_client, fk_voiture),
CONSTRAINT client_voiture_client FOREIGN KEY (fk_client) REFERENCES client (id_client) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT client_voiture_voiture FOREIGN KEY (fk_voiture) REFERENCES voiture (id_voiture) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
Nous avons donc la table client_voiture qui fait le liant entre les tables client et voiture. J'ai ma clé composée (PRIMARY KEY (fk_client, fk_voiture)), j'ai bien mes contraintes pour les clés étrangères.
J'aimerai savoir s'il est utile, ou quelle serait l'utilité de rajouter ces deux nouveaux index à la table client_voiture :
INDEX fk_client (fk_client)
INDEX fk_voiture (fk_voiture)
Ce qui nous donnerait au final :
Code:
1 2 3 4 5 6 7 8 9
| CREATE TABLE IF NOT EXISTS client_voiture (
fk_client INTEGER UNSIGNED NOT NULL,
fk_voiture INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (fk_client, fk_voiture),
INDEX fk_client (fk_client),
INDEX fk_voiture (fk_voiture),
CONSTRAINT client_voiture_client FOREIGN KEY (fk_client) REFERENCES client (id_client) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT client_voiture_voiture FOREIGN KEY (fk_voiture) REFERENCES voiture (id_voiture) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
Merci par avance pour vos lumières.