SQL Server  

SQL Server Basics: Tables, Primary Keys, Foreign Keys — Explained Simply

Introduction

Every application, whether a small school system or a large-scale enterprise product, stores data somewhere. Most business applications rely on SQL Server because it provides structure, consistency, performance, and reliability.

Before writing advanced queries or optimizing performance, every developer must clearly understand the fundamentals:

  • What are tables?

  • Why do we need primary keys?

  • How do foreign keys maintain relationships?

  • What is data normalization and why does it matter?

This article explains these concepts in a practical, beginner-to-intermediate format using simple examples and real-world scenarios. The language is easy to understand, yet the explanations are accurate enough for production concepts.

Real-World Context

Imagine we are building an application for a car service company. We need to store:

  • Customer details

  • Cars owned by each customer

  • Service records

  • Billing and history

This is where SQL Server helps by storing data in a structured and relational form.

What Is a Table?

A table is a structured container that stores data in rows and columns, similar to an Excel sheet but with strict rules.

Example Table: Customer

CustomerIdFullNamePhoneNumberCity
1Rohan Sharma9876543210Mumbai
2Priya Patel8877665544Ahmedabad
3Vijay Kumar9988221100Bengaluru

Tables represent entities, meaning things we store data about.

What Is a Primary Key?

A Primary Key (PK) uniquely identifies each row. Without it:

  • Data becomes unmanageable.

  • You cannot correctly reference the record.

  • Duplicate data may appear.

In the example above, CustomerId is the Primary Key.

Key Properties of a Primary Key

PropertyMeaning
Must be uniqueTwo rows cannot have the same key
Cannot be NULLEvery record must have a key
Usually numericAuto-increment values (IDENTITY) are common

Example: Creating a Table with Primary Key

CREATE TABLE Customer (
    CustomerId INT IDENTITY(1,1) PRIMARY KEY,
    FullName NVARCHAR(100) NOT NULL,
    PhoneNumber VARCHAR(15),
    City NVARCHAR(50)
);

IDENTITY(1,1) tells SQL Server to auto-generate IDs starting from 1.

What Is a Foreign Key?

A Foreign Key (FK) links one table to another. It enforces a relationship and prevents invalid data.

Example: A customer can have multiple cars.

Table: Car

CarIdCustomerIdBrandModel
11HondaCity
21MarutiSwift
32TataNexon

Here:

  • CustomerId in Car table is a Foreign Key referencing Customer table.

Creating a Table with Foreign Key

CREATE TABLE Car (
    CarId INT IDENTITY(1,1) PRIMARY KEY,
    CustomerId INT NOT NULL,
    Brand NVARCHAR(50),
    Model NVARCHAR(50),
    FOREIGN KEY (CustomerId) REFERENCES Customer(CustomerId)
);

This prevents inserting a car without a valid customer record.

Why Foreign Keys Matter

Without foreign keys:

  • You could delete a customer while their cars still exist. This creates orphan data.

  • Data becomes unreliable.

Foreign keys preserve referential integrity, meaning relationships between tables remain valid.

Database Normalization

Normalization ensures data is structured logically and without unnecessary duplication.

Example of poor design

FullNamePhoneCityCarModelServiceDate

Problems

  • Customer data repeats for every service.

  • Updating details requires modifying multiple rows.

  • Data becomes inconsistent.

Better

| Customer | Car | ServiceRecord |

Each with proper keys and relationships.

Example Relationship Diagram

Customer (PK: CustomerId)
        |
        | 1-to-many
        |
Car (PK: CarId, FK: CustomerId)
        |
        | 1-to-many
        |
ServiceRecord (PK: ServiceId, FK: CarId)

This models real-world structure cleanly.

Common Mistakes Beginners Make

MistakeWhy It Is a Problem
No PK columnCannot uniquely identify records
Using text instead of numeric PKSlow joins and indexing
Missing FK constraintsLeads to orphan data
Storing repeated fieldsViolates normalization

Best Practices

  • Use INT IDENTITY for primary keys.

  • Use meaningless keys instead of natural keys (like phone numbers).

  • Always define foreign key constraints.

  • Use proper data types.

Quick Case Study

A logistics startup initially stored shipment details without relationships. After two years:

  • 40% duplicated customer records

  • Unlinked order entries

  • Inconsistent contact details

After restructuring using proper PK and FK rules:

  • Query performance improved

  • Duplicate data removed

  • Reporting accuracy increased from 70 percent to 99 percent

Summary

Understanding tables, primary keys, and foreign keys is the foundation of SQL Server development. These concepts ensure:

  • Data integrity

  • Accurate relationships

  • Scalable design

  • Reliable business reporting

Before building complex reports, stored procedures, or APIs, ensure your database schema follows these basic principles.