All three are constraints that protect data integrity, but each has a different purpose. Primary key - Uniquely identifies each row in a table. - Must be unique and cannot contain NULLs. - Only one primary key per table (it can consist of multiple columns: a composite key). - In SQL Server, creates a clustered index by default (unless one already exists or you specify NONCLUSTERED). - Often a surrogate key (IDENTITY integer, sequence or GUID) or a natural key (ISBN, country code). CREATE TABLE Customers ( CustomerId INT IDENTITY(1,1) PRIMARY KEY, Email NVARCHAR(256) NOT NULL, Name NVARCHAR(100) NOT NULL ); Unique key (unique constraint) - Ensures all values in the column or column set are unique. - Used for alternate identifiers that are not the primary key, such as email, username or PAN number. - A table can have many unique constraints. - Allows NULLs, but database behaviour differs: SQL Server allows only one NULL in a unique constraint (NULLs are treated as equal); PostgreSQL and MySQL allow multiple NULLs. In SQL Server, a filtered unique index (WHERE Email IS NOT NULL) allows multiple NULLs. - Creates a non-clustered unique index by default in SQL Server. ALTER TABLE Customers ADD CONSTRAINT UQ_Customers_Email UNIQUE (Email); Foreign key - Creates a relationship between two tables: a column in the child table references the primary key (or a unique key) of the parent table. - Enforces referential integrity: you cannot insert an order for a customer that does not exist, or delete a customer who still has orders (unless cascade rules say otherwise). - Can contain NULLs (meaning "no related row") and duplicates (many orders for one customer). - Referential actions: ON DELETE CASCADE, SET NULL, SET DEFAULT or NO ACTION (default); same for ON UPDATE. - Not indexed automatically in SQL Server — create an index on foreign key columns to speed up joins and deletes on the parent. CREATE TABLE Orders ( OrderId INT IDENTITY PRIMARY KEY, CustomerId INT NOT NULL CONSTRAINT FK_Orders_Customers REFERENCES Customers(CustomerId), OrderDate DATETIME2 NOT NULL ); CREATE INDEX IX_Orders_CustomerId ON Orders(CustomerId); Comparison - Purpose: primary key identifies rows; unique key prevents duplicate values in alternate keys; foreign key links tables. - Count per table: one primary key; many unique keys; many foreign keys. - NULLs: primary key never; unique key allowed (behaviour varies by database); foreign key allowed. - Duplicates: primary and unique keys no; foreign key yes. - Index created automatically: primary key yes (clustered by default in SQL Server); unique key yes; foreign key no. Related terms - Candidate key: any column set that could be the primary key (unique and not null). - Alternate key: a candidate key not chosen as the primary key — enforced with a unique constraint. - Composite key: a key made of several columns, such as (OrderId, ProductId) in OrderItems. - Surrogate vs natural key: system-generated vs business-meaningful identifiers. In Entity Framework Core, these map to HasKey, HasIndex(...).IsUnique() or HasAlternateKey, and relationships configured with HasOne/WithMany and HasForeignKey.
Ravindra Wadile · · 0 likes