SQL Fundamentals for Beginners
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
Customersand another forProducts.- Rows represent a single record or item. In a
Customerstable, each row would be a different person.- Columns define the specific attributes for each record. For the
Customerstable, you might have columns forCustomerID,FirstName,LastName, and
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.
| Operation | Name | Description |
|---|---|---|
| C | Create | Add a new row of data to a table. |
| R | Read | Retrieve or search for data that already exists. |
| U | Update | Modify an existing row of data. |
| D | Delete | Permanently 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, andMajor. - Read: You need to find a student's major. You search the table for their
StudentIDand read the value in theMajorcolumn. - Update: A student changes their major from Biology to Chemistry. You find their row and change the value in the
Majorcolumn. - 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.
What is the main purpose of a database?
In a relational database, how is information typically organized?