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
- Add a row.
INSERTadds rows. Name the columns, and let the database choose theid. - Change one row.
UPDATEchanges rows, and without `WHERE`, it changes all of them. Run aSELECTwith the sameWHEREfirst, when it matters. - Change several. The same statement changes every row that matches.
- Delete some rows.
DELETEremoves rows, and as withUPDATE, forgetting `WHERE` empties the table. - 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.
- Undo with a transaction. Between
BEGINandCOMMIT, changes aren't final, andROLLBACKcancels them all. It's how you stay safe when a change matters, such as when moving money between two rows.