Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to delete last n records from table in sql when there is no primary key column in table.Please help me in this query

What I have tried:

I know how to delete last n records with primary key but without primary key i was not able to delete.
Posted
Updated 30-Mar-16 22:47pm
Comments
phil.o 31-Mar-16 3:12am    
Please describe your table and what you actually tried.
Patrice T 31-Mar-16 4:36am    
The OP gave you an answer below.
phil.o 31-Mar-16 4:47am    
Thanks for the notification, I would not have seen it otherwise.
Patrice T 31-Mar-16 4:48am    
you welcome.
xpertzgurtej 31-Mar-16 3:20am    
Suppose the table has Only 3 fields . FirstName, MiddleName and LastName. And there are 1000 records and i want to delete last 10 records. Then how to delete them

1 solution

Quote:
Suppose the table has Only 3 fields . FirstName, MiddleName and LastName. And there are 1000 records and i want to delete last 10 records. Then how to delete them

Then you will have to get a determinant for your "last 10 records" statement.
If you do not have any field than can discriminate the last record (from a sequence number, or a datetime for example), then you have to find some sort of criteria. From the informations you gave us, the only criteria I can imagine is an alphabetical one.
If you really need to delete the "last 10 inserted records", then you should include a column in your table that will serve as a discriminant.
If you do not have such a column, then you will only be able to sort on text fields. And if you do not sort the records before deleting the first 10 of them, then you will delete 10 random records.
Important thing here is you should have a field in your table that will be able to discriminate the "last 10 records".
Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900