1

I have single use keys stored in a table column. I have a table which contains only unique keys which, once read and sent, must be deleted from table using an atomic operation.

How can I accomplish this?

I need to use Node.js in asynchronous way; it is cumbersome to run two queries(select, and delete) for each request using transactions.

I wish that Delete returned the key as well... that'd have been easiest.

MySQL version: 5.5.54

TylerH
  • 20,799
  • 66
  • 75
  • 101
user5858
  • 1,082
  • 4
  • 39
  • 79

2 Answers2

1

Write a Stored Procedure to do the SELECT .. FOR UPDATE and DELETE in a single transaction and returning the SELECT results. Then perform a single CALL from your client.

Rick James
  • 135,179
  • 13
  • 127
  • 222
0

This is a possible duplicate of MySQL update output deleted which has no answer either.
The thing is that MySQL has not implemented such syntax yet. It is available in other DBs, but in MySQL you are most likely stuck with SELECT .. FOR UPDATE; DELETE ... inside of a transaction. I am afraid that in MySQL you might not have any better option yet.

Some other links:

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If you find that any of the links I provided are either answering your question or this is a direct duplicate of any of them we can close this question as a duplicate instead. – Dharman Aug 07 '19 at 16:53