varchar or nvarchar

后端 未结 9 1704
走了就别回头了
走了就别回头了 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:28

    I have red that nvarchar takes twice as varchar.

    Yes.

    nvarchar is used for internationalization.

    Yes.

    what u suggest should i use nvarchar or varchar?

    It's depends upon the application.

    0 讨论(0)
  • 2021-02-19 14:31

    By default go with nvarchar. There is very little reason to go with varchar these days, and every reason to go with nvarchar (allows international characters; as discussed).

    0 讨论(0)
  • 2021-02-19 14:32

    on performance:
    a reason to use varchar over nvarchar is that you can have twice as many characters in your indexes! index keys are limited to 900 bytes
    on usability:
    if the application is only ever intended for a english audience & contain english names, use varchar

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 14:35

    varchar is 1 byte per character, nvarchar is 2 bytes per character.

    You will use more space with nvarchar but there are many more allowable characters. The extra space is negligible, but you may miss those extra characters in the future. Even if you don't expect to require internationalization, people will often have non-English characters (e.g. é, ñ or ö) in their names.

    I would suggest you use nvarchar.

    0 讨论(0)
  • 2021-02-19 14:38

    Data to store: "Sunil"

    varchar(5) takes 7B nvarchar(5) takes 12B

    0 讨论(0)
提交回复
热议问题