Updating data with UPDATE

Update data in the table using the UPDATE operator:

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. UPDATE episodes
  2. SET title="test Episode 2"
  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. You can't UPDATE or DELETE a table
  13. -- already 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_update = (
  17. SELECT series_id,
  18. season_id,
  19. episode_id,
  20. Utf8("Yesterday's Jam UPDATED") AS title
  21. FROM episodes
  22. WHERE series_id = 1 AND season_id = 1 AND episode_id = 1
  23. );
  24. SELECT * FROM episodes WHERE series_id = 1 AND season_id = 1;
  25. UPDATE episodes ON
  26. SELECT * FROM $to_update;
  27. COMMIT;
  28. -- View result:
  29. SELECT * FROM episodes WHERE series_id = 1 AND season_id = 1;
  30. COMMIT;

Updating data with UPDATE - 图1