Click to See Complete Forum and Search --> : Resetting A Primary Key


kevinguill
01-21-2007, 05:13 PM
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

NightShift58
01-21-2007, 06:53 PM
MySQL:ALTER TABLE `myTable` AUTO_INCREMENT=1

russell
01-21-2007, 08:06 PM
and for MSSQL

DBCC CHECKIDENT ('table name', RESEED, newSeedValue)


and for either mysql or mssql
Truncate Table tableName

NightShift58
01-21-2007, 08:48 PM
Truncate Table tableNameTruncate 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...

chazzy
01-21-2007, 08:51 PM
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.

NightShift58
01-21-2007, 09:45 PM
Except that he already said the table's emptyWhat he said/wrote was: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...

russell
01-21-2007, 09:59 PM
agreed. truncate should be used verrrrry carefully. it is also important to know that it is not logged (in sql server) so there is no undo, period.

in this case i was assuming empty table from OP

NightShift58
01-21-2007, 10:11 PM
in this case i was assuming empty table from OPIn 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.

russell
01-21-2007, 10:37 PM
LOL :) didn't think u were trying to punish me. all good brother... :cool: and u raised an important point.

cheers
russell

kevinguill
01-21-2007, 11:47 PM
yes the table was completely empty. i decided to use this and is exactly what i needed thank again NightShift58


ALTER TABLE `myTable` AUTO_INCREMENT=1

NightShift58
01-22-2007, 12:25 AM
You're welcome!