Paramclasses Academy - Blog

Tutorials Details

←
Post 24 of 40
→

How to Design Tables and Relationships in SQL Server: Real-World Database Design with Practical Examples

How to Design Tables and Relationships in SQL Server: Real-World Database Design with Practical Examples

November 18, 2026

 

????️ How to Design Tables and Relationships for a Real-World Database

???? Introduction

Designing a database is one of the most important steps in developing a reliable and scalable application. Whether you are building an E-Commerce Website, Student Management System, Hospital Management System, Banking Application, or Inventory Management System, a well-structured database helps store, manage, and retrieve information efficiently.

In Microsoft SQL Server, database design involves identifying the required data, creating appropriate tables, selecting suitable data types, defining Primary Keys and Foreign Keys, and establishing relationships between tables.

In this tutorial, we will learn how to design a real-world database step by step using a practical E-Commerce Database Design example.

By the end of this guide, you will understand how multiple tables work together and how to implement their relationships using SQL Server.


???? What You Will Learn

After completing this tutorial, you will understand:

  • What database design is and why it matters.

  • How to identify entities and their attributes.

  • How to create tables for a real-world application.

  • How to select appropriate SQL Server data types.

  • How to define Primary Keys and Foreign Keys.

  • How to create One-to-One, One-to-Many, and Many-to-Many relationships.

  • How to use junction tables.

  • How to create relationships using SQL constraints.

  • How to retrieve related data using JOIN queries.

  • Database design best practices.


???? 1. What Is Database Design?

Database Design is the process of organizing information into tables, columns, keys, and relationships so that data can be stored and managed efficiently.

A good database design helps avoid duplicate information, maintain data accuracy, simplify queries, and support future application growth.

Example

Imagine you are developing an E-Commerce website.

The application needs to store:

  • Customer information

  • Product information

  • Product categories

  • Customer orders

  • Order details

  • Payment information

Instead of storing all this information in one large table, we divide it into multiple related tables.

This approach makes the database easier to maintain and reduces unnecessary data duplication.


???? 2. Understand the Application Requirements

Before creating database tables, understand what information the application needs to store and how different records are connected.

For our E-Commerce application, consider these requirements:

Requirement Description
Customers Store customer details
Products Store product information
Categories Organize products into categories
Orders Store customer orders
Order Items Store products included in each order
Product Categories Connect products with categories

???? Important

Do not start creating tables immediately. First, identify the main entities, their attributes, and the relationships between them.


???? 3. Identify Entities and Attributes

An Entity represents a real-world object or concept about which we need to store information.

An Attribute represents a property or field of that entity.

For our E-Commerce database:

Entity Important Attributes
Customers CustomerID, FullName, Email, Phone
Products ProductID, ProductName, Price, Stock
Categories CategoryID, CategoryName
Orders OrderID, CustomerID, OrderDate, Status
OrderItems OrderItemID, OrderID, ProductID, Quantity
ProductCategories ProductID, CategoryID

Each entity will generally become a separate database table.


???? 4. Choose Appropriate SQL Server Data Types

Selecting suitable data types is an important part of database design.

SQL Server provides different data types for numbers, text, dates, and other values.

Data Type Purpose Example
INT Whole numbers CustomerID
BIGINT Large whole numbers Large transaction IDs
VARCHAR(100) Variable-length non-Unicode text Email
NVARCHAR(100) Variable-length Unicode text Customer Name
DECIMAL(10,2) Exact decimal values Product Price
DATE Date without time Birth Date
DATETIME2 Date and time Order Date
BIT Boolean-style value IsActive

???? Best Practice

  • Use INT IDENTITY for automatically generated numeric IDs when appropriate.

  • Use NVARCHAR when storing multilingual text.

  • Use DECIMAL for prices and financial values instead of FLOAT.

  • Use suitable date and time data types.

  • Avoid unnecessarily large data types.


???? 5. Understand Database Relationships

Relationships define how records in one table are associated with records in another table.

There are three commonly used relationship types.

A. One-to-One Relationship (1:1)

A One-to-One relationship means one record in the first table is associated with at most one record in the second table, and vice versa.

Example:

One customer can have one customer profile record.

Possible tables:

  • Customers

  • CustomerProfiles

The CustomerID in CustomerProfiles can be both its Primary Key and a Foreign Key referencing Customers.

B. One-to-Many Relationship (1:N)

A One-to-Many relationship means one record in the parent table can be associated with multiple records in the child table.

Example:

