在MySQL中,为多对多连接正确索引链接表,可以遵循以下步骤:
CREATE TABLE ABLink (
A_id INT NOT NULL,
B_id INT NOT NULL,
PRIMARY KEY (A_id, B_id),
FOREIGN KEY (A_id) REFERENCES A(id),
FOREIGN KEY (B_id) REFERENCES B(id)
);
CREATE INDEX idx_A_id ON ABLink (A_id);
CREATE INDEX idx_B_id ON ABLink (B_id);
SELECT B.*
FROM B
JOIN ABLink ON B.id = ABLink.B_id
WHERE ABLink.A_id = <some_value>;
INSERT INTO ABLink (A_id, B_id)
VALUES (<some_value>, <some_other_value>);
要删除A表中的某个记录与B表中的某个记录之间的关联,可以从链接表中删除相应的记录:
DELETE FROM ABLink
WHERE A_id = <some_value> AND B_id = <some_other_value>;
通过以上步骤,可以在MySQL中为多对多连接正确索引链接表。
领取专属 10元无门槛券
手把手带您无忧上云