PDA

View Full Version : بهترین انتخاب برای ستون های از نوع string استفاده از varchar یا nvarchar



saeid1389
یک شنبه 30 مرداد 1390, 12:23 عصر
سلام به همه

- هر دو برای ذخیره داده به صورت رشته با طول متغییر استفاده می شه

- nvarchar برای ذخیره رشته های unicode (http://en.wikipedia.org/wiki/Unicode) استفاده می شه که دو برابر نسبت به نوع varchar فضا می گیره

- اگه از codepage استفاده کنی و اون code page توسط سیستم عامل شما و sql ساپورت بشه- مثلا برای زبان فارسی و یا عربی از code page 1256 (http://barnamenevis.org/showthread.php?301060-انتخاب-دو-زبان-پیش-فرض-برای-sql-server&p=1320672#post1320672) - می تونی از varchar هم استفاده کنی که البته varchar فضای کمتری جهت ذخیره کردن دیتا نسبت به nvarchar نیاز داره

اما نظر یه کاربر تو این سایت (http://stackoverflow.com/questions/144283/what-is-the-difference-between-varchar-and-nvarchar) توجه منو جلب کرد که پیشنهاد می کنه اگه از ASCII هم استفاده می کنید برای فیلدهاتون از unicode استفاده کنید و ... بقیه شو خودتون بخونید و نظر تون رو بگید.






An nvarchar column can store any Unicode data. A varchar column is restricted to an 8-bit codepage. Some people think that varchar should be used because it takes up less space. I believe this is not the correct answer. Codepage incompatabilities are a pain, and Unicode is the cure for codepage problems. With cheap disk and memory nowadays, there is really no reason to waste time mucking around with code pages anymore.

All modern operating systems and development platforms use Unicode internally. By using nvarchar rather than varchar, you can avoid doing encoding conversions every time you read from or write to the database. Conversions take time, and are prone to errors. And recovery from conversion errors is a non-trivial problem.

If you are interfacing with an application that uses only ASCII, I would still recommend using Unicode in the database. The OS and database collation algorithms will work better with Unicode. Unicode avoids conversion problems when interfacing with other systems. And you will be preparing for the future. And you can always validate that your data is restricted to 7-bit ASCII for whatever legacy system you're having to maintain, even while enjoying some of the benifits of full Unicode storage