One customer can place multiple orders.

  • One Customer → Multiple Orders

  • One Order → Multiple Order Items

  • One Category → Multiple Products, when each product belongs to one category

The Foreign Key is placed in the child table.

C. Many-to-Many Relationship (M:N)

A Many-to-Many relationship means multiple records in one table can be associated with multiple records in another table.

Example:

  • One product can belong to multiple categories.

  • One category can contain multiple products.

To implement this relationship in a relational database, we create a Junction Table.

For our example, the junction table is ProductCategories.

ProductID CategoryID
1 1
1 2
2 1
3 3

The combination of ProductID and CategoryID identifies each product-category association.


???? 6. Plan the E-Commerce Database Structure

We will create the following six tables:

  1. Customers

  2. Categories

  3. Products

  4. ProductCategories

  5. Orders

  6. OrderItems

???? Table Relationship Overview

Parent Table Child Table Relationship
Customers Orders One-to-Many
Orders OrderItems One-to-Many
Products OrderItems One-to-Many
Products ProductCategories One-to-Many
Categories ProductCategories One-to-Many

The ProductCategories table connects Products and Categories, creating a Many-to-Many relationship between them.


???? 7. Create the E-Commerce Database

First, create a new database in SQL Server.

SQL Query:

CREATE DATABASE EcommerceDB;
GO

USE EcommerceDB;
GO

The EcommerceDB database will store all tables and related information for our application.


???? 8. Create the Customers Table

The Customers table stores customer information.

SQL Query:

CREATE TABLE Customers
(
    CustomerID INT IDENTITY(1,1) PRIMARY KEY,
    FullName NVARCHAR(100) NOT NULL,
    Email VARCHAR(150) NOT NULL UNIQUE,
    Phone VARCHAR(15),
    CreatedAt DATETIME2 DEFAULT SYSDATETIME()
);

Explanation

  • CustomerID: Unique identifier for each customer.

  • FullName: Stores the customer's name.

  • Email: Stores a unique email address.

  • Phone: Stores the customer's contact number.

  • CreatedAt: Stores the record creation date and time.

The CustomerID is the Primary Key of this table.


???? 9. Create the Categories Table

The Categories table stores product category information.

SQL Query:

CREATE TABLE Categories
(
    CategoryID INT IDENTITY(1,1) PRIMARY KEY,
    CategoryName NVARCHAR(100) NOT NULL UNIQUE
);

Example Categories

  • Electronics

  • Clothing

  • Books

  • Accessories


???? 10. Create the Products Table

The Products table stores product details.

SQL Query:

CREATE TABLE Products
(
    ProductID INT IDENTITY(1,1) PRIMARY KEY,
    ProductName NVARCHAR(150) NOT NULL,
    Price DECIMAL(10,2) NOT NULL,
    Stock INT NOT NULL DEFAULT 0
);

Explanation

  • ProductID: Unique product identifier.

  • ProductName: Product name.

  • Price: Product selling price.

  • Stock: Available product quantity.

The Price and Stock columns can also be protected using CHECK constraints.


???? 11. Create the ProductCategories Junction Table

A product can belong to multiple categories, and a category can contain multiple products.

We use the ProductCategories table to implement this Many-to-Many relationship.

SQL Query:

CREATE TABLE ProductCategories
(
    ProductID INT NOT NULL,
    CategoryID INT NOT NULL,

    CONSTRAINT PK_ProductCategories
    PRIMARY KEY (ProductID, CategoryID),

    CONSTRAINT FK_ProductCategories_Products
    FOREIGN KEY (ProductID)
    REFERENCES Products(ProductID),

    CONSTRAINT FK_ProductCategories_Categories
    FOREIGN KEY (CategoryID)
    REFERENCES Categories(CategoryID)
);

???? Important Concept

This table uses a Composite Primary Key consisting of ProductID and CategoryID.

This prevents the same product from being assigned to the same category more than once.


???? 12. Create the Orders Table

The Orders table stores customer order information.

SQL Query:

CREATE TABLE Orders
(
    OrderID INT IDENTITY(1,1) PRIMARY KEY,
    CustomerID INT NOT NULL,
    OrderDate DATETIME2 DEFAULT SYSDATETIME(),
    Status VARCHAR(30) NOT NULL DEFAULT 'Pending',

    CONSTRAINT FK_Orders_Customers
    FOREIGN KEY (CustomerID)
    REFERENCES Customers(CustomerID)
);

