Advertisement
Beginner SQL Server, Constraints (Primary Key, Foreign Key, Check & Unique)

What is a primary key constraint, and why is it important for a database table?

Short answer

A primary key constraint uniquely identifies each row in a table, ensuring no two rows can have the same value in that column, and it also automatically prevents that column from containing NULL values, making it the reliable way other tables can reference a specific row.

Save SQL Server mock interview
Advertisement
AModel answer
A primary key constraint uniquely identifies each row in a table, ensuring no two rows can have the same value in that column, and it also automatically prevents that column from containing NULL values, making it the reliable way other tables can reference a specific row.
CREATE TABLE Customers (
  CustomerId INT PRIMARY KEY,
  Name VARCHAR(100)
);
Real-world example
An online store uses CustomerId as the primary key for its customers table, guaranteeing that every customer record can always be uniquely identified and referenced by other tables like orders.
Likely follow-ups
  • Can a table have more than one primary key column?
  • What happens if you try to insert a duplicate primary key value?
Related topics