|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 9:22 AM
Points: 1,359,
Visits: 1,748
|
|
I just discovered a problem in the database design for an application that was developed years ago. There is an audit table with OldValue and NewValue columns defined as nvarchar(255). However, in the mean time, new tables have been defined with columns longer than nvarchar(255). I want to propose that the audit table use a much larger value. However, nvarchar takes up twice as much space as varchar, AND not too long ago, there was a forum post about trailing blanks in nvarchar, and how that can be a problem in comparisons, etc. unless you use RTRIM().
So I was wondering what the general consensus is on using varchar versus nvarchar, especially for longer strings. Is it considered best practice to always use nvarchar, or are there some other considerations?
Thanks.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 11:17 AM
Points: 9,855,
Visits: 9,376
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 8:56 AM
Points: 2,802,
Visits: 7,110
|
|
I tend to default to using NVARCHAR, unless i can absolutely be certain that there will not be any UniCode data in there. I find the increase in space is easier to deal with than potentialy needing to chnage the data types in the future.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:09 AM
Points: 38,074,
Visits: 30,368
|
|
Carla Wilson (12/2/2008)
So I was wondering what the general consensus is on using varchar versus nvarchar, especially for longer strings. Is it considered best practice to always use nvarchar, or are there some other considerations?
Best practice is to always use the appropriate data type. so if you're going to be storing unicode, make the column nvarchar. If you're not storing unicode (just ascii strings) then make the column varchar.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 9:22 AM
Points: 1,359,
Visits: 1,748
|
|
Practically speaking, I expect only "American-keyboard" ascii characters, but I suspect the original designer wanted to cover international users. But I can think of a number of other columns in this database that would never need unicode.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Yesterday @ 12:11 AM
Points: 407,
Visits: 2,866
|
|
On the specific question of audit tables. If your requirement is to capture actual data then you should create audit table columns that match the types of the data being audited. If you have more than one type then use multiple columns. Casting every column to a single string type (whether VARCHAR or NVARCHAR) is poor practice in my opinion.
In SQL Server 2008 use Change Data Capture rather than roll-your-own audit.
David
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 9:22 AM
Points: 1,359,
Visits: 1,748
|
|
RE: audit table - it's not like a standard audit table. I have several of those, and the columns do match the datatypes of the tables that they audit. I was going to put the word in double-quotes when I first posted; this table is used in an application to keep track of change requests from users, that may or may not be approved. The application has been around a LONG time, and I'm not even going to propose messing with it. However I do need to make sure it keeps working.
Thanks to everyone for your input. It was interesting to see that the use of nvarchar is not as common as I was expecting to see.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 8:56 AM
Points: 2,802,
Visits: 7,110
|
|
Carla Wilson (12/3/2008) RE: audit table - it's not like a standard audit table. I have several of those, and the columns do match the datatypes of the tables that they audit. I was going to put the word in double-quotes when I first posted; this table is used in an application to keep track of change requests from users, that may or may not be approved. The application has been around a LONG time, and I'm not even going to propose messing with it. However I do need to make sure it keeps working.
Thanks to everyone for your input. It was interesting to see that the use of nvarchar is not as common as I was expecting to see.
It is probably not used as much in America, but here in Europe with all the different languages etc.. , it is pretty much needed for any column that will hold user input string data.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 9:16 AM
Points: 1,150,
Visits: 1,859
|
|
Note that even in America, extended ASCII is very common. Just think of all of peoples names that have accents in them. Or the word resumé. Let alone, there is this province in Canada that has French as the standard language.
So if you are designing a new system, use NCHAR and NVARCHAR. Trust me and you'll thank me later.
(PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.
|
|
|
|