Explanation

  • OrderID: Unique order identifier.

  • CustomerID: Identifies the customer who placed the order.

  • OrderDate: Stores the order date and time.

  • Status: Stores the current order status.

The CustomerID column is a Foreign Key referencing the Customers table.

Therefore, an order cannot reference a non-existing customer.


???? 13. Create the OrderItems Table

The OrderItems table stores the products included in each order.

One order can contain multiple products.

SQL Query:

CREATE TABLE OrderItems
(
    OrderItemID INT IDENTITY(1,1) PRIMARY KEY,
    OrderID INT NOT NULL,
    ProductID INT NOT NULL,
    Quantity INT NOT NULL,
    UnitPrice DECIMAL(10,2) NOT NULL,

    CONSTRAINT FK_OrderItems_Orders
    FOREIGN KEY (OrderID)
    REFERENCES Orders(OrderID),

    CONSTRAINT FK_OrderItems_Products
    FOREIGN KEY (ProductID)
    REFERENCES Products(ProductID),

    CONSTRAINT CK_OrderItems_Quantity
    CHECK (Quantity > 0),

    CONSTRAINT CK_OrderItems_UnitPrice
    CHECK (UnitPrice >= 0)
);

Explanation

  • OrderItemID: Unique order-item identifier.

  • OrderID: Identifies the related order.

  • ProductID: Identifies the ordered product.

  • Quantity: Number of units ordered.

  • UnitPrice: Product price recorded for this order item.

Why store UnitPrice in OrderItems?

A product's current price may change later. Storing the price at the time of the order helps preserve the historical order amount.


???? 14. Insert Sample Data into the Tables

Now let's insert sample records into our database.

Insert Customers

INSERT INTO Customers (FullName, Email, Phone)
VALUES
('Rahul Sharma', 'rahul@example.com', '9876543210'),
('Priya Verma', 'priya@example.com', '9876543211'),
('Amit Kumar', 'amit@example.com', '9876543212');

Insert Categories

INSERT INTO Categories (CategoryName)
VALUES
('Electronics'),
('Accessories'),
('Books');

Insert Products

INSERT INTO Products (ProductName, Price, Stock)
VALUES
('Laptop', 55000.00, 10),
('Wireless Mouse', 800.00, 50),
('SQL Server Book', 650.00, 25);

Insert Product Categories

INSERT INTO ProductCategories (ProductID, CategoryID)
VALUES
(1, 1),
(1, 2),
(2, 2),
(3, 3);

Insert Orders

INSERT INTO Orders (CustomerID, Status)
VALUES
(1, 'Pending'),
(2, 'Completed'),
(1, 'Processing');

Insert Order Items

INSERT INTO OrderItems
(OrderID, ProductID, Quantity, UnitPrice)
VALUES
(1, 1, 1, 55000.00),
(1, 2, 2, 800.00),
(2, 3, 1, 650.00),
(3, 2, 1, 800.00);

Now our database contains sample customers, categories, products, orders, and order details.


???? 15. Retrieve Data Using SQL JOIN

After creating related tables, we can retrieve information from multiple tables using JOIN statements.

A. Display All Customers and Their Orders

SELECT
    C.CustomerID,
    C.FullName,
    O.OrderID,
    O.OrderDate,
    O.Status
FROM Customers C
INNER JOIN Orders O
ON C.CustomerID = O.CustomerID;

Explanation: This query combines customer information with their corresponding orders.

B. Display Order Details with Product Names

SELECT
    O.OrderID,
    C.FullName,
    P.ProductName,
    OI.Quantity,
    OI.UnitPrice,
    OI.Quantity * OI.UnitPrice AS TotalAmount
FROM Orders O
INNER JOIN Customers C
ON O.CustomerID = C.CustomerID
INNER JOIN OrderItems OI
ON O.OrderID = OI.OrderID
INNER JOIN Products P
ON OI.ProductID = P.ProductID;

Explanation: This query retrieves the customer name, order ID, product name, quantity, unit price, and calculated line amount.

C. Display Products with Their Categories

SELECT
    P.ProductID,
    P.ProductName,
    C.CategoryName
FROM Products P
INNER JOIN ProductCategories PC
ON P.ProductID = PC.ProductID
INNER JOIN Categories C
ON PC.CategoryID = C.CategoryID;

Explanation: This query displays products along with all their associated categories.


???? 16. Understand the Difference Between Primary Key and Foreign Key

