Welcome. This is a prerequisite lesson, not an attack lesson. authentication-login, sql-injection-data-extraction, nosql-injection, and sensitive-data-discovery assume you can already read and write a database query. If you have never seen SQL, those lessons would teach an attack on top of a skill you do not yet have. This lesson supplies the skill.
There is nothing to exploit here. This is pure data modeling. By the end you will be able to read a query and predict why injection works — which is exactly the foundation the SQL injection lessons build on.
In this lesson you will:
- See how a relational database organizes data into tables of rows and columns, linked by primary and foreign keys.
- Learn the four CRUD operations —
SELECT,INSERT,UPDATE,DELETE— and read aSELECT … FROM … WHEREquery. - Meet **
UNION**, the operator that SQL injection abuses, and learn why both halves must return the same number of columns. - Understand the single seed of injection: how an application turns what a user types into a query by gluing strings together.
Estimated time: 12 minutes.
What a database is
Almost every web application needs to remember things between requests — who the users are, what products exist, what is in a basket. A database stores that data durably. The most common kind is a relational database (SQLite, MySQL, PostgreSQL), which organizes data into tables that can relate to one another, and which you talk to using a language called SQL.
A table, at a glance
Picture a users table:
| id | role | |
|---|---|---|
| 1 | [email protected] | admin |
| 2 | [email protected] | user |
- A column is a named, typed field (
emailholds text). Every row has the same columns. - A row is one entry — one user.
- A primary key (
id) uniquely identifies a row. - A foreign key is a column that points at another table's primary key — that is how an
orderstable links each order to a user.
This — typed columns, uniquely-keyed rows, foreign keys linking tables — is the whole relational model. Everything SQL does is read, add, change, or remove rows in tables like this.
When you are ready, send the Continue signal.