The main (and rather critical) disadvantage is that it seems that the link you provide doesn't actually do what you think it does.
It merely just makes an new integer type that can only be positive, it doesn't provide you with any space saving that would otherwise result from using an unsigned field (which seems to be your main aim). that is to say that the max value of their unsignedSmallint
would be the same as the max value for smallint
, you would therefore still be wasting those extra Bits (but more so since you can't insert negative values).
That is to say that their unsignedInt
would not allow values above 2^31-1.
I understand and appreciate that in 100 million rows the savings from using a int32 vs int64 on a single column is around 380MB. Perhaps the best way for you to do this is to handle this is to offset your stored value after you read it, ideally within a view and only ever read from that view, and then when doing an insert add -2^31 to the value.. But the problem then is that the parsing for int32 occurs before the insert so INSTEAD OF
triggers won't work.. (I do not know of any way to make an INSTEAD OF trigger that accepts different types to that of the owning table)
Instead your only option in this regard is to use stored procedures to set
the value, you can then either use a view or a stored proc to get the value back:
create table foo
(fooA int)
GO
CREATE VIEW [bar]
AS
SELECT CAST(fooA AS BIGINT) + 2147483647 AS fooA
FROM foo
GO
CREATE PROCEDURE set_foo
@fooA bigint
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
IF @fooA < 4294967296 AND @fooA >= 0
INSERT INTO foo VALUES (@fooA - 2147483647)
--ELSE
-- throw some message here
END
GO
This can be tested using:
exec set_foo 123
exec set_foo 555
select * FROM bar
select * FROM foo
exec set_foo 0
exec set_foo 2147483648
exec set_foo 4147483648
select * FROM bar
select * FROM foo
You will see the values are returned unsigned, however the returned values are int64 and not unsigned32 so your application will need to treat them as if they were still int64.
If you have a case where you will see significant improvement from doing this (such as almost every column in the table is twice as big as it otherwise needs to be) then the effort above might be warranted, otherwise I would just stay with bigint
instead.
CHECK
constraint on the column. – Pablointeger
and add either aCHECK
constraint or triggers to check the values. – Geulincxrules
which are deprecated. You can also create CLR UDTs now. – Inchmeal