Home Database Mysql Tutorial MySQL中char与varchar的区别

MySQL中char与varchar的区别

Jun 07, 2016 pm 04:24 PM
char mysql var varchar the difference

char和varchar类型相似,但是它们的存储和检索方式不同。在MySQL5.0.3,它们在最大长度和是否保留尾部空格也不相同。 char和varchar定义了字符串的最大长度。例如,char(30)可以放30个字符。 char列的长度在你建表的时候,就是你定义的固定长度。值的范围是0

char和varchar类型相似,但是它们的存储和检索方式不同。在MySQL5.0.3,它们在最大长度和是否保留尾部空格也不相同。

char和varchar定义了字符串的最大长度。例如,char(30)可以放30个字符。

char列的长度在你建表的时候,就是你定义的固定长度。值的范围是0~255。char值存储的时候,右部以空格来填充到指定的长度。当检索char的时候,会自动去掉尾部的空格。

varchar列是变长的。在MySQL5.0.3之前可以定义的长度是0~255,之后可以定义到0~65535。在MySQL5.0.3及以后的版本中varchar列的有效长度,受行的最大值和使用的字符限制。

对比char,varchar,加上前缀的数据长度是当做一个字节还是两个字节存储。这个前缀的长度表明了值的字节数。如果值不超过255的话,这个列用一个字节的长度,如果这个值可能超过255个字节,那么就使用两个字节的长度。

如果严格的SQL模式没有启用,你分配了一个char或者varchar列超出了列的最大长度,那么这个值就会被截短以填充列,这时候就会产生一个警告。对于截短的非空格字符,你可以设置严格的SQL模式,来产生一个错误。

对于varchar列来说,SQL模式在启用的时候,大量的尾部空格在插入之前都会被截短,产生一个警告。对于char列来说,不管SQL模式是否被启用,都会静默地去掉插入值的过量的尾部空格。

varchar值存储的时候是不会填充的。如何处理尾部空格,取决于版本。在MySQL5.0.3中,当值存储和检索的时候,保留尾部空格,和标准的SQL保持一致。在MySQL5.0.3之前,值被保存到varchar列的时候,尾部空格会被剔除。这样也就意味着检索值的时候,也不存在这些空格。

在MySQL5.0.3之前,如果你需要一个不去掉尾部空格的数据类型的时候,要考虑BLOB或者TEXT类型。同样当你存储加密数据或者压缩数据的时候,也要使用BLOB列,而不是char或者varchar列,来避免需要改变值的时候,尾部空格的潜在问题。

下面的表格来说明char和varchar的存储方式和显示结果的差别。(假设列用的很简单的字符,使用latin1编码)

MySQL中char与varchar的区别

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)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles