Home Database Mysql Tutorial MySQL中VARCHAR和CHAR格式数据的区别

MySQL中VARCHAR和CHAR格式数据的区别

Jun 07, 2016 pm 02:58 PM
char m varchar

CHAR与VARCHAR类型类似,但它们保存和检索的方式不同。CHAR有固定的长度,而VARCHAR属于可变长的字符类型。它们最大长度和是否尾

区别

CHAR与VARCHAR类型类似,但它们保存和检索的方式不同。CHAR有固定的长度,而VARCHAR属于可变长的字符类型。它们最大长度和是否尾部空格被保留等方面也不同。在存储和检索过程中不进行大小写转换。

下面的表格显示了将各种字符串值保存到CHAR(4)和VARCHAR(4)列后的结果,说明了CHAR和VARCHAR之间的差别:

值 CHAR(4) 存储需求 VARCHAR(4) 存储需求

'' ' ' 4个字节 '' 1个字节

'ab' 'ab ' 4个字节 'ab' 3个字节

'abcd' 'abcd' 4个字节 'abcd' 5个字节

'abcdefgh' 'abcd' 4个字节 'abcd' 5个字节

从上面可以看得出来CHAR的长度是固定的,不管你存储的数据是多少他都会都固定的长度。而VARCHAR则处可变长度但他要在总长度上加1字节,这个用来存储字符长度(如果声明的长度超过255,,则使用2个字节)。所以实际应用中用户可以根据自己的数据类型来做。

请注意,上表中最后一行的值只适用不使用严格模式时;如果MySQL运行在严格模式,超过列长度的值不被保存,并且会出现错误。

从CHAR(4)和VARCHAR(4)列检索的值并不总是相同,因为检索时从CHAR列删除了尾部的空格。通过下面的例子说明差别:

mysql> CREATE TABLE test(a VARCHAR(4), b CHAR(4));
mysql> INSERT INTO test VALUES ('ab ', 'ab ');
mysql> SELECT CONCAT(a, '+'), CONCAT(b, '+') FROM test;

结果如下:

CONCAT(a, '+') CONCAT(b, '+')

ab + ab+

从上面可以看出来,由于某种原因CHAR有固定长度,所以在处理速度上要比VARCHAR快很多,但是相对浪费存储空间,所以对存储不大,但在速度上有要求的可以使用CHAR类型,反之可以用VARCHAR类型来实现。
建议

MyISAM存储引擎 建议使用固定长度,数据列代替可变长度的数据列
INNODB 存储引擎 建议使用VARCHAR类型

MySQL InnoDB存储引擎锁机制实验

InnoDB存储引擎的启动、关闭与恢复

MySQL InnoDB独立表空间的配置

MySQL Server 层和 InnoDB 引擎层 体系结构图

InnoDB 死锁案例解析

MySQL Innodb独立表空间的配置

本文永久更新链接地址

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

AITO's Wenjie M9 panoramic smart flagship SUV is about to debut AITO's Wenjie M9 panoramic smart flagship SUV is about to debut Sep 26, 2023 pm 02:41 PM

According to news on September 25, Huawei Mall has begun accepting reservations for AITO’s Wenjie M9 car. This car is positioned as a panoramic smart flagship SUV with eye-catching features. Consumers only need to pay a deposit of 5,000 yuan, which can be used to offset the final payment of up to 10,000 yuan. The new car will be available in two versions, namely the extended-range version and the pure electric version. The price is expected to be between 500,000 yuan and 600,000 yuan, making it competitive in the high-end SUV market. Wenjie M9 is expected to be officially launched in the fourth quarter of 2023. No information on its specific configuration has been disclosed yet. It is understood that the M9 model adopts a family-oriented design language. Judging from the two official pictures, the side lines of the vehicle are smooth, and the door handles adopt a hidden design, adding technology to the entire vehicle.

How to implement date comparison, sorting and other operations with the varchar type in mysql How to implement date comparison, sorting and other operations with the varchar type in mysql May 31, 2023 pm 01:49 PM

