热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

Chapter 6

Joins, Relationships, Constraints, Normalization, and Data Modeling

Learn how SQL Server tables relate to each other and how good relational design reduces duplication and inconsistency.

Inside this chapter

  1. Why Relationships Matter
  2. Primary Keys and Foreign Keys
  3. Joining Tables for Useful Results
  4. Normalization and Tradeoffs

Series navigation

Study the chapters in sequence for the smoothest path from SQL Server basics to advanced T-SQL, performance, and production operations. Use the navigation at the bottom of each page to move through the full tutorial series.

Tutorial Home

Chapter 6

Why Relationships Matter

Relational databases become powerful when tables can describe meaningful relationships. Customers place orders. Orders contain order items. Employees belong to departments. Students register for courses. Instead of copying the same text into many rows, SQL Server lets you store data once and connect it through keys.

Chapter 6

Primary Keys and Foreign Keys

CREATE TABLE dbo.OrderItems (
    OrderItemId BIGINT IDENTITY(1,1) PRIMARY KEY,
    OrderId BIGINT NOT NULL,
    ProductId BIGINT NOT NULL,
    Quantity INT NOT NULL,
    UnitPrice DECIMAL(10,2) NOT NULL,
    CONSTRAINT FK_OrderItems_Orders
        FOREIGN KEY (OrderId) REFERENCES dbo.Orders(OrderId),
    CONSTRAINT FK_OrderItems_Products
        FOREIGN KEY (ProductId) REFERENCES dbo.Products(ProductId)
);

Foreign keys enforce referential integrity so related records cannot silently break.

Chapter 6

Joining Tables for Useful Results

SELECT
    o.OrderId,
    c.FullName,
    o.OrderStatus,
    o.OrderDate
FROM dbo.Orders o
JOIN dbo.Customers c ON c.CustomerId = o.CustomerId
WHERE o.OrderStatus = 'PENDING';

Joins are used constantly in reports, admin tools, analytics, and application services. Deep join understanding is one of the clearest signs that a student has moved beyond basic SQL.

Chapter 6

Normalization and Tradeoffs

Normalization helps keep data consistent by reducing unnecessary duplication. Customer contact details belong in the customer table, not copied into each order row. At the same time, some duplication can be intentional, such as storing item price in an order line to preserve historical billing accuracy. Advanced design means understanding those tradeoffs rather than blindly following rules.

版权所有 © 2026,WithoutBook。