01 / 21

Tables, Rows
& Schemas

Master the foundational building blocks of every relational database: how data is organized into tables, rows, columns, and governed by schemas.

SQL

What Is a Relational Database?

A relational database is an organized collection of data stored in tables that can relate to each other. Think of it as a collection of spreadsheets, but with strict rules that keep your data consistent and reliable.

The Table: Your Data Container

id name grade 1 Alice 10 2 Bob 11 3 Carol 10 students (table name) ← row ↑ column

A table stores data about one kind of thing. Each table has a name (like students) and is made up of columns and rows.

Rows, Columns & Cells

Row
A single record: one complete entry in the table. Example: all data about Alice.
Column
A single attribute: one category of data shared by every row. Example: "name".
Cell
The intersection of a row and a column: one specific piece of data. Example: "Alice".

What Is a Schema?

A schema is the blueprint of your database. It defines which tables exist, what columns each table has, and what data type each column accepts: before any data is ever inserted.

Schema: school_db students id INTEGER name TEXT grade INTEGER courses id INTEGER title TEXT teacher TEXT room INTEGER The schema defines structure before data exists

Column Data Types

Every column in a schema has a data type that controls what values it can hold. This prevents invalid data from entering your database.

INTEGER
Whole numbers: 1, 42, -7
TEXT
Strings of characters: "Alice", "Biology 101"
REAL
Decimal numbers: 3.14, 98.6
BOOLEAN
True or false: TRUE, FALSE

The Primary Key Rule

Every table should have a primary key: a column (or set of columns) whose value is unique for every row. It guarantees that no two rows are identical and gives you a reliable way to find any single record.

id (PK) email name 1 alice@school.edu Alice 2 bob@school.edu Bob 3 carol@school.edu Carol ★ unique per row

Key Distinction: Four Core Concepts

Concept Definition Analogy
Table A collection of rows and columns about one entity A single spreadsheet tab
Row One complete record in the table One horizontal line of data
Column One attribute shared by all rows A vertical header category
Schema The blueprint: tables, columns, and their types The building's floor plan

Creating a Table with SQL

The CREATE TABLE statement defines a new table in your schema. You specify the table name, each column name, and each column's data type.

CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, grade INTEGER, email TEXT );

How to Design a Table

1
Identify the entity. What "thing" does this table represent? (students, products, orders)
2
List its attributes. What facts do you need to store about each one? (name, email, grade)
3
Choose data types. Is each attribute text, a number, a date, or a boolean?
4
Pick a primary key. Choose a column that uniquely identifies every row.
5
Add constraints. Mark required columns with NOT NULL. Set defaults where helpful.

Example 1: A Books Table

A school library needs to track its books. Let's design a books table.

CREATE TABLE books ( book_id INTEGER PRIMARY KEY, title TEXT NOT NULL, author TEXT NOT NULL, pages INTEGER, available BOOLEAN DEFAULT TRUE );
book_id title author pages available 1 To Kill a Mockingbird Harper Lee 281 TRUE 2 1984 George Orwell 328 FALSE 3 The Great Gatsby F. Scott Fitzgerald 180 TRUE

Example 2: Inserting a Row

Once a table exists, you add data with INSERT INTO. Each insert creates a new row.

INSERT INTO students (id, name, grade, email) VALUES (4, 'Diana', 12, 'diana@school.edu');

Example 3: A Multi-Table Schema

Real databases have multiple tables that relate to each other. Here, enrollments connects students to courses.

students id (PK) INTEGER name TEXT grade INTEGER enrollments id (PK) INTEGER student_id INTEGER course_id INTEGER courses id (PK) INTEGER title TEXT teacher TEXT FK FK student_id and course_id are Foreign Keys: they reference the primary keys of other tables. This is how relational databases connect data across tables.

Schema vs Data

Schema (Structure)
Defined once with CREATE TABLE. Changes rarely. Describes what data can exist.
Data (Content)
Added with INSERT, changed with UPDATE, removed with DELETE. Changes constantly.

Common Mistakes to Avoid

✕ No Primary Key
Without a primary key, you cannot uniquely identify rows. Duplicates creep in and queries become unreliable.
✕ Wrong Data Type
Storing a phone number as INTEGER loses leading zeros. Store it as TEXT instead to preserve format.
✕ One Giant Table
Cramming all data into one table leads to repeated data and update anomalies. Split into related tables.

Summary: SQL Building Blocks

Term What It Is SQL Command
Table A named container for rows and columns CREATE TABLE
Row A single record (one entry) INSERT INTO
Column An attribute with a name and data type Defined in CREATE TABLE
Schema The full blueprint of all tables and types Collection of CREATE statements
Primary Key Column(s) that uniquely identify each row PRIMARY KEY constraint
Data Type Rule for what kind of value a column holds INTEGER, TEXT, REAL, BOOLEAN

Question 1 of 5

In a relational database, what does a row represent?

Question 2 of 5

What is the purpose of a schema in a database?

Question 3 of 5

Which SQL constraint ensures that no two rows in a table share the same value in a specific column?

Question 4 of 5

What will happen when this INSERT statement runs?

Question 5 of 5

A school database has a students table and a courses table. You need a way to record which students are in which courses. What is the best approach?