Skip to content

SA0068A : Check all Check Constraints in the current database for following specified naming convention

Inconsistent or unclear check constraint naming can lead to confusion and maintenance challenges.

Having clear and consistent naming conventions for check constraints is crucial for maintaining database integrity and readability. Poorly named constraints make it difficult to understand their purpose, leading to potential misinterpretation or errors when modifying schemas or writing queries.

For example:

-- Example without a clear naming convention
ALTER TABLE Employee
ADD CHECK (Age >= 18);

This example lacks a clear naming strategy, which makes it challenging to identify the constraint in future modifications or troubleshooting.

  • Readability: Unnamed or poorly named constraints hinder understanding of database logic.

  • Maintenance: Difficulty in identifying and managing constraints during schema updates or debugging.

`

To ensure clear and consistent naming conventions for check constraints, follow these methods to improve database integrity and readability.

Follow these steps to address the issue:

1.Identify the check constraints that do not adhere to a clear naming convention. You can list existing constraints by querying the INFORMATION_SCHEMA.CHECK_CONSTRAINTS view or using SQL Server Management Studio (SSMS).

2.Develop a naming convention that reflects both the table and the purpose of the constraint, such as CK_TableName_ColumnName_Condition .

3.Rename the constraint using the newly defined naming convention. Use sp_rename to change the constraint name.

For example:

-- Example of renaming a constraint for clarity
ALTER TABLE Employee
DROP CONSTRAINT [Check_1];
ALTER TABLE Employee
ADD CONSTRAINT CK_Employee_Age_Adult CHECK (Age >= 18);

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

Name Description Default Value
ColumnConstraintNamePattern Column level check constraint name pattern. CK*{table_name}*{column_name}
TableConstraintNamePattern Table level check constraint name pattern. regexp:CK*{table_name}*[A-Za-z_]+

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

8 minutes per issue.

Naming Rules, Code Smells

There is no additional info for this rule.

Analysis Rules