Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am specifically thinking about unsigned
int
.
Here is a practical example: what do you do when your identity column maxes out? It's possible to either go
BigInt
(8 bytes storage instead of 4) or to refactor the application to support negative integers, and even to create your own rules as indicated in
this answer
; neither of those options are optimal.
UInt
would be an ideal solution, but SQL Server does not offer it (where MySQL does).
I understand that unsigned datatypes are not part of the SQL standard (SQL-2003) but still seems like a waste to me.
What is the reason of not including these (in SQL Server or in the standard)?
–
–
–
–
–
–
–
If I had to guess, I would say that they are trying to avoid a proliferation of types. Generally speaking there isn't anything that an unsigned integer can do that a signed integer can't do. As for the case when you need a number between 2147483648 and 4294967296 you probably should go to an 8 byte integer since the number will also eventually exceed 4294967296.
–
–
–
I found a similar question
on Microsoft Office Dev Center.
The reply from Jim Hogg (Program Manager) has some pro's and con's for adding unsigned int's. The major con is the rules to implement implicit type conversions become a nightmare to get right.
The request was closed as "Won't Fix".
–
–
They don't support the SIGNED and UNSIGNED keyword because they're not standard. In SQL standard, all numeric types are signed.
UNSIGNED (and SIGNED, which is the default) are MySQL extensions that can be useful to store higher unsigned numbers in the same amount of bytes, and disallow negative numbers.
–
–
Take 32 bit(8 byte) int for example.
The range of 32 bit int is from -2^31 to 2^31-1.
It takes 31 bit to record the value you assigned and only 1 bit to record the sign of value.
So the answer to your question is "Unnecessary". Even though every value you assigned is positive, it waste only 1 bit per value. Creating a new datatype for saving only 1 bit per value is not a good way to optimize storage space.
–
Set your DB to have the min identity
Identity(-2147483648, 1)
Then when loading into your .net UInt64 variable add 2147483648 to it. then
-2147483648 becomes 0
-1000000000 becomes 1147483648
But also in most cases the internal keys shouldn't be exposed to clients, I typically use a separate key which can be anything like 'ABCKey1"
However I'm in agreement that the datatype is large enough in 99% of the systems out there. If you really need more you could use a GUID - however that sucks for Index's unless you use the next sequential GUID.
You can always use
DECIMAL
. Huge decimal -
DECIMAL(38, 0)
. Should suffice for a month or two...
CREATE TABLE IdentityTest
Id DECIMAL(38, 0) IDENTITY,
Name NVARCHAR(200)
INSERT INTO IdentityTest VALUES('John'),('Peter'),('Tom')
SELECT * FROM IdentityTest
DROP TABLE IdentityTest
Which produces this result:
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.