当前位置:网站首页>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 .
边栏推荐
- dev treelist 常用用法小结
- C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
- [day_060423] no two
- Map collection inheritance structure
- How does the seckill system ensure that the database does not crash and prevent oversold goods
- 【Day03_0420】C语言选择题
- 【Day_05 0422】统计回文
- Conda 虚拟环境envs目录为空
- 【pytorch】图片增广
- [day_040421] calculate candy
猜你喜欢

力扣——4. 寻找两个正序数组的中位数

力扣5: 最长回文子串
![[1]数学建模基础入门知识](/img/29/90b1c7533e9443852758d10080e239.png)
[1]数学建模基础入门知识

Upgrade appium automation framework to the latest 2.0

将一个正整数分解质因数,要求分解成尽可能小的多个的因数。

Address resolution ARP Protocol

What are the aspects of performance testing? What are the classification and testing methods?

How can machinery manufacturing enterprises do well in production management with the help of ERP system?

快速排序(quick-sort)
![[1] Basic knowledge of mathematical modeling](/img/29/90b1c7533e9443852758d10080e239.png)
[1] Basic knowledge of mathematical modeling
随机推荐
What are the aspects of performance testing? What are the classification and testing methods?
Show you the principle of IO multiplexing (select, poll and epoll)
[specified interval inversion in BM2 linked list]
[day_040421] calculate candy
[C language] file operation
@ConstructorProperties注解理解以及其对应使用方式
BPG笔记(四)
What are the main reasons for server crash
[day_030420] numbers that appear more than half of the time in the array
C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
快速排序(quick-sort)
Convert amount figures to uppercase
Liberg avenue to Jane series
【Web3 系列开发教程——创建你的第一个 NFT(4)】NFTs 可以给你带来什么
将金额数字转换为大写
Why the server is stuck
Overview of image classification of vision transformer must read series
Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
定义方法时为什么使用static关键字