Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server 2016 (13.x) and later versions
Azure SQL Database
Azure SQL Managed Instance
SQL database in Microsoft Fabric
You can define a primary key in the SQL Server Database Engine by using SQL Server Management Studio or Transact-SQL. Creating a primary key automatically creates a corresponding unique clustered index. However, your primary key can be specified as a nonclustered index instead.
The code samples in this article use the AdventureWorks2022
or AdventureWorksDW2022
sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.
A table can contain only one PRIMARY KEY
constraint.
All columns defined within a PRIMARY KEY
constraint must be defined as NOT NULL
. If nullability isn't specified, all columns participating in a PRIMARY KEY
constraint have their nullability set to NOT NULL
.
Creating a new table with a primary key requires CREATE TABLE
permission in the database and ALTER
permission on the schema in which the table is being created.
Creating a primary key in an existing table requires ALTER
permission on the table.
Caution
If you want to redefine the primary key, any relationships to the existing primary key must be deleted before the new primary key can be created. A message will warn you that existing relationships will be automatically deleted as part of this process.
A primary key column is identified by a primary key symbol in its row selector.
If a primary key consists of more than one column, duplicate values are allowed in one column, but each combination of values from all the columns in the primary key must be unique.
If you define a compound key, the order of columns in the primary key matches the order of columns as shown in the table. However, you can change the order of columns after the primary key is created. For more information, see Modify Primary Keys.
The following example creates a primary key on the column TransactionID
in the AdventureWorks2022
database.
ALTER TABLE [Production].[TransactionHistoryArchive]
ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);
The following example creates a table and defines a primary key on the column TransactionID
in the AdventureWorks2022
database.
CREATE TABLE [Production].[TransactionHistoryArchive1] (
TransactionID INT IDENTITY(1, 1) NOT NULL,
CONSTRAINT PK_TransactionHistoryArchive1_TransactionID PRIMARY KEY CLUSTERED (TransactionID)
);
The following example creates a table in the AdventureWorks2022
database, with a nonclustered primary key on the column CustomerID
. Then, it adds a clustered index on TransactionID
.
Create a table to add the clustered index.
CREATE TABLE [Production].[TransactionHistoryArchive1] (
CustomerID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID(),
TransactionID INT IDENTITY(1, 1) NOT NULL,
CONSTRAINT PK_TransactionHistoryArchive1_CustomerID PRIMARY KEY NONCLUSTERED (CustomerID)
);
Now add the clustered index.
CREATE CLUSTERED INDEX CIX_TransactionID
ON [Production].[TransactionHistoryArchive1] (TransactionID);
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Module
Design a Performant Data Model in Azure SQL Database with Azure Data Studio - Training
Learn how to create a data model, tables, indexes, constraints, and use data types with Azure data studio.
Certification
Microsoft Certified: Azure Database Administrator Associate - Certifications
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.