Primary Key Foreign Key
Uniquely identifies a row Connects related tables
Must contain unique values Values must match a referenced key or be NULL when allowed
Cannot contain NULL values Can contain NULL if the column allows it
One Primary Key constraint per table A table can have multiple Foreign Keys

Example:

CustomerID is the Primary Key in Customers and a Foreign Key in Orders.

This relationship connects each order to its customer.


???? 17. Database Design Best Practices

Follow these important practices when designing a real-world SQL Server database.

✅ 1. Use Meaningful Table Names

Use clear names such as Customers, Products, Orders, and Categories.

Avoid unclear names such as T1, T2, or DataTable.

✅ 2. Define Primary Keys

Every table should have a suitable key that uniquely identifies its records.

✅ 3. Define Foreign Key Relationships

Use Foreign Keys to maintain referential integrity between related tables.

✅ 4. Avoid Duplicate Data

Separate entities into appropriate tables instead of repeatedly storing the same information.

✅ 5. Apply Normalization

Use normalization principles to reduce unnecessary duplication and update anomalies.

✅ 6. Choose Suitable Data Types

Select data types according to the actual values being stored.

✅ 7. Add Appropriate Constraints

Use NOT NULL, UNIQUE, CHECK, DEFAULT, PRIMARY KEY, and FOREIGN KEY constraints wherever required.

✅ 8. Create Indexes Carefully

Indexes on frequently searched or joined columns can improve query performance. However, unnecessary indexes can increase storage and write overhead.

✅ 9. Plan for Future Requirements

Consider possible future features such as payments, shipping, returns, and customer addresses before finalizing the design.

✅ 10. Test the Database

Insert sample records and execute different queries to verify that the structure works as expected.


???? 18. Common Database Design Mistakes

Mistake Why It Is a Problem
Storing everything in one table Causes duplication and maintenance problems
Not defining Primary Keys Makes records difficult to identify uniquely
Ignoring Foreign Keys Can allow invalid relationships
Using incorrect data types Can cause inaccurate storage or unnecessary overhead
Repeating category names in product records Creates unnecessary duplication
Not using junction tables Makes Many-to-Many relationships difficult to manage
Creating too many unnecessary columns Makes the design harder to maintain

???? 19. Frequently Asked Questions (FAQs)

Q1. What is database design in SQL Server?

Database design is the process of organizing data into tables, columns, keys, constraints, and relationships to store and manage information efficiently.

Q2. What is an Entity in database design?

An Entity represents a real-world object or concept, such as a Customer, Product, Employee, or Order.

Q3. What is the difference between One-to-One and One-to-Many relationships?

In a One-to-One relationship, one record is associated with at most one record in another table. In a One-to-Many relationship, one parent record can be associated with multiple child records.

Q4. How is a Many-to-Many relationship implemented in SQL Server?

A Many-to-Many relationship is generally implemented using a junction table containing Foreign Keys that reference the two related tables.

Q5. Why are Foreign Keys important?

Foreign Keys help maintain referential integrity by ensuring that referenced records exist in the related table.

Q6. What is a Composite Primary Key?

A Composite Primary Key is a key made up of two or more columns that together uniquely identify a record.

Q7. Why should we normalize a database?

Normalization helps organize data, reduce unnecessary duplication, and prevent common insertion, update, and deletion anomalies.

Q8. Can SQL Server create database relationships through SSMS?

Yes. SQL Server Management Studio provides Table Designer and Database Diagram tools for creating and visualizing tables and relationships.

Q9. What is a Junction Table?

A Junction Table is an intermediate table used to implement a Many-to-Many relationship between two tables.

Q10. Which tables are commonly required in an E-Commerce database?

Common tables include Customers, Products, Categories, Orders, OrderItems, Payments, and Shipping.


???? Conclusion

Designing tables and relationships is a fundamental skill for every database developer. A properly designed database helps maintain data accuracy, reduce duplication, improve data management, and support application growth.

In this tutorial, we learned how to design a practical E-Commerce Database in Microsoft SQL Server using Customers, Products, Categories, ProductCategories, Orders, and OrderItems tables. We also implemented Primary Keys, Foreign Keys, One-to-Many relationships, Many-to-Many relationships, and practical JOIN queries.

Learning database design is an important step toward becoming a professional SQL Server developer. At Param Classes, students can strengthen their database development skills through practical SQL Server tutorials, real-world examples, and hands-on projects.

Keep practicing, build real-world database projects, and continue learning with Param Classes!

←
Post 24 of 40
→