Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What about using a nvarchar as a foreign key?

Up until now my foreign keys have always been uniqueidentifiers or an int.

Would it impact performance negatively if I used a nvarchar field as a foreign key? Are there any other considerations I should be concerned about?

like image 700
shenku Avatar asked Feb 26 '14 05:02

shenku


1 Answers

Although you can make nvarchar as foreign key but I would not suggest you to use nvarchar as foreign key. There are many reasons out which I think the one which is very essential is when you will be doing JOINS then nvarchar will be slower as compared to int.

Would it impact performance negatively if I used a nvarchar field as a foreign key?

Yes definitely there would be performance issue as an int uses 4bytes of data and is usually quicker to JOIN than NVARCHAR.

You can also check space used by using the stored procedure sp_spaceused by taking the column as int and nvarchar and you will find the difference.

EXEC sp_spaceused 'TableName'
like image 179
Rahul Tripathi Avatar answered Sep 21 '22 01:09

Rahul Tripathi