Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:32 AM
piper1970
 
Posts: n/a
Default resetting auto-incremented field to start at zero

I'm trying to get a primary key field to start the autoincrement at zero,
but seem stuck at 1. Is this possible?

After googling for webpages on this topic, I've seen many references to
using a 'delete from my_table' command, followed by a
'alter table my_table auto_increment=0'. However, in my implementation, the
next insert still produces a 1 for the auto-incremented field (primary
field)

While in mysql, I ran the following statements,

*********
mysql> create table test_table(
-> id int unsigned not null auto_increment,
-> value varchar(255),
-> right_now timestamp not null,
-> primary key(id));
Query OK, 0 rows affected (0.07 sec)

mysql> insert into test_table values (0,'steve', NOW());
Query OK, 1 row affected (0.03 sec)

mysql> select * from test_table;
+----+-------+---------------------+
| id | value | right_now |
+----+-------+---------------------+
| 1 | steve | 2008-01-22 22:07:05 |
+----+-------+---------------------+
1 row in set (0.00 sec)

mysql> delete from test_table;
Query OK, 1 row affected (0.00 sec)

mysql> alter table test_table auto_increment=0;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into test_table(value,right_now) values ('steve', NOW());
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_table;
+----+-------+---------------------+
| id | value | right_now |
+----+-------+---------------------+
| 1 | steve | 2008-01-22 22:12:14 |
+----+-------+---------------------+
1 row in set (0.00 sec)
***********

As you can see, the auto-increment still doesn't seem to work:

I tried again by adding 2 records, then deleting them, and altering, and
re-inserting. Same problem


*********
mysql> insert into test_table (value,right_now) values ('joe',NOW());
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_table;
+----+-------+---------------------+
| id | value | right_now |
+----+-------+---------------------+
| 1 | steve | 2008-01-22 22:12:14 |
| 2 | joe | 2008-01-22 22:24:46 |
+----+-------+---------------------+
2 rows in set (0.01 sec)

mysql> delete from test_table;
Query OK, 2 rows affected (0.00 sec)

mysql> alter table test_table auto_increment=0;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into test_table(value,right_now) values ('jack', NOW());
Query OK, 1 row affected (0.00 sec)

mysql> select * from test_table;
+----+-------+---------------------+
| id | value | right_now |
+----+-------+---------------------+
| 1 | jack | 2008-01-22 22:25:40 |
+----+-------+---------------------+
1 row in set (0.00 sec)
*********

When looking at the variables, it shows that auto_increment_increment, and
auto_increment_offset are both 1, even though I set the offset to zero in
the my.cnf file.

*******
# The MySQL server
[mysqld]
basedir="C:/xampp/mysql"
tmpdir="C:/xampp/tmp"
datadir="C:/xampp/mysql/data"

# Added here (01/21/08) to allow auto-increment to start at zero
auto_increment_offset = 0
***********



From the reference manual,
http://dev.mysql.com/doc/refman/5.0/...variables.html ,
it states that setting the auto_increment_increment or auto_increment_offset
to 0 effectively sets them to 1 instead. This suggests to me that the
auto-incremented value of zero is effectively off limits as an
auto-increment value. Is that correct?


How do I get the auto-increment to start at 0?

Thanks in advance to any help given.

piper


Here is my configuration

