SQL PRIMARY KEY: Create & Add to an Existing Table

โšก Smart Summary

A primary key in SQL Server is a column or set of columns whose values uniquely identify every row in a table. Each table holds one primary key, and its columns must never be NULL.

  • ๐Ÿ”‘ What is a primary key: A primary key is one or more columns whose values are unique and identify each row in a table.
  • ๐Ÿšซ Core rules: A table allows only one primary key, every value must be unique, and no key column may contain NULL.
  • ๐Ÿ–ฑ๏ธ Two creation methods: SQL Server Management Studio Table Designer and the T-SQL CREATE TABLE statement both define a primary key.
  • ๐Ÿงฑ CONSTRAINT clause: Inside CREATE TABLE, the CONSTRAINT … PRIMARY KEY clause names the key and lists its column or columns.
  • โž• Add to an existing table: ALTER TABLE … ADD CONSTRAINT adds a primary key to the NOT NULL columns of an existing table.
  • ๐Ÿงฉ Composite key: A primary key can combine up to 16 columns, forming a composite key that is unique across the whole set.

SQL Server PRIMARY KEY: How to Create and Add to an Existing Table

What is Primary Key in SQL?

A primary key in SQL Server is a field, or a combination of fields, that identifies a record uniquely. It is a column or a set of columns in which every value is unique. In other words, no two rows can share the same primary key value.

Rules for Primary Key

  • Each table can have only one primary key.
  • All the values are unique, and the primary key value can uniquely identify each row.
  • The system will not allow inserting a row whose primary key value already exists in the table.
  • A primary key cannot be NULL.

The diagram below summarizes all the points above about the SQL primary key.

Diagram summarizing SQL primary key rules: unique values, one per table, and not NULL

How to Create Primary Key in SQL Server

You can create a primary key in two ways:

Let us study each method in detail.

SQL Server Management Studio

Step 1) Right-click on the table name and click Design.

Right-click the table and choose Design in SQL Server Management Studio

Step 2) Right-click on the column name and click Set Primary Key.

Right-click the column and choose Set Primary Key in Table Designer

Result: Course_Id is now a primary key, shown by a key icon beside the column.

Course_Id column marked with a key icon as the primary key

T-SQL: Create a Primary Key While Creating a New Table

Below is the syntax to create a table with a primary key from T-SQL.

Syntax:

CREATE TABLE <Table_Name>
(
Column1 datatype,
Column2 datatype,CONSTRAINT <Name> PRIMARY KEY (Column name)
.
);

Here, CONSTRAINT names the key, PRIMARY KEY marks the column that must stay unique, and every column also needs a data type.

Let us create a table with one column as a SQL primary key.

Query:

CREATE TABLE COURSE_TSQL_PK 
(Course_ID Int not Null,
 Course_name Varchar(20)
 CONSTRAINT PK PRIMARY KEY (Course_ID)	
)

Step 1) Run the query by clicking Execute.

Executing the CREATE TABLE query that defines the Course_ID primary key

Result: Course_Id is now a primary key on the COURSE_TSQL_PK table.

COURSE_TSQL_PK table showing Course_ID set as the primary key

Example: Let us see whether it allows entering multiple records with the same Course ID.

Step 1) Insert four rows with different Course_ID values.

Insert into COURSE_TSQL_PK values (1,'SQL');
Insert into COURSE_TSQL_PK values (2,'Python');
Insert into COURSE_TSQL_PK values (3,'SQL');
Insert into COURSE_TSQL_PK values (4,'C');

Step 2) Verify that all data was inserted successfully by running the SELECT query.

SELECT query result showing four rows with distinct Course_ID values

Note: You can insert duplicate values in a non-primary-key column.

Step 3) Now let us try inserting a new record with an existing Course_ID, which is the primary key.

Insert into COURSE_TSQL_PK values (4,'JAVA');

Result: The system does not allow inserting the new value, because 4 already exists in the Course_ID column, which is a primary key.

Error message rejecting a duplicate primary key value for Course_ID

How to Add Primary Key to Existing Table in SQL?

Now we will see how to add a primary key to an existing table in SQL.

You can use the ALTER statement to create a primary key. However, a primary key can be created only on columns that are defined as NOT NULL. You cannot create a primary key on a column that allows NULLs; if a column currently allows NULLs, you must first alter it to NOT NULL, or drop and recreate the table.

Here is the syntax:

ALTER TABLE tableName
ADD CONSTRAINT constraintName PRIMARY KEY (column_1, column_2, ... column_n);

For example:

ALTER TABLE students
ADD CONSTRAINT students_pk PRIMARY KEY (admission);

ALTER TABLE statement adding a primary key constraint named students_pk

We have added a primary key constraint to an already existing table. The constraint has been added on the admission column and assigned the name students_pk.

Interesting Facts!

  • A primary key can be a combination of multiple columns. This combination is known as the composite primary key.
  • A primary key can have a maximum of 16 columns.

FAQs

Both enforce uniqueness. A table allows only one primary key, and its columns cannot be NULL. A table may hold several unique keys, and a unique column can accept one NULL value. Primary keys usually define the main row identifier.

Yes. SQL Server automatically builds a unique index to enforce the primary key. By default this index is clustered, which physically orders the rows. You can specify NONCLUSTERED instead when another column already carries the clustered index.

A composite primary key combines two or more columns so their values are unique together, even when each column repeats individually. SQL Server allows a primary key to span up to 16 columns and a total key length of 900 bytes.

A primary key uniquely identifies each row within its own table and never allows NULLs. A foreign key stores values that must match a primary or unique key in another table, creating a relationship between the two tables.

Run ALTER TABLE table_name DROP CONSTRAINT constraint_name. You must know the constraint name, which you can find in sys.key_constraints. Dropping the primary key also removes its supporting unique index and any foreign key that references it.

An IDENTITY column that auto-generates numbers is a popular surrogate primary key because it stays narrow, stable, and never NULL. Choose a natural key instead only when an existing column, such as a product code, is already guaranteed unique.

Yes. GitHub Copilot can write CREATE TABLE statements and PRIMARY KEY constraints from natural-language prompts and suggest suitable key columns. Always review the generated keys, data types, and nullability, then test the script before running it in production.

AI and machine learning tools analyze sample data and query patterns to recommend which column or columns should be the primary key, flag candidate keys that contain duplicates or NULLs, and suggest surrogate keys. The developer confirms each recommendation before applying it.

Summarize this post with: