No history yet

Introduction to Databases

What's a Database?

Think of a database as a highly organized digital filing cabinet. Instead of paper folders, it uses a structured system to store, manage, and retrieve vast amounts of information quickly and efficiently. Your phone's contact list is a simple database. A library's catalog, a company's employee records, or an online store's inventory are all managed by more complex databases.

The main job of a database is to bring order to data. It turns a chaotic jumble of information into a neat, searchable collection. This organization makes it possible for applications to find the exact piece of information they need, right when they need it.

Organizing with Relations

The most common type of database is a relational database. The name comes from the way it organizes information into collections of tables, which can be related to one another. A Relational Database Management System (RDBMS) is the software that manages these kinds of databases.

Imagine you have a table of authors and another table of books. A relational database allows you to link a book to its author, creating a relationship between the two tables. This is far more efficient than storing the author's complete information over and over again for every book they've written.

Data in these tables is arranged in a simple grid structure.

  • Tables are collections of related data, like a spreadsheet. You might have a table for Customers and another for Products.
  • Rows represent a single record or item. In a Customers table, each row would be a different person.
  • Columns define the specific attributes for each record. For the Customers table, you might have columns for CustomerID, FirstName, LastName, and Email.

Keeping Data Connected

For relationships between tables to work, we need a reliable way to identify each row. Just using a person's name isn't enough—there could be two people named 'John Smith'. Databases solve this problem using keys.

Primary Key

noun

A column (or a set of columns) that contains a unique identifier for each row in a table. This value cannot be empty or duplicated. In our Students table, StudentID is the primary key because no two students can have the same ID.

Primary keys ensure that every record is unique. But how do we link a student to the classes they're enrolled in? That’s where foreign keys come in.

Foreign Key

noun

A column in one table that refers to the primary key of another table. It acts as a link or a bridge, creating a relationship between the two tables.

If we had an Enrollments table, we could include a StudentID column. This StudentID column in the Enrollments table would be a foreign key that points back to the primary key in the Students table. This is how we know which student is enrolled in which class without repeating all of their information.

The Four Basic Operations

No matter how complex a database is, there are only four fundamental operations you can perform on its data. These are often known by the acronym CRUD.

OperationNameDescription
CCreateAdd a new row of data to a table.
RReadRetrieve or search for data that already exists.
UUpdateModify an existing row of data.
DDeletePermanently remove a row of data.

Here's how these apply to our Students table:

  • Create: A new student enrolls. You add a new row with their StudentID, FirstName, LastName, and Major.
  • Read: You need to find a student's major. You search the table for their StudentID and read the value in the Major column.
  • Update: A student changes their major from Biology to Chemistry. You find their row and change the value in the Major column.
  • Delete: A student graduates and their record is archived. You remove their row from the active students table.

These four operations are the building blocks for almost everything you do with a database.

With these concepts in mind, you're ready to see how we can use a language to perform these operations and interact with a database.

Quiz Questions 1/5

What is the main purpose of a database?

Quiz Questions 2/5

In a relational database, how is information typically organized?