SQL Fundamentals
Introduction to Databases
What Is a Database?
At its core, a database is just an organized collection of data, stored electronically. Think of it as a highly efficient digital filing cabinet. Instead of stuffing papers into folders, you store information in a structured way that makes it easy to find, manage, and update.
There are many kinds of databases, but the most common type is the Relational Database Management System, or RDBMS. This is the type of database that SQL (Structured Query Language) is designed to work with. In a relational database, data is organized into tables, which can be linked to each other based on shared data.
The Structure of a Database
Relational databases use a simple and intuitive structure made up of tables, columns, and rows. It’s a lot like a spreadsheet.
A table organizes data about a specific topic, like customers or products.
A column contains a specific piece of information for every record in the table, like a name or an email address.
A row represents a single, complete record in the table, like all the information for one specific customer.
Imagine a table for storing customer information. It might be called Customers and look something like this:
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 1 | Maria | Anders | maria.a@example.com |
| 2 | Ana | Trujillo | ana.t@example.com |
| 3 | Antonio | Moreno | antonio.m@example.com |
Here, CustomerID, FirstName, LastName, and Email are the columns. Each row is a unique record for a single customer.
Data Types and Keys
Every column in a database table is assigned a data type. This tells the database what kind of data the column is allowed to hold, such as text, numbers, dates, or boolean values (true/false). Defining data types is crucial. It ensures that the data is consistent and helps the database store information efficiently. You can't store a name in a column meant for dates.
To keep the data organized and prevent duplicates, we use keys. The most important one is the primary key.
Primary Key
noun
A column (or set of columns) in a table that contains a unique value for each row. It cannot have null values.
But what if we have more than one table? How do we connect them? That's where foreign keys come in. A foreign key is a column in one table that refers to the primary key in another table. It's the 'glue' that links your tables together.
Let's say we have an Orders table. To know which customer placed an order, we can include a CustomerID column.
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 101 | 2 | 2023-10-26 |
| 102 | 1 | 2023-10-26 |
| 103 | 2 | 2023-10-27 |
In the Orders table, OrderID is the primary key. The CustomerID column is a foreign key because it links back to the CustomerID primary key in the Customers table. Now we can easily see that Ana Trujillo placed orders 101 and 103.
This structure of related tables is the foundation of a relational database. It minimizes data redundancy and keeps everything consistent and reliable. Understanding this structure is the first step to mastering SQL, which is the language we use to interact with all this organized data.