A relational database stores data in tables that contain rows and columns. This structure makes it easy to organize, search, and manage related information.
Each table represents a specific type of data, such as customers, products, or orders.
Table of Contents
Tables, Rows, and Columns
A relational database consists of one or more tables.
- Columns define the attributes or properties of the data.
- Rows contain individual records.
- Each record is uniquely identified by a key, usually called a primary key.
For example, a customer table might contain:
| Customer ID | Name | |
|---|---|---|
| 1001 | John Smith | [email protected] |
| 1002 | Jane Doe | [email protected] |
In this example:
Customer IDis the primary key.- Each row represents a unique customer.
- Each column stores a specific attribute of that customer.
Relationships Between Tables
Relational databases connect related information using keys.
For example:
Customers Table
| Customer ID | Name |
|---|---|
| 1001 | John Smith |
Orders Table
| Order ID | Customer ID | Product |
|---|---|---|
| 5001 | 1001 | Laptop |
The Customer ID in the Orders table links the order to the customer in the Customers table. This relationship allows data to be connected without duplicating information.
Using SQL to Access Data
Relational databases are typically managed using SQL (Structured Query Language).
SQL allows users and applications to:
- Retrieve data
- Add new records
- Update existing information
- Delete records
- Generate reports
- Manage database structure
For example, a simple SQL query can retrieve all customers:
SELECT * FROM customers; Or display all orders placed by a specific customer:
SELECT * FROM orders
WHERE customer_id = 1001; Data Integrity
Relational databases use built-in rules to ensure that data remains accurate and consistent.
Common integrity rules include:
Entity Integrity
- Every table must have a primary key.
- Primary key values must be unique.
- Primary key values cannot be empty (NULL).
Referential Integrity
- Foreign key values must correspond to valid records in related tables.
- Relationships between tables remain consistent.
- Invalid or orphaned records are prevented.
Benefits of the Relational Model
Because data is organized into related tables, relational databases provide:
- Efficient data storage
- Reduced data duplication
- Fast querying and reporting
- Consistent data management
- Strong security and access controls
- Reliable relationships between datasets
Summary
A relational database works by storing data in structured tables connected through relationships. Each record is identified by a unique key, and SQL is used to manage and retrieve information. This approach provides an efficient, reliable, and scalable way to store and organize data for websites, applications, and business systems.