Page 1 of 1

Unique index key

Posted: Thu Dec 17, 2015 6:00 am
by skiman
Hi,

This new thread for the following i saw.

Brian wrote:
Do you use some form of str(recno(),10) in everyone of your indexes to insure unique index keys ?
Roger answered with :
This is an important point.
I2Bin(RecNo()) also works.
I have several index files with a non-unique key. I never had problems with it.
Example: in my sales file I have an index on month(date). This way the calculation of monthly totals is easy and fast, in my agenda if have an index with startdate, and so on. I never had problems with this. I could add an extra key to create unique indexes. Would this be necessary? Could the use of index files without unique keys influence index files where the index key is always unique?

Secondly a question about the I2Bin(recno()) as extra key? What about files with more than 32767 records. Does this generate an unique key with I2Bin?

Re: Unique index key

Posted: Thu Dec 17, 2015 6:18 am
by Tom
Besides, if you struggle with unique numbers and references: Take a look at "UuidCreate()" and "UuidToChar()". UuidCreate() creates an - world-wide - unique identifier, UuidToChar() translates this binary to a character string (lenght 36). This is inside Xbase++ since 1.9 SL1. I added a field "UUID" to all my tables which need unique identifieres; "APPEND BLANK" is translated using the preprocessor and looks if this field exists. If so, the unique identifier is replaced. This removed ALL problems I ever had with numbers and cross-table-references. It's not useful if numbering must be strictly incremental or if the numbers can be used by the users (like for invoice numbers or customer IDs). But in all other situations, like for table relations, it's just perfect. Even merging tables from different datasources isn't a problem anymore, since the UUIDs are unique in all situations.

Re: Unique index key

Posted: Thu Dec 17, 2015 6:42 am
by rdonnay
What about files with more than 32767 records. Does this generate an unique key with I2Bin?
Actually, yes it would be unique provided that the primary expression does not exceed 32k records.

For example, INDEX ON CUSTNMBR+I2Bin(Recno()) would be unique for the first 32k records with the same custnmbr.
If it is likely that a database would have more than 32k of the same custnmbr, then Tom's solution would be more appropriate.

Re: Unique index key

Posted: Thu Dec 17, 2015 8:31 am
by bwolfsohn
but if all you're doing is trying to generate a unique index key, str(recno()) will ALWAYS be unique.. what are the advantages of l2bin as opposed to str(recno())

Re: Unique index key

Posted: Thu Dec 17, 2015 9:02 am
by rdonnay
I was told that it is faster when creating the indexes. I believe I got that information from Steffen.