Rullst AcademyRullst Academy/Lesson
Databases and SQL I · Lesson 06 / 12

Change without losing the target

Predict UPDATE and DELETE scope, test and undo a trial.

Learning paths16 min

Let's discover together

  1. 1

    Before changing data, SELECT with the same condition and check ids and count. label = Orbit finds two rows; id = 1 identifies one. Choose targets by the problem, not the first screen row.

  2. 2

    UPDATE changes columns; DELETE removes rows. Without WHERE they may affect the entire table. DELETE does not remove the table definition: DROP TABLE is different. Practice only on the fictional database.

  3. 3

    The example starts BEGIN, changes id 1, queries and uses ROLLBACK. The final query shows the old value. COMMIT requires checking results; real operations also need appropriate recovery.

  4. 4

    To practice DELETE, insert a new record without loans and remove only its id. The FK prevents deleting materials with dependent rows, including returned loans. Test and explain the error.

Your data workbench

Our model: kits.id ← loans.kit_id. One material can have many loans; names may repeat. We use fictional data only.

Predict before opening the reference. It shows verified output from the original example, not execution of your text. Run your changes with the kit on your computer.

-- Change without losing the target
BEGIN;
SELECT id, stock FROM kits WHERE id=1;
UPDATE kits SET stock=4 WHERE id=1;
SELECT id, stock FROM kits WHERE id=1;
ROLLBACK;
SELECT id, stock FROM kits WHERE id=1;
Compare with reference output
[
  {
    "columns": [
      "id",
      "stock"
    ],
    "rows": [
      [
        1,
        3
      ]
    ]
  },
  {
    "columns": [
      "id",
      "stock"
    ],
    "rows": [
      [
        1,
        4
      ]
    ]
  },
  {
    "columns": [
      "id",
      "stock"
    ],
    "rows": [
      [
        1,
        3
      ]
    ]
  }
]

Run the command in the extracted folder’s terminal. Use python3 on macOS/Ubuntu or py if that is your Windows launcher. Every run starts in memory; read README.md for saving files and recovery practice.

python lab.py run lesson.sql
My files and GitHub

The editor saves your .sql draft in this browser. Download it and replace lesson.sql in the kit; for the final project, availability.sql. The ZIP remains the original template. GitHub publishing sends only the confirmed file, without executing SQL or sending a database.

Official links and next step

✦
YOUR MISSION

Predict UPDATE and DELETE scope, test and undo a trial.

Now it's your turn

Discovery workshop

Read, predict and check

Predict UPDATE and DELETE scope, test and undo a trial.
Build your answers and check when you are ready.
A moment to understand

How do you check the target before updating?

I need a hint

UPDATE changes columns; DELETE removes rows. Without WHERE they may affect the entire table. DELETE does not remove the table definition: DROP TABLE is different. Practice only on the fictional database.

Loading your journey…

Take it with you

Change id 4 in a transaction, query before/after and roll back. Explain why label can target a different set.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.