Skip to content
ohsudo, Home

Changing data: INSERT, UPDATE, DELETE and transactions

Queries so far only looked. These change things, and on a real server, a missing WHERE is how people lose data. Here it costs nothing: Start again brings the data back.

● A real database, running in your browser. Nothing you type leaves this page.

Exercise 1 of 6

Add a row

INSERT adds rows. Name the columns, and let the database choose the id.

Your task

Add a department named Legal, located in Zurich.

Tables and columns
    or Ctrl+Enter

    What you'll practice

    1. Add a row. INSERT adds rows. Name the columns, and let the database choose the id.
    2. Change one row. UPDATE changes rows, and without `WHERE`, it changes all of them. Run a SELECT with the same WHERE first, when it matters.
    3. Change several. The same statement changes every row that matches.
    4. Delete some rows. DELETE removes rows, and as with UPDATE, forgetting `WHERE` empties the table.
    5. The database says no. A foreign key stops you from deleting a row that others depend on, so no employee is left pointing at a department that's gone. This error is a good thing.
    6. Undo with a transaction. Between BEGIN and COMMIT, changes aren't final, and ROLLBACK cancels them all. It's how you stay safe when a change matters, such as when moving money between two rows.

    See also