Every modern web application, online store, or content management system (CMS) needs a place to securely store data. Whether it is user accounts, product orders, or blog posts, a relational database is almost always running in the background. In this article, we will look at the basics of the SQL language and how databases organize and process information.
What is SQL and a Relational Database?
SQL (Structured Query Language) is a standardized programming language designed for communicating with relational databases (such as MySQL, PostgreSQL, or SQLite). In a relational database, data is organized into tables consisting of rows (records) and columns (attributes).
| Term | Description | Example |
|---|---|---|
| Table | A collection of related data about a specific object. | users, products, orders |
| Column / Field | A specific property or attribute of data. | first_name, email, price |
| Row / Record | A single entry or entity within a table. | One specific user with all their details |
Basic Operations in SQL (CRUD)
Most work with databases relies on four fundamental operations, collectively known as CRUD (Create, Read, Update, Delete). For each of these operations, SQL provides a corresponding command word.
- SELECT: Reading and retrieving data from one or more tables.
- INSERT: Inserting new records into a table.
- UPDATE: Modifying and updating existing data.
- DELETE: Removing unnecessary or outdated records.
Example of Basic SQL Commands
The example below shows how to retrieve, insert, and update data in a user table using simple queries.
-- 1. Selecting all users who have an active status
SELECT user_id, first_name, email
FROM users
WHERE status = 'active';
-- 2. Inserting a new user into the table
INSERT INTO users (first_name, email, created_at)
VALUES ('John', 'john@example.com', NOW());
-- 3. Updating the email address for a specific user
UPDATE users
SET email = 'new_email@example.com'
WHERE user_id = 42;
Importance of Primary and Foreign Keys
To ensure uniqueness and link tables together, we use keys. A Primary Key is a unique number or identifier for each row in a table (e.g., user_id). A Foreign Key is a column in one table that references the primary key in another table – creating a relationship between an order and the customer who placed that order.