Please enable JavaScript to use CodeHS

Data and Databases

Tables and rows

A database is like a collection of filing cabinets. Each cabinet is a table, each drawer is a row, and each label on the drawer is a column. Organizing data this way makes it easy to search, sort, and update.

SQL ~10 min Middle / High School
The Big Idea

What is a database?

A database is a system that stores data so it can be searched, updated, and retrieved reliably. Databases power almost every app you use, from social media to online banking to video games.

There are many kinds of databases, but the most common kind for everyday apps is a relational database. Relational databases store data in tables that can be linked together. Popular examples include MySQL, PostgreSQL, and SQLite.


How It Works

Tables, rows, and columns

Data in a relational database is stored in tables. A table has a name and a set of columns. Each column has a name and a data type, such as text, number, or date. A row is one record with values for each column.

For example, a table called Students might have columns for id, first_name, last_name, and grade. Each row represents one student.

Vocabulary: the design of a table, including its columns and types, is called its schema. A schema is like a blueprint for your data.


Key Concept

Primary keys

Every table usually has a primary key. This is a column whose value is unique for each row. It acts like an ID number that lets the database find one specific record quickly.

Primary keys are important because names can be shared. Two students might both be named Alex Smith. An id number makes sure you always know which Alex Smith you are talking about.


Interactive

Build a table

Fill in the fields below to add a new row to the Students table. Watch the table grow, then try writing an INSERT statement that matches your data.

idfirst_namelast_namegrade
1MayaChen10

Try writing the SQL: INSERT INTO Students (first_name, last_name, grade) VALUES ('', '', );


Code

A table as a list of rows

A database table is a collection of rows with the same columns. This Python list is a tiny table you can query with list operations.


Your Turn

Check yourself

Design a table for storing books in a school library. List at least three columns and explain why each one is useful.

Your answer:

Sample answer: title, author, isbn, year_published, available. title and author describe the book. isbn is a good primary key because it is unique. available is true or false so you know if the book can be checked out.


Check Yourself

Quick quiz

1. In a database table, what is a row?

2. What is a primary key?

3. What is a schema?

Keep learning

You know how tables store data. Next, learn how to ask a database questions with SQL.

Part of the Data and Databases series.