Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old March 22nd, 2006, 05:11 PM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old March 22nd, 2006, 05:32 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

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.

 
Old March 22nd, 2006, 05:39 PM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"?

 
Old March 22nd, 2006, 05:46 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

so you want the IDs to look like this:

2
4
8
16
32
etc.

 
Old March 22nd, 2006, 06:09 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 22nd, 2006, 08:51 PM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's unlikely to ever get past 8-10. I appreciate the point, but I have a decent reason for wanting to do this.

 
Old March 23rd, 2006, 12:07 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

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

 
Old March 23rd, 2006, 08:18 AM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old March 23rd, 2006, 08:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old March 23rd, 2006, 08:32 AM
Authorized User
 
Join Date: Jan 2006
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Was it just that that was your 666th post or were you trying to be snide?






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Design: Foreign Key to Multiple Primary Keys? kalel_4444 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 10 May 8th, 2008 04:14 PM
SQL to identify the primary key in a table? dbayona SQL Server 2005 1 October 24th, 2007 02:33 AM
SQL Server Primary key Lofa SQL Server 2000 12 September 29th, 2005 08:45 PM
How to Auto Generate ID (Primary Key) SQL database havering SQL Server 2000 9 December 1st, 2004 10:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.