Is there any maximum on the number of times a single table can be referenced in a SQL Server database via foreign key?
Table A A_ID Table B ~ B_ID, A_ID Table C ~ C_ID, A_ID Table D ~ D_ID, A_ID ...
migrated from stackoverflow.com Jan 29 '12 at 6:50
This question came from our site for professional and enthusiast programmers.
The recommended limit for the number of foreign keys in a table is 253. See Maximum Capacity Specifications for SQL Server on MSDN.
"Although a table can contain an unlimited number of FOREIGN KEY constraints, the recommended maximum is 253. Depending on the hardware configuration hosting SQL Server, specifying additional FOREIGN KEY constraints may be expensive for the query optimizer to process."
Note that these limits apply to the detail table, the table containing the foreign keys. You add foreign key constraints to the detail table.
The referenced (main) table is not affected by this, therefore there are no limits on how many times the main table can be referenced.
The short answer is, "No." The longer answer is that there is no explicit restriction, but since those relationships require metadata, the do consume memory and resources: if you have, say, 10,000 foreign key relationships to a single table's primary key, you're likely to find that a
Here's what the documentation has to say:
It should also be noted, though, that, SQL Server's system views (e.g.,
Not that you'd ever come close to exceeding that limit.
As far as i know the maximum number of references is 253. I can hardly imagine a case where you have to go even near that mark.
I'm not agree with @Oliver about the fact that there is no restriction with the number of foreign keys that can refer to a table.
This morning, I've received an error from sql server while trying to execute a very simple delete statement of one row of a table that is referred more than enough... More than 253 times!
The query processor ran out of stack space during query optimization. Please simplify the query.
Here is the explanation that I found :
SQL Server does not have a predefined limit on either the number of FOREIGN KEY constraints a table can contain (which reference other tables), or the number of FOREIGN KEY constraints owned by other tables that reference a specific table. Nevertheless, the actual number of FOREIGN KEY constraints is limited by your hardware configuration and by the design of your database and application. We recommend that a table contain no more than 253 FOREIGN KEY constraints, and that it be referenced by no more than 253 FOREIGN KEY constraints. Consider the cost of enforcing FOREIGN KEY constraints when you design your database and applications.
NOTES: The reason why I have so many foreign is because I build a system where the customer can design his own screens and one of my customers have dragged hundreds of dropdown. Shame on me letting the customer do that.