So far every query has only read data. Three statements change it: INSERT adds new rows, UPDATE changes existing ones, and DELETE removes them. Together with SELECT they make up the everyday “CRUD” operations (create, read, update, delete). They are powerful, and one missing WHERE can change every row in a table, so this lesson also covers how to use them safely.
In this lesson you will learn
- How to insert one row or several at once
- What happens to columns you leave out
- How to update rows with a
WHEREcondition - How to delete rows safely
- How transactions let you undo mistakes
INSERT: Adding Rows
INSERT INTO students (id, name, city, dept, cgpa)
VALUES (9, 'Sumaiya', 'Barishal', 'EEE', 3.71);
INSERT INTO students (id, name, city, dept) VALUES
(10, 'Kamal', 'Dhaka', 'BBA'),
(11, 'Lina', 'Sylhet', 'CSE');
SELECT id, name, city, dept, cgpa FROM students WHERE id >= 9;
Output
+----+---------+----------+------+------+
| id | name | city | dept | cgpa |
+----+---------+----------+------+------+
| 9 | Sumaiya | Barishal | EEE | 3.71 |
| 10 | Kamal | Dhaka | BBA | NULL |
| 11 | Lina | Sylhet | CSE | NULL |
+----+---------+----------+------+------+
(3 rows)
- List the columns, then the matching
VALUESin the same order. - One
INSERTcan add several rows, separated by commas. - Columns you leave out get their default value, or
NULLif there is none. Kamal and Lina have no CGPA yet. - Always name the columns.
INSERT INTO students VALUES (...)without them breaks as soon as someone adds a column to the table.
UPDATE and DELETE
-- Fix one record
UPDATE students SET cgpa = 3.60 WHERE name = 'Nadia';
-- Give every BBA student a 0.05 bonus
UPDATE students SET cgpa = ROUND(cgpa + 0.05, 2) WHERE dept = 'BBA';
-- Remove one record
DELETE FROM students WHERE id = 6;
SELECT id, name, dept, cgpa FROM students ORDER BY id;
Output
+----+--------+------+------+
| id | name | dept | cgpa |
+----+--------+------+------+
| 1 | Nusrat | CSE | 3.82 |
| 2 | Arif | EEE | 3.41 |
| 3 | Tania | CSE | 3.95 |
| 4 | Imran | BBA | 3.15 |
| 5 | Mitu | CSE | 3.67 |
| 7 | Sabbir | BBA | 3.6 |
| 8 | Nadia | CSE | 3.6 |
+----+--------+------+------+
(7 rows)
UPDATE table SET column = value WHERE conditionchanges only matching rows. Nadia now has a CGPA.- The new value can use the old one:
cgpa = ROUND(cgpa + 0.05, 2)raised both BBA students by 0.05. Rounding avoids tiny floating-point leftovers like 3.5999999. DELETE FROM table WHERE conditionremoves matching rows. Rafi (id 6) is gone.
Never run UPDATE or DELETE without WHERE by accident
DELETE FROM students; empties the whole table, and UPDATE students SET cgpa = 0; overwrites every row. Before changing data, run a SELECT with the same WHERE to see exactly which rows will be affected.
Undo with Transactions
A transaction groups changes so they either all happen or none do:
BEGIN;
DELETE FROM students WHERE dept = 'BBA';
-- check the result, then either:
ROLLBACK; -- undo everything since BEGIN
-- or COMMIT; to make the changes permanent
Many database tools start in “autocommit” mode, where every statement is saved immediately, so start a transaction yourself before risky changes.
Common Mistakes
- Forgetting
WHEREonUPDATEorDELETE. - Inserting a duplicate primary key, which fails with a “UNIQUE constraint failed” error.
- Values in the wrong order when the column list is left out.
- Building SQL by joining user input into strings, which allows SQL injection. Use parameterised queries in application code.
Practice Exercises
Exercise 1. Move Mitu (id 5) to Dhaka.
Show answer
UPDATE students SET city = 'Dhaka' WHERE id = 5;
Exercise 2. Delete every student with no CGPA recorded.
Show answer
DELETE FROM students WHERE cgpa IS NULL;
Frequently Asked Questions
What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes rows (all or some) and can be rolled back. TRUNCATE quickly removes all rows but keeps the table (not available in SQLite). DROP TABLE removes the table itself, structure and all.
Can I copy rows from one table into another?
Yes: INSERT INTO archive (id, name) SELECT id, name FROM students WHERE dept = 'BBA';
Summary
INSERT INTO t (cols) VALUES (...)adds rows; missing columns become default orNULL.UPDATE t SET col = value WHERE ...changes matching rows.DELETE FROM t WHERE ...removes matching rows.- Check with
SELECTfirst, and use transactions for risky changes.
Next: SQL Aggregate Functions.
