Subject Re: autoincrement
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 30 May 2022 16:04:21 +0200
Newsgroups dbase.getting-started
Attachment(s) autoinc_test.prg

On 2022/05/29 17:19, Ken Mayer wrote:

>> The decimal equivalent is 4 294 967 295.
>>
>> You shouldn't be running out of numbers any time soon. :-)
>
> And my understanding is that when it maxes it the numbers go negative? I
> haven't tried to do that, but in theory that could mean double the
> potential values.

It does indeed.  The help file makes it quite clear that a LONG value is
signed 32-bit integer.  (I always seem to look up these things later
rather than sooner. :-( )

As a signed 32bit value it means that the maximum value for an autoinc
field is 0x7FFFFFFF i.e 2 147 483 647.  Half the value I posted before
I'd read the help file but it will still be a while before one runs out
of values. :-)

Normally an autoinc field starts with the value 1.  The attached test
program shows that as the value transitions from positive to negative
NULL is saved in the field.  When the value transitions from negative to
positive 0 is saved in the field.  This has the potential to cause
problems but in real life nobody is likely to encounter this.

Mervyn







if file('autoinc_test.dbf')
drop table autoinc_test
endif

if not file('autoinc_test.dbf')
   create table autoinc_test  (id autoinc)
   use autoinc_test
   for n = 1 to 5
       append blank
    next  
   use
endif
clear
f = new file()
f.open('autoinc_test.dbf','RW')
f.seek(110)
f.write(chr(0xFE)) //2 before transition from positive to negative
f.write(chr(0xFF))
f.write(chr(0xFF))
f.write(chr(0x7F))
f.close()
use autoinc_test
   for n = 1 to 5
       append blank
    next  
   use
f.open('autoinc_test.dbf','RW')
f.seek(110)
f.write(chr(0xFE)) //2 before transition from negative to positive
f.write(chr(0xFF))
f.write(chr(0xFF))
f.write(chr(0xFF))
f.close()
use autoinc_test
   for n = 1 to 5
       append blank
    next  
list id,itoh(id) all
use