patternsqlMinor
SQL Server Max number of Times a table can be referenced as a foreign key?
Viewed 0 times
cannumberreferencedsqlforeignmaxservertimestablekey
Problem
Is there any maximum on the number of times a single table can be referenced in a SQL Server database via foreign key?
IE
IE
Table A A_ID
Table B ~ B_ID, A_ID
Table C ~ C_ID, A_ID
Table D ~ D_ID, A_ID
...Solution
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:
Number of FOREIGN KEY Constraints In A Table
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.
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.
delete against the referenced table might be a little...painful.Here's what the documentation has to say:
Number of FOREIGN KEY Constraints In A Table
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.
It should also be noted, though, that, SQL Server's system views (e.g.,
sys.objects) use 32-bit integers as identifiers, the domain of which puts an implicit upper limit on the total number of foreign keys the database could contain.Not that you'd ever come close to exceeding that limit.
Context
StackExchange Database Administrators Q#11859, answer score: 8
Revisions (0)
No revisions yet.