Home > front end >  How can I delete n rows from a table using ctid?
How can I delete n rows from a table using ctid?

Time:03-06

I have a table with no primary key, just two columns with random varchar(100) values(cannot be sorted or used as a limit value). Is there a way to delete first n rows using ctid? I imagine something like:

DELETE FROM my_table 
Where {my_table.ctid < '(0,10000)'}

CodePudding user response:

DELETE FROM my_table WHERE ctid in
 (SELECT ctid FROM my_table ORDER BY ctid LIMIT n);

Because you can't rely ctid's to mean any order you might as well delete random n rows

DELETE FROM my_table WHERE ctid in
 (SELECT ctid FROM my_table ORDER BY random() LIMIT n);
  • Related