πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Database Concepts

SQL FundamentalsDatabase Basics🟒 Free Lesson

Advertisement

SQL Fundamentals

Database Concepts

Relational databases store data in structured tables connected through relationships. Understanding these core concepts is essential for effective database design.

  • Tables β€” Organized collections of related data with rows and columns
  • Keys β€” Primary and foreign keys that uniquely identify and link records
  • Constraints β€” Rules that enforce data integrity and prevent invalid data

Good database design is the foundation of reliable applications.

What is a Database?

DfRelational Database

An organized collection of structured data stored in tables (relations) that are connected through defined relationships. Data is accessed and managed using SQL, with relationships enforced through keys and constraints.

Think of a database as a digital filing system β€” each table is a folder, each row is a document, and each column is a field on that document.

Database Architecture

Client LayerApplicationSQL DriverDatabase ServerQuery ParserQuery OptimizerExecution EngineTransaction ManagerStorage LayerBuffer CacheStorage EngineData FilesIndex FilesLog FilesDatabase

Tables: The Foundation

DfTable

A two-dimensional data structure organized into rows (records) and columns (fields). Each column has a defined data type, and each row represents a single entity instance.

Architecture Diagram
Table: employees
+----+-------------+-----------+--------+-------------+
| id | first_name  | last_name | salary | department  |
+----+-------------+-----------+--------+-------------+
| 1  | Alice       | Johnson   | 75000  | Engineering |
| 2  | Bob         | Smith     | 62000  | Marketing   |
| 3  | Carol       | Williams  | 85000  | Engineering |
+----+-------------+-----------+--------+-------------+
TermMeaningExample
TableCollection of related dataemployees
Row / RecordA single entryAlice Johnson's data
Column / FieldA single attributesalary
CellIntersection of row and column75000

Primary Keys

DfPrimary Key

A column or set of columns that uniquely identifies each row in a table. Every table should have a primary key to ensure each record can be uniquely referenced. Primary keys cannot contain NULL values.

-- Single column primary key (most common)
CREATE TABLE customers (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
);

-- Auto-incrementing primary key
CREATE TABLE customers (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,  -- MySQL
    -- id SERIAL PRIMARY KEY,               -- PostgreSQL
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
);

Primary Key Rules

RuleDescriptionExample
UniquenessEvery value must be differentTwo customers can't have id=1
NOT NULLCannot be emptyEvery row must have an id
ImmutabilityShould never changeDon't update primary keys
SimplicityUse simple typesINTEGER or UUID preferred

Foreign Keys

DfForeign Key

A column that creates a link between two tables by referencing the primary key of another table. Foreign keys enforce referential integrity, ensuring that relationships between tables remain consistent.

-- Create parent table first
CREATE TABLE customers (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
);

