Fundamentals of Relational Databases

This article will introduce you to the core concepts that form the foundation of relational database systems.

This is a crash course in some of the concepts we will use in the following sections, so don't worry if you don't understand them right away. It took me a little time to use and play with databases before some concepts clicked.

Tables, Rows, and Columns

In a relational database, data is organized into tables. You can think of a table as similar to a spreadsheet or a grid. Let's break down the components:

Tables

A table is a collection of related data organized into rows and columns. For example, you might have a table called "Students" to store information about students in a school.

Rows

Each row (also called a record) in a table represents a single item or entity. In our "Students" table, each row represents one student.

Columns

Columns (also called fields) define the type of data that can be stored in each part of a row. In our "Students" table, we might have columns for "StudentID", "FirstName", "LastName", "DateOfBirth", and "Grade".

Here's a visual representation of our "Students" table:

A visual representation of the table we described right before this image

In this example:

  • The table name is "Students"
  • There are 5 columns: StudentID, FirstName, LastName, DateOfBirth, and Grade
  • There are 3 rows, each representing a different student

Common Data Types in Relational Databases

Relational databases support various data types to store different kinds of information efficiently. Here are some common types you'll encounter:

Numeric Types

Integer: Whole numbers without a fractional component.

  • Example: 42, -17, 0
  • Use this for quantities that are always whole numbers, like the number of students in a class.

Decimal or Numeric: Exact numeric values with a fixed number of decimal places.

  • Example: A decimal(5,2) can store numbers from -999.99 to 999.99
  • Use this for financial calculations or any time you need exact decimal representations.

Float: Approximate numeric values with floating decimal points.

  • Use this when you need to store very large or very small numbers, and exact precision isn't crucial.

Character Types

Fixed-length Character: Stores a fixed number of characters.

  • Example: A column defined to always store exactly 10 characters
  • Use this when you know the data will always be the same length, like state abbreviations (US, CA, etc.)

Variable-length Character: Stores strings up to a maximum length.

  • Example: A column that can store names up to 50 characters long
  • Use this for names, addresses, or any text data with varying lengths.

Date and Time Types

Date: Stores a calendar date (typically year, month, and day).

  • Example: 2023-06-24
  • Use this for birthdays, holidays, or any date where the time doesn't matter.

Time: Stores a time of day.

  • Example: 15:30:00 (3:30 PM)
  • Use this for scheduling, opening hours, or any time-specific data where the date isn't relevant.

Timestamp: Stores both date and time.

  • Example: 2023-06-24 15:30:00
  • Use this for event logs, transaction records, or any time you need to know exactly when something happened.

Boolean Type

Stores true/false values.

  • Use this for yes/no questions, on/off states, or any binary condition.

Here's how we might conceptually define our "Students" table using these data types:

Students Table:

  • StudentID: Integer
  • FirstName: Variable-length character (up to 50 characters)
  • LastName: Variable-length character (up to 50 characters)
  • DateOfBirth: Date
  • Grade: Integer
  • GPA: Decimal (3 digits total, 2 after the decimal point)
  • IsEnrolled: Boolean

Key Concepts in Relational Databases

Understanding these concepts is crucial for designing and working with relational databases effectively. These foundational ideas form the backbone of how relational databases operate and how we interact with them. They're not just theoretical concepts—they directly impact how you structure your data, ensure its integrity, and optimize database performance.

As you learn about these concepts, try to imagine how they might apply to real-world scenarios. For example, think about how a library might use primary keys to track books, or how a retail store might use foreign keys to connect customer information with their orders.

These concepts are the building blocks that allow databases to model complex real-world relationships and maintain the accuracy and consistency of data.

Grasping these concepts will empower you to:

  • Design efficient and logical database structures
  • Ensure data integrity and prevent data inconsistencies
  • Optimize database performance for faster data retrieval and manipulation
  • Understand and resolve common database issues
  • Communicate effectively with other database professionals

Don't worry if you don't fully grasp everything immediately; these ideas will become clearer as you work with databases hands-on.

Let's dive in:

Primary Key

