SA0071A : Check all Foreign Key Constraints in the current database for following specified naming convention |
![]() |
Inconsistent or unclear foreign key constraint naming can lead to confusion and maintenance challenges.

In SQL Server databases, it is crucial to maintain a consistent naming convention for foreign key constraints. Proper naming not only improves readability but also aids in database management and maintenance. By adhering to a structured pattern, database administrators and developers can identify relationships more easily, reducing confusion and errors.
For example:
1-- Poor naming convention for foreign keys 2ALTER TABLE Orders 3ADD CONSTRAINT FK_12345 FOREIGN KEY (CustomerId) REFERENCES Customers(Id);
This example illustrates a naming issue. Using arbitrary or non-descriptive names like FK_12345 fails to convey meaningful information about the relationship, making the database schema harder to understand and maintain.
-
Poor readability: Developers facing unfamiliar or legacy databases might struggle to understand the data model.
-
Increased risk of errors: Without clear naming conventions, mistakenly altering or misinterpreting constraints becomes more likely.
`

Rename foreign key constraints to follow a consistent and descriptive naming convention for better readability and maintenance.
Follow these steps to address the issue:
-
Identify the current naming pattern used for foreign key constraints in your database. For example, FK_TableName_ColumnName, where TableName is the name of the table and ColumnName is the column involved in the relationship.
-
Use the SQL Server Management Studio (SSMS) or a T-SQL script to find existing foreign key constraints with non-standard names. You can query the INFORMATION_SCHEMA.TABLE_CONSTRAINTS and INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS views to list them.
-
Rename the foreign key constraints following your defined naming convention using the sp_rename system stored procedure. This enhances clarity and prevents ambiguity.
For example:
1-- Example of renaming a foreign key constraint 2EXEC sp_rename 3 'FK_12345', -- Current constraint name 4 'FK_Orders_CustomerId', -- New descriptive constraint name 5 'OBJECT';

The rule has a ContextOnly scope and is applied only on current server and database schema.

Name | Description | Default Value |
---|---|---|
NamePattern |
Foreign key constraint name pattern. |
regexp:FK_[A-Z][A-Za-z_]+ |
ColumnsListSeparator |
Separator which to be used for separating the columns in the {parent_column_list} and {referenced_column_list} placeholders. |
_ |
PairedColumnsSeparator |
Separator which to be used for separating parent and referenced columns. |

The rule requires Analysis Context. If context is missing, the rule will be skipped during analysis.


Naming Rules, Code Smells

There is no additional info for this rule.
