MSDN Documentation

SQL Database Engine: Database Concepts

Welcome to the core concepts of the SQL Database Engine. This section provides a foundational understanding of how data is structured, managed, and accessed within a relational database system.

1. Tables and Columns

A table is the fundamental structure for storing data in a relational database. It organizes data into rows and columns, similar to a spreadsheet. Each column represents a specific attribute or piece of information, while each row represents a single record or entity.

Example: Customer Table

Consider a Customers table:


CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(100) UNIQUE
);
                

Here, CustomerID is the primary key, ensuring each customer has a unique ID.

2. Data Types

Each column in a table is assigned a specific data type, which defines the kind of data it can store and the operations that can be performed on it. Common data types include:

3. Relationships and Normalization

Relationships between tables are crucial for organizing data efficiently and reducing redundancy. Normalization is a process used to organize data in a database, typically to minimize data duplication and improve data integrity.

Example: Orders and Customers (One-to-Many)

An Orders table might have a CustomerID as a foreign key:


CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    OrderDate DATE,
    CustomerID INT,
    TotalAmount DECIMAL(10, 2),
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
                

This links each order to a specific customer.

4. Views

A view is a virtual table based on the result-set of a SQL statement. It contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

Example: Customer Order Summary View

A view to see customer names and their order counts:


CREATE VIEW CustomerOrderSummary AS
SELECT
    C.FirstName,
    C.LastName,
    COUNT(O.OrderID) AS NumberOfOrders
FROM
    Customers AS C
LEFT JOIN
    Orders AS O ON C.CustomerID = O.CustomerID
GROUP BY
    C.CustomerID, C.FirstName, C.LastName;
                

You can then query this view like a regular table: SELECT * FROM CustomerOrderSummary;

5. Indexes

An index is a data structure that improves the speed of data retrieval operations on a database table. Indexes are created on one or more columns of a table. While they speed up reads, they can slow down writes (inserts, updates, deletes) as the index also needs to be updated.

6. Transactions

A transaction is a sequence of database operations performed as a single logical unit of work. Transactions adhere to the ACID properties:

Example: Transferring Funds

A simple transaction to move money between accounts:


BEGIN TRANSACTION;
    UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 101;
    UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 202;
COMMIT TRANSACTION;