Deleting data

Delete data from the table using DELETE.

Note

We assume that you already created tables in step Creating a table and populated them with data in step Adding data to a table.

  1. DELETE
  2. FROM episodes
  3. WHERE
  4. series_id = 2
  5. AND season_id = 5
  6. AND episode_id = 12
  7. ;
  8. COMMIT;
  9. -- View result:
  10. SELECT * FROM episodes WHERE series_id = 2 AND season_id = 5;
  11. -- YDB doesn't see changes that take place at the start of the transaction,
  12. -- which is why it first performs a read. It is impossible to execute UPDATE or DELETE on
  13. -- if the table was changed within the current transaction. UPDATE ON and
  14. -- DELETE ON let you read, update, and delete multiple rows from one table
  15. -- within a single transaction.
  16. $to_delete = (
  17. SELECT series_id, season_id, episode_id
  18. FROM episodes
  19. WHERE series_id = 1 AND season_id = 1 AND episode_id = 2
  20. );
  21. SELECT * FROM episodes WHERE series_id = 1 AND season_id = 1;
  22. DELETE FROM episodes ON
  23. SELECT * FROM $to_delete;
  24. COMMIT;
  25. -- View result:
  26. SELECT * FROM episodes WHERE series_id = 1 AND season_id = 1;
  27. COMMIT;

Deleting data - 图1