views:

67

answers:

4

What should i know about involving basic data types in SQL Server?

In my database i need

  1. Flags/bits, I assume I should use byte
  2. 64bit ids/ints
  3. a variable length string. It could be 5 letters it could be 10,000 (for desc but i plan to allow unlimited length usernames)

Is there a TEXT type in SQL Server? I dont want to use varchar(limit) unless i could use something ridiculously high like 128k. How do i specify 1byte - 8byte ints?

+1  A: 

Here is the document.

http://msdn.microsoft.com/en-us/library/aa258271%28SQL.80%29.aspx

Daniel A. White
that's for SQL Server 2000 and quite outdated.....
marc_s
+4  A: 

For 1), use BIT - it's one bit, e.g. eight of those fields will be stuck into a single byte.

For 2), use BIGINT - 64-bit signed int

For 3), definitely do NOT use TEXT/NTEXT - those are deprecated as of SQL Server 2005 and up.

Use VARCHAR(MAX) or NVARCHAR(MAX) for up to 2 GB of textual information instead.

Here's the list of the SQL Server 2008 data types:

http://msdn.microsoft.com/en-us/library/ms187594.aspx

marc_s
+3  A: 
  • Flags/bits, I assume I should use byte

Use "bit" which is exactly that: one bit

  • 64bit ids/ints

bigint is 64 bit signed

  • a variable length string. It could be 5 letters it could be 10,000 (for desc but i plan to allow unlimited length usernames)

varchar(max) is up to 2GB. Otherwise varchar(8000) is the conventional limit

Microsoft even put into a nice handy web page for you

gbn
+1 You definitely found the nicer overview page than I did! :-)
marc_s
Do i have to worry about efficient and bit? if i have 12 bits or 16bits they both would end up taking the same space? (assuming its not indexed) just different inserting logic?
acidzombie24
Just let the engine take care of it: it packs them in itself into bytes, so 12 and 16 will take 2 bytes. This is what the engine is designed to do.
gbn
+1  A: 

Others have already provided good answers to your question. If you are doing any .NET development, and need to map SQL data types to CLR data types, the following link will be quite useful.

http://msdn.microsoft.com/en-us/library/bb386947.aspx

Randy

Randy Minder