Without referencing the SERIAL id.
Something like:
delete from users LAST 3
which would delete last 3 rows from the table.
The PostgreSQL DELETE statement allows you to delete one or more rows from a table. First, specify the name of the table from which you want to delete data after the DELETE FROM keywords. Second, use a condition in the WHERE clause to specify which rows from the table to delete. The WHERE clause is optional.
There could be thousands, potentially millions of rows to delete. I have a file of primary keys, one per line. The two options I was thinking of were along the lines of the below, but I don't know/understand enough of the internals of PostgreSQL to make an informed decision which would be best.
In DELETE query, you can also use clauses like WHERE, LIKE, IN, NOT IN, etc., to select the rows for which the DELETE operation will be performed. The use of WHERE clause is optional. If you do not provide any selection criteria for the rows, then all the rows of the table would be deleted.
Summary: 1 The DELETE statement is used for deleting one or more records from a table. 2 To delete only select rows from a table, you can combine the DELETE statement with the WHERE clause. 3 If the DELETE clause is used without the WHERE clause, it deletes all records from the table. More items...
This will work :
DELETE
FROM users
WHERE id in (
SELECT id
FROM users
ORDER BY id desc
LIMIT 3
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With