SQL Fundamentals
Introduction to Databases
What is a Database?
Think of a database as a digital filing cabinet. It's a system for storing and organizing information so you can easily find, update, and manage it. Every time you check your bank account, browse an online store, or use social media, you're interacting with a database.
At its core, a database is an organized collection of structured information, or data, typically stored electronically. Its main job is to make data management efficient and reliable. Without databases, finding a specific piece of information would be like searching for a single book in a library with no catalog and no organized shelves. Databases provide the structure and tools needed to keep everything in its right place.
Types of Databases
There are many ways to organize a digital filing cabinet. The two most common types of databases are relational and non-relational.
Relational databases store data in tables, which look a lot like spreadsheets. Each table has rows and columns, creating a neat, predictable structure. This model is excellent for handling data where relationships between different pieces of information are important. This is the type of database that SQL (Structured Query Language) is designed to work with.
Non-relational databases, often called NoSQL databases, are more flexible. They can store data in various formats, such as documents, key-value pairs, or graphs. This makes them useful for large amounts of unstructured data, like social media posts or sensor data. While powerful, they work differently and don't rely on the same rigid structure as their relational counterparts.
For the rest of this discussion, we'll focus on relational databases, as they are the foundation for learning SQL.
The Building Blocks
Relational databases have a clear hierarchy. Let's break down the basic components, from largest to smallest.
Tables: A table is a collection of related data organized in rows and columns. You can think of it as a single spreadsheet. Each table in a database holds information about one specific type of thing, like 'Customers' or 'Products'.
Records (or Rows): A record is a single entry in a table. If you have a 'Students' table, each row would represent one specific student.
Fields (or Columns): A field is a single piece of information within a record, defining an attribute of the entity. In our 'Students' table, you would have columns for 'StudentID', 'FirstName', 'LastName', and 'Major'.
Here’s what a simple 'Students' table might look like:
| StudentID | FirstName | LastName | Major |
|---|---|---|---|
| 101 | Sarah | Jones | Biology |
| 102 | David | Smith | Computer Science |
| 103 | Emily | Chen | English |
Connecting the Dots
The real power of a relational database comes from creating relationships between tables. This is done using keys.
Primary Key
noun
A field (or set of fields) that uniquely identifies each record in a table.
A primary key ensures that you can reliably find and refer to a specific row. It cannot contain duplicate values, and it cannot be empty (NULL).
Foreign Key
noun
A field in one table that refers to the primary key in another table.
Foreign keys are the glue that holds a relational database together. They create a link between tables, allowing you to combine data. For instance, you could have a separate 'Courses' table. To know which students are in which courses, you could create an 'Enrollments' table that uses the primary key from 'Students' and the primary key from 'Courses' as foreign keys.
This structure prevents you from having to repeat student names in the 'Enrollments' table. You just need their ID. This principle of reducing redundancy is called normalization, and it's key to designing efficient and reliable databases.
By organizing data into separate, related tables, normalization ensures data integrity. That means when you update a student's name in the 'Students' table, that change is automatically reflected everywhere that student's ID is used, without needing to change it in multiple places.
What is the primary function of a database?
Which of the following best describes the structure of a relational database?
Understanding these core concepts—databases, tables, keys, and relationships—is the first step. They provide the framework that allows a language like SQL to perform its magic.