During the use of MySQL, dates are generally stored in datetime, timestamp and other formats. However, sometimes due to special needs or historical reasons, the date is stored in varchar format. So how should we process the date data in varchar format? ? Use function: STR_TO_DATE(str,format) The STR_TO_DATE(str,format) function is the inverse function of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains date and time components, or if the word

What is the difference between char and varchar in mysql What is the difference between char and varchar in mysql Sep 04, 2023 pm 02:16 PM

The differences between char and varchar in mysql are: 1. CHAR is fixed length, while VARCHAR is variable length; 2. CHAR storage and retrieval efficiency is high, while VARCHAR storage and retrieval efficiency is not high; 3. CHAR takes up storage space, VARCHAR can save storage space.

Keywords in C language: Is go one of them? Keywords in C language: Is go one of them? Mar 15, 2024 am 11:15 AM

Keywords in C language: Is go one of them? In the process of learning C language, we will inevitably encounter various keywords, which play an important role in programming. For beginners, sometimes there will be some doubts. For example, someone may ask, is there the keyword "go" in C language? Next, let's explore this issue together. First of all, we need to make it clear that C language is a very classic programming language, and its keywords have long been determined and will not be changed easily. In C language

What is a digital virtual currency? Where to buy digital virtual currency? What is a digital virtual currency? Where to buy digital virtual currency? Feb 14, 2025 pm 05:06 PM

Digital virtual currency, also known as cryptocurrency or virtual currency, is a decentralized digital currency. Unlike traditional currencies, digital virtual currencies are not controlled by central banks or governments, but are traded through computer networks on the Internet. The security of digital virtual currencies is guaranteed by complex cryptographic algorithms, making them non-forgery, anonymity and global circulation. With the popularity of digital virtual currencies, purchasing channels are also increasing.

Huawei Smart Car M7: Newly unveiled in September, the quality of driving experience has been improved! Huawei Smart Car M7: Newly unveiled in September, the quality of driving experience has been improved! Aug 22, 2023 am 09:13 AM

Huawei executive Yu Chengdong revealed some exciting news about Huawei's new smart car M7 on his personal Weibo today. This new car is expected to be officially released in September, bringing consumers a new driving experience. Understandably, Yu Chengdong said on Weibo that Huawei’s Inquiry M7 has made great progress in terms of driving and riding experience. Whether driving or riding, you can clearly feel the improvement. The vehicle's visibility and performance have been greatly optimized, making it ideal for long-distance family trips. He also mentioned that in order to create a more comfortable riding experience, Huawei has invested a lot of energy in new interior and exterior design. The new seats are softer and more comfortable, and have stronger wrapping properties, allowing passengers to get a better rest during the journey. According to Yu Chengdong, Huawei’s focus on Huawei smart cars

How does MySQL determine the size of VARCHAR How does MySQL determine the size of VARCHAR May 27, 2023 pm 09:20 PM

First of all, it is not recommended to use the TEXT type, because using TEXT will seriously affect efficiency. The best way is to use VARCHAR and determine the maximum length of the field. We can first define a field rule_value in the table and set the length to 255, then enter the smallest json string: use MySQL's CHAR_LENGTH function to calculate the size of rule_value: SELECTid, rule_type, rule_value, CHAR_LENGTH(rule_value) as rule_value_length, rule_markFROMtest_tableWHEREr

CentOS7 deploys cicd service in Jenkins CentOS7 deploys cicd service in Jenkins Feb 20, 2024 am 08:30 AM

Project address https://www.jenkins.io/zh/Set host name: hostnamectlset-hostnamejenkins personalized host name without time echo"PS1='[e[37;1m][[e[36;40m][e [32;40m]u[e[33;40m]@[e[31;40m]h[e[34;40m]w[e[37;1m]]\$[e[m]'">>. bashrc closes CentOS7's own firewall service systemctldisablefirewalld&&systemctlstopfirewa

See all articles