Modifying Data with INSERT, UPDATE, DELETE
So far you've practiced reading from the University database. Now it's time to change the data. We'll add new students and courses, update grades, and remove outdated rows while protecting the integrity of our relationships.
Learning Objectives
- Insert rows into
department,student,course, andenrollment - Update existing rows safely with targeted
WHEREclauses - Delete records while respecting foreign-key constraints
- Apply best practices like transactions and pre-check queries
Inserting Data
INSERT adds brand-new rows. Always list the columns explicitly so your statements survive schema tweaks.
INSERT INTO department (dept_id, dept_name)
VALUES (7, 'Data Science');
INSERT INTO student (student_id, first_name, last_name, email, dept_id, enrollment_year)
VALUES (13, 'Leena', 'Roy', 'leena.roy@students.local', 7, 2025);
Multiple rows can be inserted in a single statement:
INSERT INTO course (course_id, course_code, course_title, credits, dept_id, instructor_id) VALUES
(12, 'DS201', 'Intro to Data Science', 3, 7, NULL),
(13, 'DS310', 'Statistical Modeling', 4, 7, NULL);
If your database auto-generates IDs (e.g., INTEGER PRIMARY KEY in SQLite), omit those columns and let the engine supply the values.
Insert from a SELECT
Populate a table by pulling data from an existing one.
CREATE TABLE cs_advising (
student_id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT
);
INSERT INTO cs_advising (student_id, first_name, last_name)
SELECT student_id, first_name, last_name
FROM student
WHERE dept_id = 1;
Updating Data
UPDATE changes existing rows. The WHERE clause is mandatory unless you truly intend to alter every row.
UPDATE course
SET instructor_id = 2
WHERE course_code = 'DS201';
UPDATE enrollment
SET grade = 'A'
WHERE course_id = 7 -- Algorithms
AND semester = 'Spring 2025'
AND grade IN ('A-', 'B+');
To review affected rows before committing, run the same filter with SELECT:
SELECT *
FROM enrollment
WHERE course_id = 7
AND semester = 'Spring 2025'
AND grade IN ('A-', 'B+');
Deleting Data
DELETE removes rows. Use it with precision; foreign-key constraints may block deletes when dependent rows exist.
DELETE FROM enrollment
WHERE enroll_id = 5;
If you need to purge records for an entire semester:
DELETE FROM enrollment
WHERE semester = 'Fall 2024'
AND grade IS NULL;
To remove a course entirely, delete dependent enrollments first or define cascading rules:
BEGIN TRANSACTION;
DELETE FROM enrollment WHERE course_id = 12;
DELETE FROM course WHERE course_id = 12;
COMMIT;
Best Practices
- Always test with
SELECT: Preview the rows that match yourWHEREclause. - Use transactions: Wrap related
INSERT,UPDATE, orDELETEstatements in a transaction so you can roll back if needed. - Respect constraints: Inserting or updating with invalid foreign keys (e.g., a non-existent
dept_id) will fail. - Default values: Rely on default settings (like
creditsdefaulting to 3) when appropriate instead of hard-coding them everywhere. - Logging: In production systems, audit critical changes so you can trace who altered what.
Summary
INSERTadds data; specify columns for clarity and resilience.UPDATEchanges data; a narrowWHEREclause keeps modifications safe.DELETEremoves data; understand dependencies before you run it.- Transactions and preflight
SELECTqueries protect you from mistakes.
Quiz
Show quiz
-
Which clause ensures an
UPDATEaffects only intended rows?
A)SET
B)FROM
C)WHERE
D)VALUES -
How can you add two new courses in a single statement?
A) Run two separateINSERTcommands
B) UseINSERT ... VALUES (...), (...);
C) UseUPDATEwith multiple values
D) Modify the table default -
True or False: You can delete a course that still has enrollment records without additional steps.
A) True
B) False -
Why should you wrap related modifications in a transaction?
A) It speeds up queries
B) It allows rolling back if any statement fails
C) It automatically creates indexes
D) It bypasses foreign-key checks
Answers
- C
- B
- B
- B