-- Create child table with foreign key
CREATE TABLE orders (
    id INTEGER PRIMARY KEY,
    customer_id INTEGER,
    order_date DATE DEFAULT CURRENT_DATE,
    total DECIMAL(10,2),
    
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Foreign Key Actions

ON DELETE CASCADEDelete ParentDelete All ChildrenData RemovedON DELETE SET NULLDelete ParentSet FK to NULLChildren StayON DELETE RESTRICTDelete ParentChildren Exist?YesNoBlockedAllowed
ActionWhat Happens When Parent is DeletedUse Case
ON DELETE CASCADEChild rows are also deletedOrder items when order deleted
ON DELETE SET NULLForeign key is set to NULLOptional relationships
ON DELETE RESTRICTPrevents deletion if children existCritical relationships
-- Example with multiple foreign key actions
CREATE TABLE order_items (
    id INTEGER PRIMARY KEY,
    order_id INTEGER NOT NULL,
    product_id INTEGER NOT NULL,
    quantity INTEGER DEFAULT 1,
    
    FOREIGN KEY (order_id) REFERENCES orders(id)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    
    FOREIGN KEY (product_id) REFERENCES products(id)
        ON DELETE RESTRICT
        ON UPDATE CASCADE
);

Choose foreign key actions carefully. CASCADE can delete large amounts of data unintentionally. Always consider the business impact before setting ON DELETE CASCADE.

Entity Relationships

CUSTOMERSint id PKstring namestring emailORDERSint id PKint customer_id FKdate order_datedecimal totalORDER_ITEMSint order_id PK,FKint product_id PK,FKint quantityPRODUCTSint id PKstring namedecimal price1 β€” N1 β€” N1 β€” Nplacescontainsordered in||β€”o{ One-to-Many ||β€”|{ One-to-Required-Many
RelationshipDescriptionExample
One-to-OneOne record in A maps to one in BUser ↔ Profile
One-to-ManyOne record in A maps to many in BCustomer β†’ Orders
Many-to-ManyMany records in A map to many in BStudents ↔ Courses

Constraints Deep Dive

DfConstraints

Rules applied to columns that enforce data integrity. Constraints ensure that data follows business rules and prevent invalid data from being inserted or modified.

CREATE TABLE products (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    sku VARCHAR(10) UNIQUE NOT NULL,
    price DECIMAL(10,2) CHECK (price > 0),
    stock INTEGER DEFAULT 0,
    category TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT uq_product_category UNIQUE (name, category)
);
ConstraintDescriptionPerformance Impact
NOT NULLColumn cannot have NULL valueMinimal
UNIQUEAll values must be differentCreates index
PRIMARY KEYUnique identifier for each rowCreates clustered index
FOREIGN KEYLinks to another table's primary keyIndex recommended
CHECKEnsures values meet a conditionEvaluated on INSERT/UPDATE
DEFAULTSets default value if none providedNo impact

Indexes: Performance Optimization

DfIndex

A data structure that improves the speed of data retrieval operations. Indexes work similarly to a book's index β€” they allow the database to find data without scanning every row.

Without IndexQueryFull Table ScanO(n)RowWith IndexQueryIndex LookupO(log n)Row
-- Create index on frequently queried columns
CREATE INDEX idx_customers_email ON customers(email);

-- Create composite index (for multi-column queries)
CREATE INDEX idx_orders_customer_date 
ON orders(customer_id, order_date);

-- Unique index (enforces uniqueness + performance)
CREATE UNIQUE INDEX idx_customers_email_unique 
ON customers(email);

Database Normalization

DfNormalization

The process of organizing data to reduce redundancy and improve data integrity. Normalization involves decomposing tables into smaller, well-structured tables linked by relationships.

Normal Forms

Normal FormRulePurpose
1NFEach column contains atomic values; no repeating groupsEliminate redundancy
2NFAlready in 1NF; no partial dependencies on composite PKEliminate partial redundancy
3NFAlready in 2NF; no transitive dependenciesEliminate indirect redundancy
-- BAD: Violates 1NF (repeating group)
CREATE TABLE orders_bad (
    id INTEGER PRIMARY KEY,
    customer_name TEXT,
    products TEXT  -- "Laptop,Mouse,Keyboard" - multiple values!
);

-- GOOD: Follows 1NF (atomic values)
CREATE TABLE orders_good (
    id INTEGER PRIMARY KEY,
    customer_name TEXT
);

CREATE TABLE order_items (
    order_id INTEGER,
    product_name TEXT,
    quantity INTEGER,
    PRIMARY KEY (order_id, product_name)
);

Naming Conventions

Use consistent naming conventions throughout your database. This makes your schema easier to understand and maintain. Most teams use snake_case for table and column names.

-- GOOD naming conventions
CREATE TABLE customer_orders (
    id INTEGER PRIMARY KEY,
    customer_id INTEGER,
    order_date TIMESTAMP,
    total_amount DECIMAL(10,2)
);

-- BAD naming conventions
CREATE TABLE CustomerOrders (     -- CamelCase
    ID INTEGER PRIMARY KEY,       -- ALL CAPS
    CustID INTEGER,               -- Inconsistent abbreviations
    OrderDate TIMESTAMP,
    Total DECIMAL(10,2)           -- Ambiguous name
);

Summary

Key Takeaways

  1. Databases store data in tables with rows and columns
  2. Primary keys uniquely identify each row; foreign keys link tables
  3. Constraints enforce data integrity and prevent invalid data
  4. Indexes dramatically improve query performance
  5. Normalization reduces redundancy and improves data quality
⭐

Premium Content

Database Concepts

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement