Skip to content

SA0067B : Check all Unique Key Constraints for following specified naming convention

Inconsistent or unclear unique constraint naming can impact database understanding and maintainability.

In SQL Server, unique constraints ensure that all values in a column or a set of columns are distinct across rows. However, if these constraints are not named meaningfully, it can lead to confusion and maintenance difficulties. Meaningful naming helps in understanding database design and improving code readability. Additionally, it assists in debugging and managing unique constraints effectively.

For example:

-- Example of a poorly named unique constraint
ALTER TABLE Employees ADD CONSTRAINT UQ_1 UNIQUE (EmployeeID);

This example is problematic because the constraint name UQ_1 does not convey any useful information about the table or columns involved. A more descriptive name would enhance clarity and maintainability.

  • Vague or non-descriptive constraint names can lead to confusion during troubleshooting and maintenance.

  • Consistent naming conventions are crucial for effective collaboration among team members who work with the database.

To ensure effective maintenance and collaboration, unique constraints should be named meaningfully and consistently. This helps in understanding database design and improves code readability.

Follow these steps to address the issue:

1.Identify the unique constraint that needs renaming. You can do this by querying the sys.objects and sys.tables system views or using SQL Server Management Studio (SSMS) to browse the database structure.

2.Determine a naming convention that accurately reflects the purpose of the constraint and the columns it is associated with, such as UQ_TableName_ColumnName .

3.Rename the constraint using the sp_rename stored procedure to apply the new meaningful name. Execute the following SQL command:

For example:

-- Example of renaming a unique constraint
EXEC sp_rename
'Employees.UQ_1', -- Current constraint name
'UQ_Employees_EmployeeID'; -- New meaningful constraint name

The rule has a Batch scope and is applied only on the SQL script.

NameDescriptionDefault Value
NamePatternDfault unique key name pattern.UK*{table_name}*{column_list}
ColumnsListSeparatorSeparator which to be used for separating the columns in the {column_list} placeholder._
UniqueClusteredNamePatternName pattern for unique clustered constraints.AK*{table_name}*{column_list}
UniqueNonClusteredNamePatternName pattern for unique non-clustered constraints.-

The rule does not need Analysis Context or SQL Connection.

8 minutes per issue.

Naming Rules, Code Smells

There is no additional info for this rule.

CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE CLUSTERED (ID,LastName);
ALTER TABLE Persons
ADD UNIQUE (ID);
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UniqueId int not null CONSTRAINT AK_Person_UniqueId UNIQUE CLUSTERED
);
 MessageLineColumn
1SA0067B : The unique key constraing name UC_Person does not match the naming convention. The expected name is [UK_Persons_IDLastName].715
2SA0067B : The unique key constraing name UC_Person does not match the naming convention. The expected name is [AK_Persons_IDLastName].1115
3SA0067B : The unique key constraing name AK_Person_UniqueId does not match the naming convention. The expected name is [AK_Persons_UniqueId].2137

Analysis Rules