Learn how to create, drop, disable, and enable a primary key in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), a primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a null value. A table can have only one primary key.
A primary key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.
You can create a primary key in SQL Server with the CREATE TABLE statement.
The syntax to create a primary key using the CREATE TABLE statement in SQL Server (Transact-SQL) is:
OR
Let's look at an example of how to create a primary key using the CREATE TABLE statement in SQL Server (Transact-SQL).
For example:
In this example, we've created a primary key on the employees table that is made up of only one field - the employee_id field.
We could have also created the primary key as follows:
Next, let's look at how to create a primary key in SQL Server (Transact-SQL) where the primary key is a composite key that is made up of more than one field.
For example:
In this example, we have created a primary key that is made up of two columns, the last_name and the first_name columns. These two fields would uniquely define the records in the employees table.
You can create a primary key in SQL Server (Transact-SQL) with the ALTER TABLE statement. However, you can only use the ALTER TABLE statement to create a primary key on column(s) that are already defined as NOT NULL. If the column(s) allow NULL values, you will not be able to add the primary key without dropping and recreating the table.
The syntax to create a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL) is:
Let's look at an example of how to create a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL).
For example:
In this example, we've created a primary key on the existing employees table called employees_pk. It consists of the field called employee_id. Again, it is important to note that the employee_id must already be defined as a NOT NULL field for this ALTER TABLE statement to succeed. If the employee_id column allows NULL values, the employees table will have to be dropped and recreated with employee_id defined as a NOT NULL field for the primary key to be created.
We could also create a primary key with more than one field as in the example below:
In this example, we've created a primary key on the employees table that consists of the last_name and first_name fields. The last_name and first_name fields must be defined as NOT NULL in the employees table for this primary key to be created succesfully.
You can drop a primary key in SQL Server using the ALTER TABLE statement.
The syntax to drop a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL) is:
Let's look at an example of how to drop a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL).
For example:
In this example, we would drop the primary key on the employees table called employees_pk.
You can disable a primary key using the ALTER TABLE statement in SQL Server (Transact-SQL).
The syntax to disable a primary key using the ALTER INDEX statement in SQL Server (Transact-SQL) is:
Let's look at an example of how to disable a primary using the ALTER INDEX statement in SQL Server (Transact-SQL).
For example:
In this example, we would disable the primary key on the employees table called employees_pk.
You can enable a primary key using the ALTER INDEX statement in SQL Server (Transact-SQL).
The syntax to enable a primary key using the ALTER INDEX statement in SQL Server (Transact-SQL) is:
Let's look at an example of how to enable a primary key using the ALTER INDEX statement in SQL Server.
In this example, we're enabling a primary key on the employees table called employees_pk.