LESSON 7 OF 14

Fundamental Database Design and Normalization

This lesson explains how to model a business problem as entities, attributes, and relationships, reduce duplicated data, and apply the basic principles of 1NF, 2NF, and 3NF in SQL Server.

55 dk 134

Introduction

A database is not simply a random collection of tables and columns. A well-designed database represents business rules clearly, reduces unnecessary duplication, and helps keep information consistent. In this lesson, the table and data type concepts from previous lessons are combined in a practical design problem.

Our example is a simple online sales system. The system must store customers, products, orders, and order items. We will first analyze the business problem, identify entities and relationships, and then apply the principles of 1NF, 2NF, and 3NF to create SQL Server tables.

Normalization does not mean splitting every possible value into the maximum number of tables. Its purpose is to reduce unnecessary repetition and insert, update, and delete anomalies while preserving data integrity.

1. Fundamentals of Modeling a Business Problem

Entities and attributes

When analyzing a business problem, begin by identifying the entities about which the system must store information. An entity is a real-world object, person, event, or concept. In our example, the main entities are Customer, Product, Order, and Order Item.

The properties of an entity are called attributes. A customer may have a customer number, name, and email address. A product may have a product number, name, and current price. Each attribute will eventually correspond to a table column.

The role of keys

A primary key is a column or group of columns that uniquely identifies a row in a table. For example, CustomerID identifies a customer, ProductID identifies a product, and OrderID identifies an order. Because an order can contain multiple products, the table representing the relationship between orders and products can use a single key named OrderItemID.

Relationships between tables can be enforced with foreign keys. Orders.CustomerID references Customers.CustomerID, while OrderItems.OrderID references Orders.OrderID. In SQL Server, primary key and foreign key constraints support data integrity; a foreign key prevents a child row from referencing a value that does not exist in the related parent table.

Thinking about cardinality

  • One customer can place zero or many orders.
  • Each order belongs to one customer.
  • One order can contain one or more order items.
  • One product can appear in many different orders.

Therefore, customer-to-order and order-to-order-item relationships are one-to-many relationships. Orders and products have a many-to-many relationship, which is resolved by an associative table such as OrderItems.

2. Why Is Normalization Necessary?

At first, storing all information in one table may appear convenient:

OrderIDOrderDateCustomerNameCustomerEmailProductNameUnitPriceQuantity
10012026-09-09Ayşe Demirayse@example.comKeyboard750.002
10012026-09-09Ayşe Demirayse@example.comMouse400.001

In this design, the same order and customer information is repeated for every order item. If the customer's email address changes, all related rows must be updated. It is also difficult to store a customer who has not placed an order yet. These problems are commonly described as update, insert, and delete anomalies.

3. First Normal Form: 1NF

A table is in first normal form when every cell contains one indivisible value, repeating column groups are not used, and each row can be uniquely identified.

For example, columns named Product1, Product2, and Product3 are a poor design. When the number of products increases, additional columns are required. Instead, each order-product combination should be represented as a separate row. An order containing two products has two order-item rows.

Applying 1NF makes values atomic, but the table may still contain too much repeated information. Whether first and last names should be stored separately depends on the requirements. Separate columns may be useful when the application must search, sort, or personalize by each part of the name.

4. Second Normal Form: 2NF

A table is in second normal form when it is already in 1NF and every non-key column depends on the whole primary key. This rule is especially important when a composite primary key is used.

Assume the following design uses OrderID and ProductID as a composite key:

OrderID, ProductID, OrderDate, CustomerID, ProductName, UnitPrice, Quantity

OrderDate and CustomerID depend only on OrderID. ProductName and the current UnitPrice depend only on ProductID. Therefore, these columns do not depend on the complete composite key and create partial dependencies.

The solution is to separate the information into three logical areas:

  • Orders: Order-level date and customer information.
  • Products: Product name and current price.
  • OrderItems: The quantity and sale price that connect an order with a product.

It is important to retain the price at the time of the order in OrderItems.UnitPrice. Even if the product's current price changes later, the total of a historical order must remain unchanged.

5. Third Normal Form: 3NF

A table is in third normal form when it is in 2NF and non-key columns do not depend on other non-key columns. They should depend directly on the key. This problem is called a transitive dependency.

For example, if Orders contains CustomerID, CustomerName, and CustomerEmail, customer data is repeated in every order. CustomerName and CustomerEmail depend on the customer, not on the order. Therefore, customer attributes belong in a separate Customers table, while Orders should retain only CustomerID.

Similarly, if products belong to categories and the category name is repeated in the product table, a separate category table may be appropriate. However, do not create a separate table for every small concept without considering real business dependencies.

6. Creating the Normalized Structure in SQL Server

The following example can be executed in the training database created in an earlier lesson. The creation order matters: create referenced parent tables before tables that contain foreign keys.

USE TrainingDB;
GO

CREATE TABLE dbo.Customers
(
    CustomerID INT IDENTITY(1,1) NOT NULL,
    CustomerName NVARCHAR(100) NOT NULL,
    CustomerEmail NVARCHAR(255) NOT NULL,
    CONSTRAINT PK_Customers PRIMARY KEY (CustomerID),
    CONSTRAINT UQ_Customers_Email UNIQUE (CustomerEmail)
);
GO

