SA0048B : The table is created without a a primary key

Ensure all tables have a primary key defined immediately to avoid integrity and performance issues.

Description

In SQL Server, tables ideally should have a primary key to enforce data integrity and optimize database operations. A primary key uniquely identifies each record in a table and assists in maintaining orderly data storage, retrieval, and relationships between tables.

For example:

SQL
 1-- Example of poor practice
 2CREATE TABLE Example.Table1
 3(
 4 Id int NOT NULL,
 5 Column1 varchar(30) NOT NULL
 6);
 7
 8-- Example of best practice
 9CREATE TABLE Example.Table2
10(
11 Id int NOT NULL PRIMARY KEY,
12 Column1 varchar(30) NOT NULL
13);

If a primary key is not defined at table creation or added promptly via an ALTER TABLE statement in the same batch, several issues may arise.

  • Data integrity risk due to potential duplicate entries, as each record should be uniquely identifiable.

  • Performance degradation, as operations like indexing and queries rely on a well-defined primary key for optimization.

  • Difficulty in establishing relationships with other tables, hindering normalization and efficient data modeling.

How to fix

Define a primary key for tables missing one to enhance data integrity and performance.

Follow these steps to address the issue:

  1. Identify the table lacking a primary key by reviewing the database schema or using SQL analysis tools.

  2. Determine the appropriate column or combination of columns that uniquely identify each record in the table. These columns should not contain null values and should represent unique data.

  3. If you are creating a new table, define the primary key within the CREATE TABLE statement.

  4. For an existing table without a primary key, use the ALTER TABLE statement to add the primary key. Ensure that the column(s) chosen do not contain duplicate values or NULLs before performing this operation.

For example, when creating a new table or altering an existing one:

SQL
 1-- Creating a new table with a primary key
 2CREATE TABLE Example.Table1
 3(
 4    Id int NOT NULL PRIMARY KEY,
 5    Column1 varchar(30) NOT NULL
 6);
 7
 8-- Adding a primary key to an existing table
 9ALTER TABLE Example.Table2
10ADD CONSTRAINT PK_Table2_Id PRIMARY KEY (Id);

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 requires Analysis Context. If context is missing, the rule will be skipped during analysis.

Effort To Fix
20 minutes per issue.
Categories

Performance Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE TABLE Table_WithOut_Pk
 2(
 3 CustomerID int,
 4 Company varchar(30) NOT NULL,
 5 ContactName varchar(60) NOT NULL, 
 6)
 7
 8CREATE TABLE Table_With_PK
 9(
10 CustomerID int PRIMARY KEY,
11 Company varchar(30) NOT NULL,
12 ContactName varchar(60) NOT NULL, 
13)
14
15CREATE TABLE MySchema.Table_With_Added_Column_PK
16(
17 Company varchar(30) NOT NULL,
18 ContactName varchar(60) NOT NULL, 
19)
20
21ALTER TABLE MySchema.Table_With_Added_Column_PK ADD CustomerID Int not null not null constraint PK_Table_With_Added_PK primary key clustered;
22
23
24CREATE TABLE MySchema.Table_With_Added_PK
25(
26 CustomerID int NOT NULL,
27 Company varchar(30) NOT NULL,
28 ContactName varchar(60) NOT NULL, 
29)
30
31ALTER TABLE Table_With_Added_PK ADD CONSTRAINT PK_Table_With_Added_PK primary key (CustomerID)

Analysis Results
  Message Line Column
1 SA0048B : The table Table_WithOut_Pk is being created without having a primary key defined. 1 13
See Also

Other Resources