Oliver S
Oliver S

Reputation: 417

SQL When to use Which Data Type

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!

Upvotes: 6

Views: 23857

Answers (6)

user565869
user565869

Reputation:

Other people have given good general answers, but I'd add one important point: when using VARCHAR()s (which I would recommend for those kinds of fields), be sure to use a length that's big enough for any reasonable value. For example, I typically declare VARCHAR(100) for a name, e-mail address, domain name, city name, etc., and VARCHAR(200) for an URL or street address.

This is more than you'll routinely need. In fact, 30 characters is enough for almost all of these values (except full name, but a good database should always store first and last name separately), but it's better than having to change data types some day down the road. There's very little cost in specifying a higher-than-necessary length for a VARCHAR, but note that VARCHAR(MAX) and TEXT do entail significant overhead, so use them only when necessary.

Here's a post which points out a case where a longer-than-necessary VARCHAR can hurt performance: Importance of varchar length in MySQL table. Goes to show that everything has a cost, though in general I'd still favor long VARCHARs.

Upvotes: 0

user71950
user71950

Reputation: 59

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.

Upvotes: 5

cori
cori

Reputation: 8810

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.

Upvotes: 2

Tomalak
Tomalak

Reputation: 338158

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).

Upvotes: 1

Neil N
Neil N

Reputation: 25258

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

Upvotes: 2

GordonBy
GordonBy

Reputation: 3397

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....

Upvotes: 1

Related Questions