varchar or nvarchar

后端 未结 9 1835
走了就别回头了
走了就别回头了 2021-02-19 13:54

I am storing first name and last name with up to 30 characters each. Which is better varchar or nvarchar.

I have read that nvarchar

9条回答
  •  一整个雨季
    2021-02-19 14:33

    I have red that nvarchar takes twice as varchar

    Yes. According to Microsoft: "Storage size, in bytes, is two times the number of characters entered + 2 bytes" (http://msdn.microsoft.com/en-us/library/ms186939(SQL.90).aspx).

    But storage is cheap; I never worry about a few extra bytes.

    Also, save yourself trouble in the future and set the maximum widths to something more generous, like 100 characters. There is absolutely no storage overhead to this when you're using varchar or nvarchar (as opposed to char/nchar). You never know when you're going to encounter a triple-barrelled surname or some long foreign name which exceeds 30 characters.

    nvarchar is used for internationalization.

    nvarchar can store any unicode character, such as characters from non-Latin scripts (Arabic, Chinese, etc). I'm not sure how your application will be taking data (via the web, via a GUI toolkit, etc) but it's likely that whatever technology you're using supports unicode out of the box. That means that for any user-entered data (such as name) there is always the possibility of receiving non-Latin characters, if not now then in the future.

    If I was building a new application, I would use nvarchar. Call it "future-proofing" if you like.

提交回复
热议问题