SQL Server Interview Question #169

What is a one-to-many relationship?

Database Design, Normalization & Advanced SQL Senior Advanced

Detailed Explanation

A one-to-many relationship means one parent row can have many child rows, while each child row references one parent.

Examples include Category-to-Products, Customer-to-Orders, and PoliceStation-to-CaseReports. It is implemented by storing the parent's key as a foreign key in the child table.

Code Example

CREATE TABLE dbo.Products
(
    Id INT PRIMARY KEY,
    Name NVARCHAR(200) NOT NULL,
    CategoryId INT NOT NULL,
    CONSTRAINT FK_Products_Categories
        FOREIGN KEY (CategoryId)
        REFERENCES dbo.Categories(Id)
);