⚡0 XP🔥0

INSERT, UPDATE, DELETE and Transactions

Lesson 8 of 10 ⏱ 5 min read ✎ 3 exercises ❓ 4-question quiz ▶ Runs SQL

Add rows with INSERT (single, multiple, INSERT ... SELECT), change data with UPDATE, remove rows with DELETE, upserts, and keep data consistent with transactions and ACID.

DML (Data Manipulation Language) changes the data inside tables: INSERT, UPDATE, DELETE. Every try-it below starts from a fresh copy of the college database, so experiment freely.

INSERT - add rows#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

Columns you leave out get their DEFAULT value, or NULL.

Common Mistake

INSERT INTO table VALUES (...) without a column list depends on the exact column order of the table - it breaks silently if someone adds or reorders columns. Name the columns in application code.

INSERT ... SELECT#

Insert the result of a query - useful for archiving or building summary tables:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

UPDATE - change existing rows#

UPDATE table SET column1 = value1, column2 = value2 WHERE condition;
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
हिंदी में समझें

UPDATE से मौजूदा data बदलते हैं और DELETE से rows हटाते हैं। WHERE लिखना कभी न भूलें - बिना WHERE के UPDATE पूरी table की हर row बदल देगा और DELETE पूरी table खाली कर देगा। Safe तरीका: पहले उसी WHERE के साथ SELECT चलाकर देखें कि कौन-सी rows प्रभावित होंगी।

UPDATE with a subquery#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

DELETE - remove rows#

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Tip

Professional habit: before every UPDATE or DELETE, run a SELECT with the same WHERE clause and check the rows. Many companies also use soft deletes - an is_active or deleted_at column - instead of really deleting data.

Upsert - insert or update#

When a row may already exist, an upsert inserts it or updates the existing one:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

This syntax works in SQLite and PostgreSQL. MySQL uses INSERT ... ON DUPLICATE KEY UPDATE, SQL Server and Oracle use MERGE.

Transactions#

A transaction groups several statements into one unit of work: either all of them take effect, or none do. The classic example is a fee transfer: debit one account, credit another - both or neither.

BEGIN TRANSACTION;
    UPDATE accounts SET balance = balance - 5000 WHERE id = 1;
    UPDATE accounts SET balance = balance + 5000 WHERE id = 2;
COMMIT;        -- make it permanent
-- or ROLLBACK;  -- undo everything since BEGIN
▶ Try it YourselfSQL
Ctrl + Enter to run

ACID - what transactions guarantee#

Property Meaning
Atomicity all-or-nothing: a failure in the middle undoes the whole transaction
Consistency the database moves from one valid state to another (constraints always hold)
Isolation concurrent transactions do not see each other's half-finished work
Durability once committed, changes survive crashes and power cuts

SAVEPOINT name and ROLLBACK TO name let you undo just part of a transaction.

Summary#

  • INSERT INTO t (cols) VALUES (...), several rows at once, or INSERT ... SELECT.
  • UPDATE t SET ... WHERE ... and DELETE FROM t WHERE ... - always check the WHERE first.
  • Upserts (ON CONFLICT ... DO UPDATE) insert or update.
  • Transactions (BEGIN → COMMIT/ROLLBACK) make multi-step changes all-or-nothing (ACID).

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Admit a new student

Insert student 131, Ishaan Rathore, gender M, city Indore, dept 6, year 1, cgpa 8.1, email ishaan.rathore@vindhya.edu, admission date 2026-07-10, fee 52000. Then show student_id, name, dept_id of students in dept 6.

Database: college (see the tables in this lesson)
2

Grace marks

In enrollments, add 5 grace marks to every enrollment of course CE101 with marks below 40, then show enroll_id, marks for course CE101 ordered by enroll_id.

Database: college (see the tables in this lesson)
3

Remove failed enrollments

Delete every enrollment with grade F, then show the number of remaining enrollments as remaining.

Database: college (see the tables in this lesson)

Quick Quiz

Q1.

What happens if you run UPDATE students SET cgpa = 10; without WHERE?

Q2.

What does ROLLBACK do?

Q3.

The "A" in ACID stands for...

Q4.

Which statement copies rows from one table into another?