tags:

views:

408

answers:

5

Hi I was wondering when I should use the different data types. As in in my table, how can I decide which to use: nvarchar, nchar, varchar, varbinary, etc.

Examples:

What would I use for a ... column:

Phone number,
Address,
First Name, Last Name,
Email,
ID number,
etc.

Thanks for any help!

+1  A: 

A data type beginning with n means it can be used for unicode characters... eg nVarchar.

Selection of integers is also quite fun.

http://www.databasejournal.com/features/mssql/article.phpr/2212141/Choosing-SQL-Server-2000-Data-Types.htm

The most common data type i use is varchar....

GordonB
A: 

Varchar, Integer, and Bit cover 99% of my day to day uses.

Neil N
A: 

The N* data types (NVARCHAR, NCHAR, NTEXT) are for Unicode strings. They take up two times the space their "normal" pendants (VARCHAR, CHAR, TEXT) need, but they can store Unicode without conversion and possible loss of fidelity.

The TEXT data types can store nearly unlimited amounts of data, but they perform not as good as the CHAR data types because they are stored outside of the record.

THE VARCHAR data types are of variable length. They will not be padded with spaces at the end, but their CHAR pendants will (a CHAR(20) is always twenty characters long, even if if contains 5 letters only. The remaining 15 will be spaces).

The binary data types are for binary data, whatever you care to store into them (images are a primary example).

Tomalak
+1  A: 

The question really depends on your requirements. I know that's not a particularly satisfactory answer, but it's true.

The n..char data types are for Unicode data, so if you're going to need to use unicode character sets in your data you should use those types as opposed to their "non-n" analogs. the nchar and char type are fixed length, and the nvarchar and varchar type can have a variable length, which will effect the size of the column on the disk and in memory. Generally I would say to use the type that uses the least disk space but fits for your needs.

This page has links to the Microsoft descriptions of these datatypes for SQL Server 2005, many of which give pointers for when to use which type. You might be particularly interested in this page regarding char and varchar types.

cori
+2  A: 

As a general rule, I would not define anything as a "number" field if I wasn't going to be doing arithmetic on it, even if the data itself was numeric.

Your "phone" field is one example. I'd define that as a varchar.