>Hi.
>
>I am trying to create a composite key on two numeric columns for a table. VFP 6.0 seems to create the key by just adding the numeric values. This obviously does not enforce the uniqueness.
>
>Is there a workaround? Or I have done something wrong?
Sure - there are a couple of possible approaches.
The simplest one is to create an index based on the character representation of the number. You can use the STR() function to do this; if you have numeric, integer keys named nKey1 and nKey2 respectively, you could say:
INDEX ON STR(nKey1,8) + STR(nKey2,8) TAG ConCatSTR
An alternative is to use BINTOC(), which converts the number to a binary string - it's faster, but not human-readable:
INDEX ON BINTOC(nKey1,4) + BINTOC(nKey2,4) TAG ConCatBIN
Another alternative if you knew that the second index number was always less than a given power of 10 or power of two would be to add the two numbers, multiplying by an appropriate value. If, for example, you knew that the second key nKey2 was always in the range 0-9999, you could:
INDEX on Nkey1 * 10000 + nKey2 TAG AddedPwr10
If nKey2 was always less than 65536 (2^16), a power of two, you could use a bitwise operator to do a slick operation; this works very well when the fields are of type I:
INDEX ON BITLSHIFT(nKey1,16) + nKey2 TAG AddShifted
And I've probably left out a couple of dozen other approaches!