Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Composite key on numeric columns
Message
From
27/02/1999 07:58:47
 
 
To
27/02/1999 07:38:42
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00192305
Message ID:
00192307
Views:
18
>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!
EMail: EdR@edrauh.com
"See, the sun is going down..."
"No, the horizon is moving up!"
- Firesign Theater


NT and Win2K FAQ .. cWashington WSH/ADSI/WMI site
MS WSH site ........... WSH FAQ Site
Wrox Press .............. Win32 Scripting Journal
eSolutions Services, LLC

The Surgeon General has determined that prolonged exposure to the Windows Script Host may be addictive to laboratory mice and codemonkeys
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform