UNIHIVE

Study Hub UNIPORT CSC 282

CSC 282 · Database Programming

SQL: inserting, updating and deleting, and transactions

INSERT, UPDATE and DELETE, NULLs, and transactions with COMMIT and ROLLBACK.

Our notes

2 min read

INSERT

    INSERT INTO department (dept_code, dept_name)
    VALUES ('CSC', 'Computer Science');

    INSERT INTO student (matric_no, surname, other_names, dept_code, level)
    VALUES ('U2024/5570001', 'Okoro', 'Ada', 'CSC', 200),
           ('U2024/5570002', 'Bello', 'Musa', 'CSC', 200);

List the columns: the statement keeps working if a column is added later. A row that breaks a constraint (duplicate key, missing department, score 120) is refused.

UPDATE

    UPDATE student SET level = 300
    WHERE matric_no = 'U2024/5570001';

    UPDATE result SET score = score + 2
    WHERE course_code = 'STA 260' AND session = '2025/2026';

Without WHERE, every row changes. Run the matching SELECT first to see which rows will be affected.

DELETE

    DELETE FROM result
    WHERE course_code = 'CSC 282' AND session = '2019/2020';

Without WHERE, every row goes (the table stays). A delete that would break referential integrity is refused unless the foreign key says ON DELETE CASCADE.

NULL

NULL means "unknown or not applicable", not zero or empty text.

  • Test with IS NULL / IS NOT NULL (score = NULL is never true).
  • Aggregates ignore NULLs: AVG over 60, 80 and NULL is 70, but COUNT(*) is 3 while COUNT(score) is 2.

Transactions

A transaction is a group of statements that must succeed or fail together. Moving ₦5,000 between two wallets:

    START TRANSACTION;
    UPDATE wallet SET balance = balance - 5000 WHERE owner = 'A';
    UPDATE wallet SET balance = balance + 5000 WHERE owner = 'B';
    COMMIT;

If anything fails, ROLLBACK undoes both updates.

ACID

  • Atomicity: all or nothing.
  • Consistency: the database moves from one valid state to another (rules hold).
  • Isolation: concurrent transactions do not see each other's half-finished work.
  • Durability: once committed, changes survive a crash.

Concurrency problems a DBMS prevents

  • Lost update: two clerks read balance 10,000, both add a payment, the second write overwrites the first.
  • Dirty read: reading another transaction's uncommitted change that is later rolled back.

DBMSs use locks or row versions to keep transactions isolated.

Exams usually ask

  • Write INSERT, UPDATE and DELETE for a scenario.
  • Explain a transaction and the ACID properties with a bank example.
  • Explain NULL and its effect on aggregates.

Common mistake

Writing WHERE score = NULL. Use WHERE score IS NULL.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 282 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.