O.S. Windows XP
MySQL version 5.0.45-community-nt, packaged with XAMPP version 1.6.4


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:32 AM
Captain Paralytic
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On 23 Jan, 06:44, "piper1970" <some...@someone.com> wrote:
> I'm trying to get a primary key field to start the autoincrement at zero,
> but seem stuck at 1. Is this possible?
>
> After googling for webpages on this topic, I've seen many references to
> using a 'delete from my_table' command, followed by a
> 'alter table my_table auto_increment=0'. However, in my implementation, the
> next insert still produces a 1 for the auto-incremented field (primary
> field)
>
> While in mysql, I ran the following statements,
>
> *********
> mysql> create table test_table(
> -> id int unsigned not null auto_increment,
> -> value varchar(255),
> -> right_now timestamp not null,
> -> primary key(id));
> Query OK, 0 rows affected (0.07 sec)
>
> mysql> insert into test_table values (0,'steve', NOW());
> Query OK, 1 row affected (0.03 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:07:05 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
>
> mysql> delete from test_table;
> Query OK, 1 row affected (0.00 sec)
>
> mysql> alter table test_table auto_increment=0;
> Query OK, 0 rows affected (0.17 sec)
> Records: 0 Duplicates: 0 Warnings: 0
>
> mysql> insert into test_table(value,right_now) values ('steve', NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:12:14 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
> ***********
>
> As you can see, the auto-increment still doesn't seem to work:
>
> I tried again by adding 2 records, then deleting them, and altering, and
> re-inserting. Same problem
>
> *********
> mysql> insert into test_table (value,right_now) values ('joe',NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:12:14 |
> | 2 | joe | 2008-01-22 22:24:46 |
> +----+-------+---------------------+
> 2 rows in set (0.01 sec)
>
> mysql> delete from test_table;
> Query OK, 2 rows affected (0.00 sec)
>
> mysql> alter table test_table auto_increment=0;
> Query OK, 0 rows affected (0.13 sec)
> Records: 0 Duplicates: 0 Warnings: 0
>
> mysql> insert into test_table(value,right_now) values ('jack', NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | jack | 2008-01-22 22:25:40 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
> *********
>
> When looking at the variables, it shows that auto_increment_increment, and
> auto_increment_offset are both 1, even though I set the offset to zero in
> the my.cnf file.
>
> *******
> # The MySQL server
> [mysqld]
> basedir="C:/xampp/mysql"
> tmpdir="C:/xampp/tmp"
> datadir="C:/xampp/mysql/data"
>
> # Added here (01/21/08) to allow auto-increment to start at zero
> auto_increment_offset = 0
> ***********
>
> From the reference manual,http://dev.mysql.com/doc/refman/5.0/...variables.html,
> it states that setting the auto_increment_increment or auto_increment_offset
> to 0 effectively sets them to 1 instead. This suggests to me that the
> auto-incremented value of zero is effectively off limits as an
> auto-increment value. Is that correct?
>
> How do I get the auto-increment to start at 0?
>
> Thanks in advance to any help given.
>
> piper
>
> Here is my configuration
>
> O.S. Windows XP
> MySQL version 5.0.45-community-nt, packaged with XAMPP version 1.6.4


You need to put it in there specifically with the correct SQL mode
set, but it is not recommended
http://dev.mysql.com/doc/refman/5.0/...-sql-mode.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:32 AM
Willem Bogaerts
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

>> How do I get the auto-increment to start at 0?

A colleague of mine once did this. It will haunt you till the end of
times. It will be a real nuisance if you want to import such a table
into another database (like a developer's sandbox database, for
instance). So if you regard your colleagues, please don't.

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:32 AM
Captain Paralytic
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On 23 Jan, 10:09, Willem Bogaerts
<w.bogae...@kratz.maardanzonderditstuk.nl> wrote:
> >> How do I get the auto-increment to start at 0?

>
> A colleague of mine once did this. It will haunt you till the end of
> times. It will be a real nuisance if you want to import such a table
> into another database (like a developer's sandbox database, for
> instance). So if you regard your colleagues, please don't.
>
> Regards,
> --
> Willem Bogaerts
>
> Application smith
> Kratz B.V.http://www.kratz.nl/


As I said "it is not recommended". Since you were only quoting the the
previous post, it would have been beter to respond to that one.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:32 AM
Willem Bogaerts
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

> ... Since you were only quoting the the
> previous post, it would have been beter to respond to that one.


Yes, I saw that too late. Sorry!

Regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:32 AM
Alex Buell
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On Tue, 22 Jan 2008 22:44:32 -0800, I waved a wand and this message
magically appears in front of piper1970:

> I'm trying to get a primary key field to start the autoincrement at
> zero, but seem stuck at 1. Is this possible?


Short answer: DON'T DO THAT!
--
http://www.munted.org.uk

Fearsome grindings.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:32 AM
piper1970
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

thanks for the advice. will be disregarding attempt to use 0 as
auto-increment value.

piper


"piper1970" <someone@someone.com> wrote in message
news:B7SdncEIbtvMfwvanZ2dnUVZ_qKgnZ2d@comcast.com. ..
> I'm trying to get a primary key field to start the autoincrement at zero,
> but seem stuck at 1. Is this possible?
>
> After googling for webpages on this topic, I've seen many references to
> using a 'delete from my_table' command, followed by a
> 'alter table my_table auto_increment=0'. However, in my implementation,
> the next insert still produces a 1 for the auto-incremented field (primary
> field)
>
> While in mysql, I ran the following statements,
>
> *********
> mysql> create table test_table(
> -> id int unsigned not null auto_increment,
> -> value varchar(255),
> -> right_now timestamp not null,
> -> primary key(id));
> Query OK, 0 rows affected (0.07 sec)
>
> mysql> insert into test_table values (0,'steve', NOW());
> Query OK, 1 row affected (0.03 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:07:05 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
>
> mysql> delete from test_table;
> Query OK, 1 row affected (0.00 sec)
>
> mysql> alter table test_table auto_increment=0;
> Query OK, 0 rows affected (0.17 sec)
> Records: 0 Duplicates: 0 Warnings: 0
>
> mysql> insert into test_table(value,right_now) values ('steve', NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:12:14 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
> ***********
>
> As you can see, the auto-increment still doesn't seem to work:
>
> I tried again by adding 2 records, then deleting them, and altering, and
> re-inserting. Same problem
>
>
> *********
> mysql> insert into test_table (value,right_now) values ('joe',NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | steve | 2008-01-22 22:12:14 |
> | 2 | joe | 2008-01-22 22:24:46 |
> +----+-------+---------------------+
> 2 rows in set (0.01 sec)
>
> mysql> delete from test_table;
> Query OK, 2 rows affected (0.00 sec)
>
> mysql> alter table test_table auto_increment=0;
> Query OK, 0 rows affected (0.13 sec)
> Records: 0 Duplicates: 0 Warnings: 0
>
> mysql> insert into test_table(value,right_now) values ('jack', NOW());
> Query OK, 1 row affected (0.00 sec)
>
> mysql> select * from test_table;
> +----+-------+---------------------+
> | id | value | right_now |
> +----+-------+---------------------+
> | 1 | jack | 2008-01-22 22:25:40 |
> +----+-------+---------------------+
> 1 row in set (0.00 sec)
> *********
>
> When looking at the variables, it shows that auto_increment_increment, and
> auto_increment_offset are both 1, even though I set the offset to zero in
> the my.cnf file.
>
> *******
> # The MySQL server
> [mysqld]
> basedir="C:/xampp/mysql"
> tmpdir="C:/xampp/tmp"
> datadir="C:/xampp/mysql/data"
>
> # Added here (01/21/08) to allow auto-increment to start at zero
> auto_increment_offset = 0
> ***********
>
>
>
> From the reference manual,
> http://dev.mysql.com/doc/refman/5.0/...variables.html ,
> it states that setting the auto_increment_increment or
> auto_increment_offset to 0 effectively sets them to 1 instead. This
> suggests to me that the auto-incremented value of zero is effectively off
> limits as an auto-increment value. Is that correct?
>
>
> How do I get the auto-increment to start at 0?
>
> Thanks in advance to any help given.
>
> piper
>
>
> Here is my configuration
>
> O.S. Windows XP
> MySQL version 5.0.45-community-nt, packaged with XAMPP version 1.6.4
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 10:32 AM
Peter H. Coffin
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On Wed, 23 Jan 2008 20:46:15 -0800, piper1970 wrote:
> thanks for the advice. will be disregarding attempt to use 0 as
> auto-increment value.


Ideally, your PK shouldn't have any meaning outside identifying the row.
For me, the best way to manage that is to never know nor care what the
value is.

--
74. When I create a multimedia presentation of my plan designed so that my
five-year-old advisor can easily understand the details, I will not label
the disk "Project Overlord" and leave it lying on top of my desk.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 10:32 AM
Kees Nuyt
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On Thu, 24 Jan 2008 08:27:40 -0600, "Peter H. Coffin"
<hellsop@ninehells.com> wrote:

>On Wed, 23 Jan 2008 20:46:15 -0800, piper1970 wrote:
>> thanks for the advice. will be disregarding attempt to use 0 as
>> auto-increment value.

>
>Ideally, your PK shouldn't have any meaning outside identifying the row.
>For me, the best way to manage that is to never know nor care what the
>value is.


Alternatively, the primary key can be meaningful, that is
identify an object in real life, like a PartNumber,
SerialNumber or SocialSecurityNumber, in which case it
can't autoincrement, of course.
--
( Kees
)
c[_] In any bureaucracy, paperwork increases as you spend more
and more time reporting on the less and less you are doing.
Stability is achieved when you spend all of your
time reporting on the nothing you are doing. (#173)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-28-2008, 10:32 AM
Peter H. Coffin
 
Posts: n/a
Default Re: resetting auto-incremented field to start at zero

On Thu, 24 Jan 2008 19:48:50 +0100, Kees Nuyt wrote:
> On Thu, 24 Jan 2008 08:27:40 -0600, "Peter H. Coffin"
><hellsop@ninehells.com> wrote:
>
>>On Wed, 23 Jan 2008 20:46:15 -0800, piper1970 wrote:
>>> thanks for the advice. will be disregarding attempt to use 0 as
>>> auto-increment value.

>>
>>Ideally, your PK shouldn't have any meaning outside identifying the row.
>>For me, the best way to manage that is to never know nor care what the
>>value is.

>
> Alternatively, the primary key can be meaningful, that is
> identify an object in real life, like a PartNumber,
> SerialNumber or SocialSecurityNumber, in which case it
> can't autoincrement, of course.


Not a fan of it. Meaningful values change. Some change very slowly, some
can change quickly. EG SocialSecurityNumber doesn't survive identity
theft cases. PartNumbers often change with suppliers. CustomerNumbers
muiltiply like cockroaches when you're dealing with less than perfectly
sophisticated customers. Now, when you're dealing with a database that
is all in one space and never imports data back in from someplace else,
you're fine. If data is out someplace and needs to come back (result of
dealing with a vendor for a project, for example) and your meaningful
value has changed in the meantime, THEN you've got a problem. PKs that
exist only to be PKs don't have this problem.

I've gone so far before as to have both a meaning-free PK, *and* a
meaning-free user key, with intersection tables that described how
multiples of each actually combined into one aggregate entity. It was
ugly amounts of hair, but it made the database do what it was supposed
to in spite of some very dirty records, and kept everything functioning
while the mess got cleaned up.

--
56. My Legions of Terror will be trained in basic marksmanship. Any who
cannot learn to hit a man-sized target at 10 meters will be used for
target practice.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:00 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483