stackademic

The leading education platform for anyone with an interest in software development.

Relational Basics

How relational databases organize data into tables of rows and columns

Overview

A relational database stores data in tables, where each table represents one kind of thing (users, orders, products). Every row is a single record and every column is an attribute with a defined data type. This structure matters because it keeps data consistent, avoids duplication, and lets you combine related tables with predictable queries.

Syntax / Usage

You define a table with CREATE TABLE, then read and write rows with SQL statements. Each column has a name and a type, and constraints like NOT NULL enforce rules.

CREATE TABLE users (
  id         INTEGER PRIMARY KEY,
  email      TEXT NOT NULL UNIQUE,
  full_name  TEXT,
  created_at TIMESTAMP DEFAULT now()
);

INSERT INTO users (id, email, full_name)
VALUES (1, 'ada@example.com', 'Ada Lovelace');

SELECT id, email FROM users WHERE full_name IS NOT NULL;

Examples

Reading specific columns for matching rows:

SELECT email, created_at
FROM users
WHERE created_at > '2026-01-01'
ORDER BY created_at DESC;

Updating a single record found by its primary key:

UPDATE users
SET full_name = 'Ada King'
WHERE id = 1;

Common Mistakes

  • Storing several values in one column (e.g. a comma-separated list) instead of using rows or a related table
  • Forgetting WHERE on UPDATE or DELETE, which changes every row
  • Using the wrong data type, such as TEXT for dates, which breaks sorting and comparisons
  • Allowing NULL where a value is always required instead of adding NOT NULL

See Also

databases-keys-and-relationships databases-sql-vs-nosql databases-normalization