What you will learn
- Describe tables, rows, columns and primary keys
- Write basic SELECT, INSERT, UPDATE and DELETE queries
- Explain how to query a database safely from server code
A database stores data so it survives after a request ends. Relational databases, such as PostgreSQL and MySQL, organise data into tables. Each table has columns that define the fields, such as name and email, and rows that hold individual records. A primary key, usually an id column, uniquely identifies each row. Tables can reference each other, for example an orders table pointing to a customers table.
SQL is the language used to work with relational databases. SELECT name, price FROM products WHERE in_stock = true reads matching rows. INSERT INTO messages (name, email) VALUES (...) adds a row, UPDATE changes existing rows, and DELETE removes them. Always include a WHERE clause on UPDATE and DELETE, or the change applies to every row in the table.
Your site should only talk to the database from server code, such as a route handler, never directly from the browser. Use parameterised queries, where the database library inserts user values separately from the SQL text, to prevent SQL injection attacks. Many hosted database services offer free starter plans, and libraries or ORMs can help you write queries in JavaScript or TypeScript.


