No history yet

Introduction to Relational Databases

Organizing Information

At its heart, a relational database is just a collection of organized lists. Think of it like a set of spreadsheets that are all linked together in a smart way. Instead of cramming all your information into one giant, messy document, you split it up into logical groups. This keeps everything tidy and efficient.

For example, imagine a database for a school. You wouldn't put a student's name, their classes, their teachers, and their grades all in one massive list. It would get repetitive and confusing fast. Instead, you'd create separate lists for students, courses, and maybe another to track who is enrolled in what. A relational database gives us a formal structure for doing this.

The Building Blocks

Every relational database is built from a few simple components. The main one is the table.

Table

noun

A collection of related data held in a structured format with rows and columns. In our school example, we would have a Students table and a Courses table.

Each table is made up of columns and rows.

Columns define what kind of information you're storing, like FirstName or CourseName.

Rows represent the actual entries, like a specific student or a particular course.

Here’s what a simple Students table might look like:

StudentIDFirstNameLastName
101AliceSmith
102BobJohnson
103CharlieBrown

Creating Connections

The real power of a relational database comes from creating relationships between tables. We can't just have separate lists; we need a way to link them. This is done using keys.

Primary Key

noun

A column (or set of columns) in a table that uniquely identifies each row. No two rows can have the same primary key, and it cannot be empty.

To connect tables, we use a foreign key. It's a key that refers to the primary key in another table.

Foreign Key

noun

A column in one table that is used to link to the primary key in another table. This is what creates the 'relation' in a relational database.

Let's visualize this. We have a table for students and a table for the courses they're enrolled in. The StudentID connects them.

Problems to Avoid

A well-organized database is crucial. When information isn't structured properly, two major problems arise: data redundancy and data anomalies.

Data redundancy is the unnecessary repetition of data. Imagine if we listed the teacher's full name and office number for every single student enrolled in their class. If a teacher changed offices, you'd have to update that information in dozens or even hundreds of places. It's inefficient and a recipe for mistakes.

Redundancy wastes space and creates opportunities for inconsistency.

Data anomalies are the inconsistencies that result from redundancy. There are three main types:

  1. Update Anomaly: If you fail to update every repeated instance of a piece of data, your database will have conflicting information. For example, a teacher might have their old office number listed for some students and the new one for others.

  2. Insertion Anomaly: You can't add certain information because other information is missing. For example, you can't add a new course to the database until at least one student has enrolled in it.

  3. Deletion Anomaly: Deleting one piece of data accidentally erases another. If a student withdraws from a course and they were the only one enrolled, deleting their enrollment might erase the course itself from the database.

These problems highlight why careful design is so important. By organizing data into separate, related tables, we can avoid these pitfalls. The process of refining this design is called normalization, which you'll explore next.