SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a table.
In simple terms:
A PRIMARY KEY is a column (or combination of columns) whose values must be unique and cannot be NULL.
Each table can have only one PRIMARY KEY.
Example: Orders Table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerName VARCHAR(100),
OrderDate DATE,
Amount DECIMAL(10,2)
);
Example: Composite PRIMARY KEY (Multiple Columns)
Sometimes a primary key is created using multiple columns.
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
Simple Definition
The PRIMARY KEY constraint uniquely identifies each record in a table. It does not allow duplicate values or NULL values and ensures each row can be uniquely identified.