How can I manage a FIFO-queue in an database with SQL? How can I manage a FIFO-queue in an database with SQL? database database

How can I manage a FIFO-queue in an database with SQL?


Reading the comments you say that you are willing to add a auto increment or date field to know the proper position of each row. Once you add this I would recommend adding one more row to the In table called processed which is automatically set to false when the row is added to the table. Any rows that have been copied to OUT already have their processed filed set to true.

+----+| In |+-----------+-----------+-------+-----------+| AUtoId    | Supply_ID | Price | Processed |+-----------+-----------+-------+-----------+|     1     |     1     |  75   |     1     ||     2     |     1     |  75   |     1     ||     3     |     1     |  75   |     0     ||     4     |     2     |  80   |     0     ||     5     |     2     |  80   |     0     |+-----------+-----------+-------+---------- +

Then to find the next item to move to OUT you can do

SELECT TOP 1 Supply_ID, Price FROM In WHERE Processed = 0ORDER BY [Your Auto Increment Field or Date]

Once the row is moved to OUT then you just UPDATE the processed field of that row to true.


I can't see a simple query that would help here. To simulate a FIFO in SQL I would look to three tables, OPERATION, OUT and FIFO. OPERATION is effectively a log of the transactions and the FIFO table is the FIFO state and OUT is the responses from the FIFO.

You would update CURRENT with the operations (adding and removing items) as they come into the OPERATION table and process requests for item 'out's into the OUT table decrementing the values in the FIFO and where necessary removing the records from the FIFO table.

Even then I don't see a simply query to process the whole thing as there's a need to query the first record to see if there is sufficient quantity for each operation, updating of that record appropriately and querying of additional records where the operation cannot be fulfilled. My level of SQL ability doesn't lead me to a simple solution to that, it constitutes business logic to me and gets lifted into that tier.