Module 3 ยท The server: Node, Express and the database Lesson 8 of 15
SQL and schema design
Tables, rows and columns. A job has a title, a company and a location; a company has many jobs. Writing that down is the whole skill.
The concept
A schema is the shape of your data: tables, the columns in each, the type of every column, and which column links two tables. Spend twenty minutes drawing it on paper before writing any query โ a job board needs users, jobs and applications, and every line of code after that is easier because the shape is settled.
Use an integer primary key on every table, a foreign key to point at another table, and add an index to any column you filter or join on. created_at on every row costs nothing and answers "how many this month" for ever. Never store what you can calculate, and never put two values in one column.
CREATE TABLE jobs (
id INT PRIMARY KEY,
title VARCHAR(160) NOT NULL,
company_id INT NOT NULL,
created_at DATETIME
);
CREATE TABLE applications (
id INT PRIMARY KEY,
job_id INT NOT NULL,
email VARCHAR(160) NOT NULL,
status VARCHAR(20) DEFAULT 'applied'
);
CREATE INDEX idx_applications_job ON applications (job_id);Common mistakes
The mistake is storing a list of tags in one text column as "python,java,sql". Searching it is guesswork and counting it is impossible. One row per tag in a job_tags table is the fix, and it is the difference between a demo and a database.
Worked example
CREATE TABLE jobs (id INTEGER PRIMARY KEY, title TEXT NOT NULL, company_id INTEGER REFERENCES companies(id)); โ the NOT NULL and REFERENCES are what keep bad data out.
Your practice task
Design three tables for your own project, create them in SQLite, insert ten realistic rows and query them back.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
A schema is the shape of your data: tables, the columns in each, the type of every column, and which column links two tables.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task โ do them and the final project brief writes itself.