MariaDB [test]> create table codes (id serial, used_at datetime);
...........
MariaDB [test]> select * from codes;
+----+---------+
| id | used_at |
+----+---------+
| 1 | NULL |
+----+---------+
1 2
----------------------------------------------- -----------------------------------------------
> begin;
Query OK, 0 rows affected (0.00 sec)
> begin;
Query OK, 0 rows affected (0.00 sec)
> update codes
set used_at = now()
where id = 1 and used_at is null;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
> update codes set used_at = now() where id = 1 and used_at is null;
# ждем-с. если не дождались - lock timeout, опа, rollback
> commit;
Query OK
> Query OK, 0 rows affected (3.41 sec)
Rows matched: 0 Changed: 0 Warnings: 0
# смотрим affected_rows, 0, опа, rollback.