 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 22nd, 2006, 05:11 PM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SQL Server Primary Key Incrementer
Is it possible to have a primary key that increments by something other than a whole number? I'd like to create a table where the keys are all powers of 2, but I can't figure out how to make that work.
|
|

March 22nd, 2006, 05:32 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Go to the table design view. Highlight the primary key row in the designer. Look down at the bottom of that page and you'll see... Use identity seed and identity increment to define how the auto increment feature will work.
|
|

March 22nd, 2006, 05:39 PM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I get that they are there, but how would I say, "I want the next row to have a value of the 2 to the power of the last id"?
|
|

March 22nd, 2006, 05:46 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
so you want the IDs to look like this:
2
4
8
16
32
etc.
|
|

March 22nd, 2006, 06:09 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
An integer key that was incremented as a power of two would have very limited value. A mere 32 entries in the table would overflow an integer key: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728,
268435456, 536870912, 1073741824, 2147483648.
In a like manner - 64 entries would overflow a bigint key.
Rand
|
|

March 22nd, 2006, 08:51 PM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
It's unlikely to ever get past 8-10. I appreciate the point, but I have a decent reason for wanting to do this.
|
|

March 23rd, 2006, 12:07 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
|
|
Then you can just put those numbers in yourself. You probably won't get the DB to do it for you. But you could always increment the key yourself in the application.
By the way, I'm really interested to hear what your reason is for doing this.
Thanks. Best of luck with this.
Richard
|
|

March 23rd, 2006, 08:18 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yeah, I just added another column that's computed as 2^(the key column), which will do. I was just trying to avoid the extra column. I'm using this to store permission information so I can figure out a user's rights quickly. See http://en.wikipedia.org/wiki/Bit_vector for a better explanation than I can give.
Thanks
|
|

March 23rd, 2006, 08:29 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Look up 'First Normal Form' to see why this may not be such a good idea...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
|
|

March 23rd, 2006, 08:32 AM
|
|
Authorized User
|
|
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Was it just that that was your 666th post or were you trying to be snide?
|
|
 |