No history yet

Introduction to Databases

What is a Database?

Think of a database as a highly organized digital filing cabinet. Instead of storing paper in folders, it stores data in a structured way, making it easy to find, manage, and update information. While a simple spreadsheet can hold data, a database is far more powerful, especially when dealing with large amounts of complex, interconnected information.

The most common type is a Relational Database Management System, or RDBMS. The key word here is "relational." These databases are designed to recognize relationships between different pieces of information. This structure is what allows us to ask complex questions and pull together related data from different places with ease. Most of the databases you'll interact with, like MySQL, PostgreSQL, and SQL Server, are relational.

The Building Blocks

In a relational database, data is organized into tables. A table is just a collection of related data held in a grid format, with rows and columns, much like a spreadsheet.

  • Columns represent the attributes or characteristics of the data (e.g., name, email, city).
  • Rows represent individual records, with each cell in the row containing a value for a specific column.

For example, a table for storing customer information might look like this:

CustomerIDFirstNameLastNameEmail
1MariaAndersmaria.a@example.com
2AnaTrujilloana.t@example.com
3AntonioMorenoantonio.m@example.com

Each row is a unique customer, and each column gives us a specific piece of information about them. But how does the database know that each customer is unique? That's where keys come in.

Primary Key

noun

A column (or set of columns) in a table that uniquely identifies each row. The value in this column must be unique for every record and cannot be empty (NULL).

In our Customers table, the CustomerID is the primary key. Even if two customers have the same name, their CustomerID will always be different. This guarantees we can pinpoint any specific record without ambiguity.

Making Connections

The real power of a relational database comes from creating relationships between tables. We don't want to cram all our information into one giant table. Instead, we can create separate tables for different types of information, like customers, products, and orders, and then link them together.

Let's say we have another table for orders. To know which customer placed an order, we can include the customer's unique ID in the Orders table. This linking column is called a foreign key.

Foreign Key

noun

A column in one table that refers to the primary key in another table. It acts as a link or cross-reference between the two tables.

Here’s what our Orders table might look like. Notice the CustomerID column, which connects each order back to a specific customer.

OrderIDCustomerIDOrderDate
10132023-04-10
10212023-04-11
10332023-04-12

This relationship allows us to easily find all orders placed by Antonio Moreno (CustomerID 3) or identify which customer placed Order 102 (Maria Anders, CustomerID 1). By splitting data into logical tables and linking them with keys, we create a database that is efficient, organized, and easy to manage.

A primary key uniquely identifies a record in its own table. A foreign key links a record in one table to a primary key in another table.

This simple but powerful structure of tables and keys is the foundation of relational databases. Understanding it is the first step to writing SQL queries that can retrieve and manipulate data in meaningful ways.

Now, let's check your understanding of these core concepts.

Quiz Questions 1/5

What is the primary purpose of a database?

Quiz Questions 2/5

In a table of university students, what is the main function of a unique StudentID column?