Delete a line at a specific line number in Postgres

I am creating an application in which I need to delete a table row with a specific row number. I don’t want to use or rely on the identifier, because if I delete the line, the following lines will not “shift” - line 8 today may be line 7 tomorrow, but line 8 will still have identifier 8.

How to write postgres SQL that essentially does this:

DELETE FROM Table
WHERE <row_number> = n;

And row_number is not a real attribute.

+3
source share
4 answers

; , "" " "? , ORDER BY , . , ... ? - , , .

, , , 8.4, , , . , .

+1

, , OID:

DELETE FROM Table WHERE oid = n;

. .

0

Not quite the row number of the table as such, but should do the job you want. Perhaps a little more flexible, because you can do the deletion by date or by order. I am going to use this for a recent list.

DELETE 
FROM table 
WHERE pk IN 
    (
        SELECT pk 
        FROM  table 
        ORDER BY pk OFFSET n
    )
0
source

I would go for:

DELETE FROM Table OFFSET <row_number> limit 1 order by id
-1
source

Source: https://habr.com/ru/post/1712616/


All Articles