Indexing foreign keys
2 answers
CREATE TABLE reftable (id INT NOT NULL PRIMARY KEY)
CREATE TABLE mytable (id INT NOT NULL, ref INT NOT NULL)
ALTER TABLE mytable
ADD CONSTRAINT fk_mytable_ref_reftable
FOREIGN KEY (ref) REFERENCES reftable (id)
CREATE INDEX ix_mytable_ref ON mytable (ref)
A column in another table (the one you are referring to) must be PRIMARY KEY
or have a constraint UNIQUE
defined on it, which means it already has an index.
+2
source to share