Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does specifying a foreign key make it an index?

Tags:

sql

sql-server

I have a table T with a primary key id and foreign key f. Is f automatically indexed when it is specified as a foreign key? Do I need explicitly add an index for f ?

like image 599
Byron Whitlock Avatar asked Dec 28 '25 22:12

Byron Whitlock


2 Answers

No index is created so yes, you need add explicitly add an index.

Edited to add... I probably ought to add that the source table/column for the data in table T must have a unique index. If you try and make an FK to a column that isn't a unique index (either as a PK or with a UNIQUE constraint), the FK can't be created.

like image 76
Chris J Avatar answered Dec 31 '25 14:12

Chris J


No, it is a constraint, not an index.

see Are foreign keys indexed automatically in SQL Server?

like image 44
Donald Byrd Avatar answered Dec 31 '25 15:12

Donald Byrd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!