views:

451

answers:

3

When designing a database, what decisions do you consider when deciding how big your nvarchar should be.

If i was to make an address table my gut reaction would be for address line 1 to be nvarchar(255) like an old access database.

I have found using this has got me in bother with the old 'The string would be truncated'. I know that this can be prevented by limiting the input box but if a user really has a address line one that is over 255 this should be allowed.

How big should I make my nvarchar(????)

+2  A: 

I don't use nvarchar personally :-) I always use varchar.

However, I tend to use 100 for name and 1000 for comments. Trapping and dealing with longer strings is something the client can do, say via regex, so SQL only gets the data it expects.

You can avoid truncation errors be parameterising the calls, for example via stored procs. If the parameter is defined as varchar(200), say, then truncation happens silently if you send > 200. The truncation error is thrown only for an INSERT or UPDATE statement: with parameters it won't happen.

The 255 "limit" for SQL Server goes back to 6.5 because vachar was limited to 255. SQL Server 7.0 + changed to 8000 and added support for unicode

Edit:

Why I don't use nvarchar: Double memory footprint, double index size, double disk size, simply don't need it. I work for a big Swiss company with offices globally so I'm not being parochial.

Also discussed here: varchar vs nvarchar performance

On further reflection, I'd suggest unicode appeals to client developers but as a developer DBA I focus on performance and efficiency...

gbn
Why would you limit the comments to 100? Stack overflow allows 600. What data type do you use?
Paul
Why don't you use nvarchar? What do you use instead?
Tor Haugen
@Paul: 1000! aarrgghhh...
gbn
@Tor: updated my answer
gbn
Oh Ok but what makes you decide on this number why not go for 4000?
Paul
Don't need it for the product management type system here: we have name and optional comment throughout. The length is also driven by requirement and practicality: it forces some conciseness and makes our GUI look better!
gbn
A: 

It depends on what the field represents. If I'm doing a quick prototype I leave the defaults of 255. For anything like comments etc I'd probably put it to 1000.

The only way I'd make it smaller really is on things I definately know the siez of, zip codes or NI numbers etc.

dean nolan
+1  A: 

My recommendation: make them just as big as you REALLY need them.

E.g. for a zip code column, 10-20 chars are definitely enough. Ditto for a phone number. E-Mails might be longer, 50-100 chars. Names - well, I usually get by with 50 chars, ditto for first names. You can always and easily extend fields if you really need to - that's no a big undertaking at all.

There's really no point in making all varchar/nvarchar fields as big as they can be. After all, a SQL Server page is fixed and limited to 8060 bytes per row. Having 10 fields of NVARCHAR(4000) is just asking for trouble.... (since if you actually try to fill them with too much data, SQL Server will barf at you).

If you REALLY need a really big field, use NVARCHAR/VARCHAR(MAX) - those are stored in your page, as long as they fit, and will be sent to "overflow" storage if they get too big.

NVARCHAR vs. VARCHAR: this really boils down to do you really need "exotic" characters, such as Japanese, Chinese, or other non-ASCII style characters? In Europe, even some of the eastern European characters cannot be represented by VARCHAR fields anymore (they will be stripped of their hachek (? spelling ?). Western European languages (English, German, French, etc.) are all very well served by VARCHAR.

BUT: NVARCHAR does use twice as much space - on disk and in your SQL Server memory - at all times. If you really need it, you need it - but do you REALLY ? :-) It's up to you.

Marc

marc_s
And if my app was being used by Russians then yeah I would need it.Thanks
Paul