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.
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.
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.
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.
| id | first_name | last_name | grade |
|---|---|---|---|
| 1 | Maya | Chen | 10 |
Try writing the SQL: INSERT INTO Students (first_name, last_name, grade) VALUES ('', '', );
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.
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.
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.