Ok so have deleted previous entries in the database but know when something new is entered into the database the primary key logs it has entry number 46. i want it to log as number 1. can i reset the primary keys to so new entries start at 1
Truncate would be ideal on an empty table but it could be a radical move for anything else, tantamount to decapitation to cure a headache...
If the table is not empty, the primary intention for using TRUNCATE should be emptying the table and not just resetting the autoincrement counter.
If the table is empty, the "ALTER TABLE...AUTO_INCREMENT=1" (or its MSSQL equivalent) is equally efficient, except that if data is present, it will simply the reset the autoincrement counter to the highest ID + 1.
My concern with "TRUNCATE" is that it has the potential to be very destructive... It should be avoided for such "mundane" tasks and used with care...
Truncate would be ideal on an empty table but it could be a radical move for anything else, tantamount to decapitation to cure a headache...
If the table is not empty, the primary intention for using TRUNCATE should be emptying the table and not just resetting the autoincrement counter.
If the table is empty, the "ALTER TABLE...AUTO_INCREMENT=1" (or its MSSQL equivalent) is equally efficient, except that if data is present, it will simply the reset the autoincrement counter to the highest ID + 1.
My concern with "TRUNCATE" is that it has the potential to be very destructive... It should be avoided for such "mundane" tasks and used with care...
Except that he already said the table's empty. Although in mysql, truncating the table doesn't always reset the key, based on which engine is in use.
Ok so have deleted previous entries in the database
... which could mean that the table is empty or partially empty, depending on what "previous entries" means. Had he said "all previous entries" then I would agree that the table would be empty.
What he didn't explicitly say was that the table was empty.
But that is really besides the point. I think that using TRUNCATE is potentially dangerous, especially in a script. I prefer the "ALTER TABLE" variety but, granted, it's a personal choice. There's more than one way to skin a cat...
In all honesty, so did I because he wanted to reset the autoincrement to 1, which would only make sense within the context of an empty table but... in these forums, assumptions are often "punished" harshly by the readership, so - shame on me - I took the easy road. Or so I thought...
My intention, though, was not to start a fiery discussion on whether the table is empty or not - which is probably irrelevant to the OP - but simply to suggest that, given two options, choose the one that is potentially less destructive.
Bookmarks