SA0067B : Check all Unique Key Constraints for following specified naming convention |
The topic describes the SA0067B analysis rule.
Check all Unique Key Constraints for following specified naming convention
The rule checks the naming of all unique constraints in the SQL code.
The NamePattern variable can be used to select or configure the desired pattern which will be used to check the object name.
The following placeholders will be replaced in the pattern during the key testing:
{table_name} – Name of the key’s parent table.
{column_list} – List of the key columns, ordered by key ordinal and separated by underscore(‘_’).
Regular expression patterns can be used, but the pattern must be prefixed with ‘regexp:’ string in order to be used as a matching regular expression.
Review the constraint name and rename it according to the naming convention.
The rule has a Batch scope and is applied only on the SQL script.
Name | Description | Default Value |
---|---|---|
NamePattern |
Dfault unique key name pattern. |
UK_{table_name}_{column_list} |
ColumnsListSeparator |
Separator which to be used for separating the columns in the {column_list} placeholder. |
_ |
UniqueClusteredNamePattern |
Name pattern for unique clustered constraints. |
AK_{table_name}_{column_list} |
UniqueNonClusteredNamePattern |
Name pattern for unique non-clustered constraints. |
– |
The rule does not need Analysis Context or SQL Connection.
Naming Rules, Code Smells
There is no additional info for this rule.
SQL
1CREATE TABLE Persons ( 2 ID int NOT NULL, 3 LastName varchar(255) NOT NULL, 4 FirstName varchar(255), 5 Age int, 6 CONSTRAINT UC_Person UNIQUE (ID,LastName) 7); 8 9ALTER TABLE Persons 10ADD CONSTRAINT UC_Person UNIQUE CLUSTERED (ID,LastName); 11 12ALTER TABLE Persons 13ADD UNIQUE (ID); 14 15CREATE TABLE Persons ( 16 ID int NOT NULL UNIQUE, 17 LastName varchar(255) NOT NULL, 18 FirstName varchar(255), 19 Age int, 20 UniqueId int not null CONSTRAINT AK_Person_UniqueId UNIQUE CLUSTERED 21); |
Message | Line | Column | |
---|---|---|---|
1 | SA0067B : The unique key constraing name UC_Person does not match the naming convention. The expected name is [UK_Persons_IDLastName]. | 7 | 15 |
2 | SA0067B : The unique key constraing name UC_Person does not match the naming convention. The expected name is [AK_Persons_IDLastName]. | 11 | 15 |
3 | SA0067B : The unique key constraing name AK_Person_UniqueId does not match the naming convention. The expected name is [AK_Persons_UniqueId]. | 21 | 37 |