Introduction to Relational Databases

Introduction to Relational Databases

Understanding the Basics

At its core, a relational database organizes data into tables, which consist of rows and columns. Each row represents a record, a single data item, and each column represents a field, a specific type of data within a record.

Example of a Table

+----+-------+-----+
| id | name  | age |
+----+-------+-----+
| 1  | John  | 25  |
| 2  | Alice | 30  |
| 3  | Bob   | 35  |
+----+-------+-----+

In this table, each row is a record of an individual’s id, name, and age. Each column specifies what data the field holds.

The Role of Schemas

A schema is a blueprint of how a database is structured. It defines the tables, the fields in each table, and the type of data each field can hold. For the table in the previous section, the schema would specify that id and age are integers and name is a string.

Schemas make sure the data stays consistent by applying rules. These rules include making sure every record has data for each field and that the data matches the specified type.

Relationships Between Tables

Relational databases allow tables to be linked, creating relationships between different data items.

These relationships can be:

  • One-to-One: Each row in Table A is linked to one row in Table B, and vice versa. For example, a person’s passport number is unique to them.

  • One-to-Many: A single row in Table A can be linked to many rows in Table B. For example, a customer can have multiple orders.

  • Many-to-Many: Rows in Table A can be linked to multiple rows in Table B, and vice versa. For example, a student can take multiple classes, and a class can have multiple students.

Interacting with Databases: SQL

Structured Query Language (SQL) is the standard language for interacting with relational databases. It allows you to perform various operations, such as creating tables, inserting data, querying data, updating data, and deleting data.

SQL is declarative, meaning you describe what you want to do, and the database figures out the most efficient way to do it.

Ensuring Reliability: ACID Properties

Relational databases are built to be reliable, making sure data stays accurate and consistent through transactions. Transactions in a database follow the ACID properties:

  • Atomicity: Ensures a transaction is all-or-nothing.

  • Consistency: Guarantees that a transaction takes the database from one valid state to another.

  • Isolation: Keeps transactions separate from each other until they’re completed.

  • Durability: Ensures that once a transaction is committed, it remains so, even in the event of a system failure.

Optimizing Searches: Indexes

As databases get bigger, searching through them can slow down. Indexes are like special lookup tables that help the database find data faster. Adding an index to a database column is like putting in a bookmark, making it easier to find specific information.

SQL Example for Creating an Index

CREATE INDEX name_index ON employees (name);

This index would make queries searching for employees by name much faster.

Managing Transactions in SQL

Transactions in SQL ensure that operations involving multiple steps are either complete fully or not at all.

Here’s a simple example:

BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

This transaction moves money between accounts, ensuring both operations complete successfully before committing the changes.

Consistency Models: Strong vs. Eventual

  • Strong Consistency: Changes made by a transaction are immediately visible to all other transactions. This applies to Relational Databases' transactions.

  • Eventual Consistency: Changes may not be instantly visible but will become so over time, ensuring data consistency across the database eventually. Outside of relational databases, this may apply to other data storage systems.