Multi-bit database field

I'm using a MSSQL database and would like to create a column that only has 4 possible values. Is there any way to define a 2-bit column? I see the bit datatype and then the next smallest is tinyint which is 1 full byte.

If there is no such field, I'd be interesting in finding out why not.

Thanks.

Answers


I don't see the reason for such a field.

It definately cannot be space - that's cheap today, and an economy of 6 bits is really negligible unless you have lots of rows. But in that case it will take up much space anyway, and extra 6 bits per row will again hardly make a difference.

Speed is also not a valid reason. Such bit fields would require additional manipulation to store and retrieve, making the whole process a lot slower than simply dealing with a whole byte.

Lastly, if it's about constraints, then you should use foreign keys or user-defined types (though that would require additional coding in .NET). Although I'd simply use a byte and implement the constraint in my application. Not so nice perhaps, but heck of a lot easier.

Update: Although, yes, an "enum" constraint would be useful in MSSQL. But that's again a little different than simply a 2-bit field.


What is your concern with field size? The way to implement a field domain should be using a Foreign Key constraint (MSSQL should have that).


Need Your Help

Sqlite databse in android appliaction working but it comes with double entry(insert) in table

android sqlite insert sqliteopenhelper

I have made an aplication in android and used Sqlite database for storing data.I have tried sqlite Open helper and made class for it..its working well but the Problem is that when i inserting the d...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.