A primary key is a column (or a combination of columns) uniquely identifying each row in a table. It's like a fingerprint for each record.

  • Must contain unique values
  • Cannot be empty (NULL)
  • A table can have only one primary key

In our Students table, StudentID serves as the primary key. This means:

  • Each StudentID must be unique
  • Every student must have a StudentID (it can't be left empty)
  • We use StudentID to identify each student in the table uniquely

This is important because:

  • Ensures each record can be uniquely identified
  • Allows for efficient data retrieval
  • Forms the basis for table relationships

Foreign Key

A foreign key is a column that creates a relationship between two tables. It refers to the primary key in another table.

Let's say we have a Courses table where each course is associated with a student:

Courses Table:

  • CourseID: Integer (Primary Key)
  • CourseName: Variable-length character
  • StudentID: Integer (Foreign Key referencing Students table)

Here, the StudentID in the Courses table is a foreign key that refers to the StudentID in the Students table, creating a relationship between the two tables.

This is important because:

  • Maintains referential integrity (ensures data consistency across tables)
  • Establishes relationships between tables
  • Prevents invalid data from being inserted into the foreign key column

Indexes

Indexes are data structures that improve the speed of data retrieval operations on a database table. Think of them like the index at the back of a book, helping you find information quickly without scanning every page.

We might create an index on the LastName column of the Students table. This would make searching for students by last name much faster, especially in a large database.

This is important because:

  • It Speeds up data retrieval for indexed columns
  • Improves the performance of data lookup and table join operations
  • Can significantly enhance query performance for large tables

Note: While indexes speed up data retrieval, they can slow down data insertion and updates and require additional storage. Use them judiciously.

Constraints

Constraints are rules enforced on data columns in a table. They limit the type of data that can be entered into a table, ensuring its accuracy and reliability.

Common constraints include:

Not Null: Ensures that a column cannot have an empty (NULL) value. For example, we might specify that FirstName and LastName cannot be empty in the Students table.

Unique: Ensures that all values in a column are different. For example, we might require that each student's email address is unique in the system.

Check: Ensures that all values in a column satisfy certain conditions. For example, we could add a rule that a student's Age must be at least 18.

Default: Specifies a default value for a column when no value is specified. For example, we might set a default value of 'Active' for a Status column in the Students table.

This is important because:

  • It maintains data integrity
  • Enforce business rules at the database level
  • Prevent invalid data from being entered into the database

For a beginner who hasn't learned SQL yet, it might be better to simplify the explanation and avoid too much technical jargon. Here's a suggestion for a more beginner-friendly approach:

Organizing Data Effectively

When working with databases, it's important to organize data in a way that's efficient and easy to manage. This process is called "normalization" in database design. While there are technical details to this process, let's focus on the main ideas that are helpful for beginners:

Avoid Repetition: Avoid storing the same information in multiple places. This helps keep your data consistent and saves space.

Group Related Information: Keep data that belongs together in the same table. For example, all customer information should be in one place.

Use Separate Tables for Different Types of Data: Instead of having one big table with everything, use multiple tables for different types of information.

Connect Tables with IDs: Link information between tables using unique identifiers (like customer IDs).

Imagine you have a table that looks like this:

An example of an unorganized order table

To organize this better with the above principles in mind, we could split it into three tables:

The result of splitting the orders into 3 separate tables

By doing this:

  • We avoid repeating customer information
  • Keeps product information separate and easy to update
  • Uses IDs to connect information across tables (you'll see I highlighted these connections in blue and green).

Another bonus of organizing data this way is that you'll make it easier to:

  • Update information (like a customer's email) in just one place
  • Add new products or customers without changing other tables
  • Retrieve information more efficiently

As you learn more about databases, you'll discover more advanced techniques for organizing data. But starting with these basic principles will give you a strong foundation.

As we progress in this series, we'll see how these concepts are implemented. We'll also learn how to insert data and query our database using SQL (Structured Query Language). The syntax might look complex at first, but you'll become comfortable reading and writing database commands with practice.

Remember, mastering these concepts takes time and practice. Don't worry if everything doesn't click immediately; we'll revisit and build upon these ideas throughout the series.

BeginnerDatabasePostgresql
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.