Lab 09 — INSERT, UPDATE, DELETE & CREATE
Not just reading data — writing it. Create tables, insert rows, update records, and safely delete data. The database resets on every page reload so experiment freely!
Concept Recap: DDL & DML
DDL (Data Definition Language) — defines structure:
CREATE TABLE name (col type, ...)— create a new tableCREATE TABLE IF NOT EXISTS— safe version that won't error if it existsDROP TABLE name— delete a table and all its data- Column constraints:
PRIMARY KEY,AUTOINCREMENT,NOT NULL,DEFAULT value,UNIQUE
DML (Data Manipulation Language) — modifies data:
INSERT INTO table (col1, col2) VALUES (v1, v2)— add a rowINSERT INTO table VALUES (v1, v2, ...)— insert with all columns in orderUPDATE table SET col = val WHERE condition— modify existing rowsDELETE FROM table WHERE condition— remove rows- Always use WHERE with UPDATE and DELETE — omitting it affects every row!
Worked Example (read-only)
Create a table, insert rows, update a price, and verify the result.
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL,
stock INTEGER DEFAULT 0
);
INSERT INTO products (name, price, stock) VALUES ('Laptop', 999.99, 50);
INSERT INTO products (name, price, stock) VALUES ('Mouse', 29.99, 200);
UPDATE products SET price = 899.99 WHERE name = 'Laptop';
SELECT * FROM products;Exercise 1 — Your First INSERT
Insert 3 students into the students table. Fill in the VALUES for students 2 and 3, then run a SELECT to verify they were added.
Exercise 2 — INSERT, UPDATE, and DELETE a Full Workflow
Build up the table step by step. Run each block individually to see changes. Do the following in sequence:
- Insert 5 students (mix of majors, GPAs 2.8–3.9, years 1–4).
- Update Alice's GPA to 3.95 (use WHERE name = 'Alice Chen').
- Update all 'English' major students to year 2.
- Delete all students with GPA below 3.0.
- Run SELECT * to verify final state.
Exercise 3 — Build Two Related Tables and JOIN Them
Create a courses table and an enrollments table. Insert data, then write a JOIN query to show which students are enrolled in which courses.
Mini Project — Full Student Registration System
Build a complete student registration system from scratch in a single editor. The database starts fresh every page reload, so you can run the whole block at once.
- INSERT a full class of 6 students.
- UPDATE 2 students — one GPA correction, one major change.
- DELETE 1 student who withdrew.
- CREATE a grades table:
grades(id, student_id, subject, score, grade_letter). - INSERT grades for 4 remaining students across at least 2 subjects each.
- Final SELECT: join students with grades to show complete academic status.
Lab 09 Complete!
You can now create tables with constraints, insert rows, update specific records, and safely delete data. You also know why WHERE is non-negotiable with UPDATE and DELETE.
One lab to go — the capstone. No fill-in-the-blanks. Real analyst questions.
Continue to Lab 10 — Capstone →