当前位置:网站首页>Differences and relations between varchar and nvarchar in database
Differences and relations between varchar and nvarchar in database
2022-07-26 06:35:00 【51CTO】
When creating a new table in the database, some field types are found with n Some don't n, So what's the difference between the two ?
So I went online to find some information as follows :
One 、
1、CHAR.CHAR It's very convenient to store fixed length data ,CHAR The index on the field is of high efficiency , Like defining char(10), So whether or not the data you store reaches 10 Bytes , Take it all 10 Bytes of space , The insufficient space is filled in automatically , So it may be used many times when reading trim().
2、VARCHAR. Store variable length data , But storage efficiency is not CHAR high . If the possible value of a field is not of fixed length , All we know is that it can't exceed 10 Characters , Define it as VARCHAR(10) Is the most cost-effective .VARCHAR The actual length of a type is the actual length of its value +1. Why? “+1” Well ? This byte is used to store the actual length used . From the perspective of space , use varchar appropriate ; Considering efficiency , use char appropriate , The key is to find the trade-off point according to the actual situation .
3、TEXT.text Store variable length non Unicode data , Maximum length is 2^31-1(2,147,483,647) Characters .
4、NCHAR、NVARCHAR、NTEXT. These three are more than the first three in terms of names “N”. It means that the storage is Unicode The character of the data type . We know that in characters , English characters need only one byte to store , But there are many Chinese characters , It takes two bytes to store , It is easy to cause confusion when English and Chinese characters exist at the same time ,Unicode Character set is to solve the problem of character set incompatibility , All of its characters are represented in two bytes , In other words, English characters are also represented by two bytes .nchar、nvarchar The length of is 1 To 4000 Between . and char、varchar Compare ,nchar、nvarchar At most 4000 Characters , Whether in English or Chinese ; and char、varchar It can store at most 8000 English ,4000 The Chinese characters . It can be seen that the nchar、nvarchar Don't worry about whether the input character is English or Chinese , More convenient , But there is some loss in the amount of English stored .
So generally speaking , If it contains Chinese characters , use nchar/nvarchar, If it's pure English and numbers , use char/varchar
Their differences can be summarized as :
CHAR,NCHAR Fixed length , Fast , Take up a lot of space , Need to be dealt with
VARCHAR,NVARCHAR,TEXT Indefinite length , Small space , Slow speed , No need to deal with
NCHAR、NVARCHAR、NTEXT Handle Unicode code
Reference article : Click to open the link
Two 、
varchar stay SQL Server In, a single word section is used to store data ,nvarchar It's using Unico To store data . Chinese characters are stored in SQL Server Will be saved as two bytes ( It is generally used Unico code ), English characters are saved in the database , If the field type is varchar, Then only one byte will be occupied , If the field type is nvarchar, Will occupy two bytes .
Under normal circumstances , We use varchar Chinese characters can also be stored , However, if the operating system is English and the support for Chinese Fonts is not comprehensive , stay SQL Server Store Chinese characters as varchar There will be chaos ( Is shown as ??). And under normal circumstances , The host will support Chinese environment , So if you use varchar To store data , It cannot be found in the development stage . Most of the time , There will be no problem when deploying .
But if the host deployed is an English operating system , And does not support Chinese environment , Then the problem arises . be-all varchar Fields will become garbled when storing Chinese ( Is shown as ??). And generally, you won't know that this is because you use the wrong data type to store , You will try to install Chinese fonts , Try to set the language environment of the operating system ... None of these can solve the problem , The only way to solve the problem is to personalize the type of database fields as nvarchar( perhaps nchar). Friends who are familiar with project management should know , It is a terrible thing to modify the database at the deployment stage .
Use nvarchar Another great advantage of is that you don't need to consider the differences between Chinese and English characters when judging strings .
Of course , Use nvarchar Storing English characters will double the storage space . But when the cost of storage is already very low , Giving priority to compatibility will bring you more benefits .
So in Design You should try to use nvarchar To store data . Only when you make sure that this field will not save Chinese , Just use varchar To store .
Generally speaking , If it contains Chinese characters , use nchar/nvarchar, If it's pure English and numbers , use char/varchar
Reference article : Click to open the link
Unicode Character set is to solve the problem of character set incompatibility , All of its characters are represented in two bytes , In other words, English characters are also represented by two bytes .
边栏推荐
- RNN循环神经网络
- [C language] address book dynamic version and document version
- BPG notes (IV)
- 堆排序(heap-sort)
- How does the seckill system ensure that the database does not crash and prevent oversold goods
- 【C语言】通讯录动态版和文件版
- Basis of multimodal semantic segmentation
- PG Vacuum 杂谈之 auto vacuum
- JVM class loading and GC garbage collection mechanism
- [day_050422] continuous maximum sum
猜你喜欢

深度学习——CV、CNN、RNN

C语言进阶——可存档通讯录(文件)

Find the original root

Map collection inheritance structure
![[C language] file operation](/img/19/5bfcbc0dc63d68f10155e16d99581c.png)
[C language] file operation

Why the server is stuck

Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.

原生高性能抓包工具Proxyman,送给爱学习的你

『HarmonyOS』探索HarmonyOS应用

Go 的切片与数组
随机推荐
[1] Basic knowledge of mathematical modeling
【Day_07 0425】Fibonacci数列
【C语言】通讯录动态版和文件版
Three skills are needed to engage in SAP related work
Interpretation of TPS motion (cvpr2022) video generation paper
[day04_0421] C language multiple choice questions
Liberg avenue to Jane series
Why the server is stuck
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
Show you the principle of IO multiplexing (select, poll and epoll)
Jz36 binary search tree and bidirectional linked list
TCP protocol -- message format, connection establishment, reliable transmission, congestion control
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)
Multi target detection
C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
【Day05_0422】C语言选择题
原生高性能抓包工具Proxyman,送给爱学习的你
Go 的切片与数组
Leetcode 347. top k high frequency elements
性能测试包括哪些方面?分类及测试方法有哪些?