SA0075B : Avoid adding constraints with default system generated name

The topic describes the SA0075B analysis rule.

Message

Avoid adding constraints with default system generated name

Description

The rule checks CREATE TABLE and ALTER TABLE statements for constraints created without specifying constraint name.

Such constraints will be are created with system generated name.

How to fix

Review the CREATE/ALTER TABLE statement and explicitly provide name for the constraint.

Scope

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

Parameters
Name Description Default Value
IgnoreTemporaryTables

Ignore temporary tables.

yes

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
8 minutes per issue.
Categories

Naming Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE Persons (
 2    ID int NOT NULL,
 3    LastName varchar(255) NOT NULL REFERENCES Persons(LastName),
 4    FirstName varchar(255),
 5    Age int,
 6    UniqueId int UNIQUE CLUSTERED,
 7    CONSTRAINT PK_Person PRIMARY KEY (ID),
 8    FOREIGN KEY (FirstName,LastName) REFERENCES Persons(FirstName,LastName)
 9);
10
11ALTER TABLE Persons ADD 
12CONSTRAINT PK_Person PRIMARY KEY (ID,LastName),
13FOREIGN KEY (FirstName,LastName) REFERENCES Persons(FirstName,LastName);
14
15ALTER TABLE Persons ADD 
16CONSTRAINT PK_Person PRIMARY KEY (ID,LastName),
17FOREIGN KEY (FirstName,LastName) REFERENCES Persons(FirstName,LastName);
18
19CREATE TABLE Test.Greeting (
20    greetingId INT IDENTITY (1,1) PRIMARY KEY,
21    Message nvarchar(255) NOT NULL,
22);
23
24CREATE TABLE Persons (
25    ID int NOT NULL CONSTRAINT PK_Persons PRIMARY KEY CONSTRAINT DF_ID default(0),
26    LastName varchar(255) NOT NULL DEFAULT ('-'),
27    FirstName varchar(255) CHECK( FirstName like 'Bo%'),
28    Age int,
29    CHECK( Age > 20) 
30);
31
32
33ALTER TABLE Persons ADD PRIMARY KEY (ID);
34ALTER TABLE Persons ADD CONSTRAINT PK_ID PRIMARY KEY (ID);
35ALTER TABLE MyTable ADD CONSTRAINT DEF_Col1 DEFAULT 50 FOR Col1 ;  
36ALTER TABLE MyTable ADD DEFAULT '' FOR Col2 ;  
37ALTER TABLE MyTable ADD CONSTRAINT Chk_Col1 CHECK (Age > 20);
38ALTER TABLE MyTable ADD CHECK (Age > 20);
39ALTER TABLE dbo.MyTable ADD DEFAULT (0) FOR SomeIntCol ;  
40ALTER TABLE MyTable ADD DEFAULT ((0)) FOR SomeIntCol ; 
41
42ALTER TABLE MyTable ADD SomeIntCol INT CONSTRAINT Chk_Col1 CHECK (SomeIntCol > 20);
43ALTER TABLE MyTable ADD SomeIntCol INT CHECK (SomeIntCol > 20); 
44ALTER TABLE MyTable ADD SomeIntCol INT DEFAULT (0); 
45ALTER TABLE MyTable ADD SomeIntCol INT CONSTRAINT DEF_Col1 DEFAULT (0);

Analysis Results
  Message Line Column
1 SA0075B : The constraint will be added with default system generated name. 3 35
2 SA0075B : The constraint will be added with default system generated name. 6 17
3 SA0075B : The constraint will be added with default system generated name. 8 4
4 SA0075B : The constraint will be added with default system generated name. 13 0
5 SA0075B : The constraint will be added with default system generated name. 17 0
6 SA0075B : The constraint will be added with default system generated name. 20 34
7 SA0075B : The constraint will be added with default system generated name. 26 35
8 SA0075B : The constraint will be added with default system generated name. 27 27
9 SA0075B : The constraint will be added with default system generated name. 29 4
10 SA0075B : The constraint will be added with default system generated name. 33 24
15 SA0075B : The constraint will be added with default system generated name. 43 39
16 SA0075B : The constraint will be added with default system generated name. 44 39
See Also

Other Resources