SQL statement to empty a table

Well, you could do two things, always from the SQL language. I indicate the syntax for a MySQL database, although it will not be very different for other database management systems.

First a statement that deletes all data from a table:

delete from tablename where 1

This removes all data from the table, simply deleting all the records.

Another possibility is:

truncate tablename

This empties the table and the effect is similar to doing a delete on all records. The difference between delete and truncate is that truncate initializes everything that was in the table.

For example, let’s imagine that we have a users table and that we have up to 100 users and an autonumber field called user_id. If we were going for user 100, a possible new user would be the one inserted as id 101.

Let’s imagine we’re at the point where we had 100 users and then we do a delete of all the records, with the delete statement. Then all the records will be deleted, but if we insert a new record, it will be inserted with the id 101.

Now let’s imagine we’re at the same point where we have 100 users and then we do a truncate instead of a delete and delete all the records. So if we insert a new record when the table has been emptied with a truncate, the record will come in with id 1.

Loading Facebook Comments ...
Loading Disqus Comments ...