CREATE TABLE dbo.Products
(
    ProductID INT IDENTITY(1,1) NOT NULL,
    ProductName NVARCHAR(150) NOT NULL,
    CurrentPrice DECIMAL(10,2) NOT NULL,
    CONSTRAINT PK_Products PRIMARY KEY (ProductID),
    CONSTRAINT CK_Products_CurrentPrice CHECK (CurrentPrice >= 0)
);
GO

CREATE TABLE dbo.Orders
(
    OrderID INT IDENTITY(1,1) NOT NULL,
    CustomerID INT NOT NULL,
    OrderDate DATE NOT NULL,
    CONSTRAINT PK_Orders PRIMARY KEY (OrderID),
    CONSTRAINT FK_Orders_Customers
        FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID)
);
GO

CREATE TABLE dbo.OrderItems
(
    OrderItemID INT IDENTITY(1,1) NOT NULL,
    OrderID INT NOT NULL,
    ProductID INT NOT NULL,
    Quantity INT NOT NULL,
    UnitPrice DECIMAL(10,2) NOT NULL,
    CONSTRAINT PK_OrderItems PRIMARY KEY (OrderItemID),
    CONSTRAINT UQ_OrderItems_Order_Product UNIQUE (OrderID, ProductID),
    CONSTRAINT FK_OrderItems_Orders
        FOREIGN KEY (OrderID) REFERENCES dbo.Orders(OrderID),
    CONSTRAINT FK_OrderItems_Products
        FOREIGN KEY (ProductID) REFERENCES dbo.Products(ProductID),
    CONSTRAINT CK_OrderItems_Quantity CHECK (Quantity > 0),
    CONSTRAINT CK_OrderItems_UnitPrice CHECK (UnitPrice >= 0)
);
GO

The UNIQUE constraint prevents the same product from appearing as two separate lines in the same order. If business rules allow the same product to appear multiple times with different prices or delivery details, this constraint should be removed and a different key design should be considered. Design decisions must reflect application requirements.

7. Inserting and Querying Sample Data

INSERT INTO dbo.Customers (CustomerName, CustomerEmail)
VALUES (N'Ayşe Demir', N'ayse@example.com');

INSERT INTO dbo.Products (ProductName, CurrentPrice)
VALUES (N'Keyboard', 750.00),
       (N'Mouse', 400.00);

INSERT INTO dbo.Orders (CustomerID, OrderDate)
VALUES (1, '2026-09-09');

INSERT INTO dbo.OrderItems (OrderID, ProductID, Quantity, UnitPrice)
VALUES (1, 1, 2, 750.00),
       (1, 2, 1, 400.00);

SELECT o.OrderID,
       c.CustomerName,
       o.OrderDate,
       p.ProductName,
       oi.Quantity,
       oi.UnitPrice,
       oi.Quantity * oi.UnitPrice AS LineTotal
FROM dbo.Orders AS o
INNER JOIN dbo.Customers AS c ON c.CustomerID = o.CustomerID
INNER JOIN dbo.OrderItems AS oi ON oi.OrderID = o.OrderID
INNER JOIN dbo.Products AS p ON p.ProductID = oi.ProductID;

This query combines the customer, order, order-item, and product tables through their keys. Although the customer name is stored only in Customers and the product name only in Products, JOIN operations present an integrated result to the user.

8. Common Mistakes

  • Keeping everything in one table: This appears simple at first but causes repetition and inconsistency.
  • Storing comma-separated lists in one cell: An approach such as ProductIDs = '1,4,7' makes querying and integrity enforcement difficult.
  • Omitting foreign keys: Application validation is useful, but integrity should also be enforced at the database level.
  • Confusing historical and current prices: The sale price should be stored separately in the order-item table.
  • Adding unrelated columns to a table: Ask which entity owns each column and which key determines its value.
  • Treating normalization as an absolute rule: Reporting or performance requirements may justify controlled denormalization. Such decisions should be supported by measurements and business requirements.
  • Creating tables in the wrong order: A foreign key should be defined after its referenced table and key exist.

9. Short Practice Exercise

Model the following business problem: A training center enrolls students in courses. Each student has a name and email address, and each course has a name and duration. A student can enroll in many courses, and a course can have many students. The enrollment date and enrollment status must also be stored.

  1. Identify columns for Students, Courses, and a third table that represents the relationship.
  2. Explain why the student-course relationship is many-to-many.
  3. Select a primary key for each table.
  4. Create two foreign keys in the enrollment table.
  5. Add a suitable constraint if email addresses must be unique.
  6. Write a JOIN query that lists the courses in which a student is enrolled.

While testing, try to insert the same student into the same course twice. If the business rule does not allow this, observe how a composite UNIQUE constraint can enforce it.

Conclusion

In this lesson, we analyzed business problems using entities, attributes, and relationships. Starting with a repeated and combined structure, we applied 1NF to make values atomic, 2NF to remove partial dependencies, and 3NF to eliminate transitive dependencies.

A normalized design separates customer, product, order, and order-item information into appropriate tables. Primary key, foreign key, UNIQUE, and CHECK constraints transfer the rules of that design into SQL Server. In the next lessons, we will develop more comprehensive SELECT, filtering, JOIN, and aggregate queries over this structure.

Sources