AlgoMaster Logo

Database Basics

Last Updated: January 3, 2026

7 min read

Understanding databases is crucial for any developer, especially when you're working with data-driven applications. Imagine trying to manage users, products, or any kind of organized information without a reliable system to store and retrieve that data.

It's a bit like trying to find a specific book in a huge library without a catalog—it would be quite a challenge!

In this chapter, we’ll explore the foundational concepts of databases, ensuring you have a solid grasp of what they are, how they function, and why they are essential in software development.

What Is a Database?

At its core, a database is an organized collection of data. It allows you to store, retrieve, and manipulate that data efficiently. Think of it as a digital filing system where information is stored in a structured way.

There are several types of databases, but in this chapter, we will focus primarily on relational databases. These databases organize data into tables, which consist of rows and columns. Each table represents a different entity, like users or products, and the relationships between those tables allow us to structure complex datasets.

Key Characteristics of Databases

  • Structured Data: Data is stored in a predefined format, making it easy to query and analyze.
  • ACID Compliance: Relational databases follow the ACID model (Atomicity, Consistency, Isolation, Durability), ensuring reliable transactions.
  • Scalability: Databases can handle large volumes of data and user requests.

Relational vs. Non-Relational Databases

While relational databases are widely used, it is also important to know about non-relational databases, often referred to as NoSQL databases.

Relational Databases

  • Structure: Data is stored in tables with a fixed schema. This means that each table must have a predefined set of columns.
  • SQL: Structured Query Language (SQL) is used to interact with relational databases. SQL allows for complex queries and data manipulation.

Non-Relational Databases

  • Flexibility: They can store unstructured or semi-structured data. This makes them suitable for applications with evolving data requirements.
  • Variety: Different types include document stores (like MongoDB), key-value stores (like Redis), and graph databases (like Neo4j).

In many situations, you’ll find relational databases being used for transactional systems, while non-relational databases may be favored for big data applications or real-time analytics.

Basic Database Concepts

Before diving deeper, let's cover some fundamental concepts common to all databases.

Tables

A table is a collection of related data entries and consists of rows and columns. Each row is a record, and each column is a property of the data. For example, a users table might have columns like id, name, email, and created_at.

Keys

  • Primary Key: A unique identifier for each record in a table. For instance, the id column in a users table is often the primary key.
  • Foreign Key: A field (or collection of fields) in one table that uniquely identifies a row in another table. This is how tables relate to each other.

Relationships

Understanding relationships is crucial in relational databases. There are three primary types:

  • One-to-One: Each record in Table A corresponds to one record in Table B.
  • One-to-Many: A record in Table A can relate to multiple records in Table B. For instance, a user can have multiple orders.
  • Many-to-Many: Multiple records in Table A can relate to multiple records in Table B, often requiring a junction table to manage the relationships.

Database Normalization

Normalization is a process of organizing the fields and tables of a database to reduce redundancy and improve data integrity.

Normal Forms

Normalization typically involves several stages, known as normal forms:

  • First Normal Form (1NF): Ensures that each column contains atomic values (no repeating groups).
  • Second Normal Form (2NF): Achieved when a table is in 1NF and all non-key attributes are fully functional dependent on the primary key.
  • Third Normal Form (3NF): A table is in 2NF and all attributes are functionally dependent only on the primary key.

Example of Normalization

Let’s consider a simple scenario where we have a products table:

This table has redundancy since supplier details are repeated. To normalize it, we can separate supplier information into a new suppliers table:

This reduces redundancy and makes updates easier.

Indexing

Indexing is a technique used to speed up the retrieval of records from a database table. Think of it like the index of a book, which helps you find information quickly without having to read every page.

How Indexes Work

An index creates a data structure (usually a B-tree) that allows the database to find rows faster. However, keep in mind that while indexes speed up read operations, they can slow down write operations since the index must also be updated.

Creating an Index

Here’s how you can create an index on a users table:

This index will help speed up queries that filter or search by the email column.

Considerations

  • Over-indexing: Adding too many indexes can negatively impact performance during data inserts or updates.
  • Indexing Strategy: Analyze your queries and decide which columns benefit most from indexing.

Transactions

A transaction is a sequence of operations performed as a single logical unit of work. Transactions are essential for maintaining data integrity, especially in applications that require multiple operations to be completed successfully or not at all.

ACID Properties

Transactions in databases must adhere to the ACID properties:

  • Atomicity: Ensures that all operations within a transaction are completed; if one fails, the entire transaction fails.
  • Consistency: A transaction takes the database from one valid state to another.
  • Isolation: Transactions must operate independently of each other.
  • Durability: Once a transaction has been committed, it will remain so, even in the event of a system failure.

Example of a Transaction

Here’s an example of how you might implement a transaction in SQL:

This ensures that both the deposit and withdrawal happen together, maintaining consistency.

Conclusion

As we’ve explored in this chapter, having a solid understanding of database basics is fundamental for building efficient, data-driven applications. We discussed the nature of databases, the differences between relational and non-relational systems, key concepts like normalization and indexing, and the importance of transactions.

These fundamentals serve as the backbone for the more complex topics we will cover next, especially as you start working with specific tools like SQLite.

Now that you understand the foundational concepts of databases, you are ready to explore the sqlite3 module.

In the next chapter, we will dive deeper into how to connect to SQLite databases, execute queries, and manipulate data using Python.