Home Database Mysql Tutorial Comprehensive understanding of data types in MySQL

Comprehensive understanding of data types in MySQL

Mar 30, 2017 am 11:32 AM

1. Char and varchar types

char and varchar types are similar, both are used to store strings, but they save and retrieve strings in different ways. char belongs to the fixed-length character type , and varchar belongs to the variable-length character type. For example: for the two type definitions of char(4) and varchar(4):

(1), '' occupy 4 bytes in char(4), varchar(4) It only occupies one byte in length;

(2), 'ab' occupies 4 bytes in char(4), and only 3 bytes in varchar(4) Length;

(3), 'abcd' occupies 4 bytes in char(4), and 5 bytes in varchar(4);

Why is there an extra byte length in the varchar type? This is because the varchar type uses the extra byte to store the actual length of the varchar type. The retrieval of char(4) and varchar(4) is not always the same, for example:

mysql> create table char_and_varchar (v varchar(4),c char(4));
Query OK, 0 rows affected (0.20 sec)

mysql> insert into char_and_varchar values ('ab  ','ab  ');
Query OK, 1 row affected (0.33 sec)

mysql> select concat(v,'cd'),concat(c,'cd') from char_and_varchar;
+----------------+----------------+
| concat(v,'cd') | concat(c,'cd') |
+----------------+----------------+
| ab  cd         | abcd           |
+----------------+----------------+
1 row in set (0.35 sec)
Copy after login


Since char is a fixed length, its processing speed is much faster than varchar , but its disadvantages are a waste of storage space, and the program needs to process trailing spaces. Therefore, data whose length does not change much and has high requirements for query speed can be considered to use the char type to store. As the MySQL version continues to upgrade, the performance of the varchar data type will also continue to improve, and the application scope of the varchar type will become wider.

In MySQL, different storage engines have different usage principles for char and varchar:

(1). In the MyISAM storage engine, it is recommended to use fixed-length field types instead of Variable length field type.
(2). In the Memory storage engine, fixed-length data lines are currently stored, so whether it is char or varchar type, it will be converted to char type for processing.
(3). In the InnoDB storage engine, it is recommended to use the varchar type.

2. TEXT and BLOB

When saving a small number of strings, you can use the char and varchar data types. When saving larger text, you usually choose to use text or BLOB. The main difference between the two is that BLOB can be used to save binary data, such as photos, while text can only be used to save character type data. Text and BLOB include three different types: text, mediumtext, longtext and blob, mediumblob and longblob respectively. The main difference between them is the length of text stored and the bytes stored.

Some issues that should be paid attention to when using BLOB and TEXT types:

(1) BLOB and TEXT will cause some performance problems, especially when a large number of deletion operations are performed. The deletion operation will leave large "holes" in the data table. Filling in these "holes" records in the future will have an impact on the insertion performance. In order to improve performance, you should regularly use the OPTIMIZETABLE function to defragment such tables to avoid holes causing performance problems.

(2) Use synthetic indexes to improve query performance for large text fields. The so-called synthetic index is to create a hash value based on the content of the large text field and store this value in a separate data column. Then the data row can be found through the hash value. For example:

mysql> create table t (id varchar(100),content blob,hash_value varchar(40));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t values (1,repeat('beijing',2),md5(content)); 
Query OK, 1 row affected (0.33 sec)

mysql> insert into t values (2,repeat('beijing',2),md5(content)); 
Query OK, 1 row affected (0.01 sec)

mysql> insert into t values (2,repeat('beijing 2008',2),md5(content));
Query OK, 1 row affected (0.01 sec)

mysql> select * from t;
+------+--------------------------+----------------------------------+
| id   | content                  | hash_value                       |
+------+--------------------------+----------------------------------+
| 1    | beijingbeijing           | 09746eef633dbbccb7997dfd795cff17 |
| 2    | beijingbeijing           | 09746eef633dbbccb7997dfd795cff17 |
| 2    | beijing 2008beijing 2008 | 1c0ddb82cca9ed63e1cacbddd3f74082 |
+------+--------------------------+----------------------------------+
3 rows in set (0.00 sec)

mysql> select * from t where hash_value=md5(repeat('beijing 2008',2));
+------+--------------------------+----------------------------------+
| id   | content                  | hash_value                       |
+------+--------------------------+----------------------------------+
| 2    | beijing 2008beijing 2008 | 1c0ddb82cca9ed63e1cacbddd3f74082 |
+------+--------------------------+----------------------------------+
1 row in set (0.00 sec)
Copy after login


Synthetic index can only be used in exact matching scenarios, which reduces disk I/O to a certain extent and improves query efficiency. If you need to perform fuzzy queries on BLOB and CLOB fields, you can use MySQL's prefix index, that is, create an index for the first n columns of the field. For example:

mysql> create index idx_blob on t (content(100));
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from t \G
*************************** 1. row ***************************
        Table: t
   Non_unique: 1
     Key_name: idx_blob
 Seq_in_index: 1
  Column_name: content
    Collation: A
  Cardinality: 3
     Sub_part: 100
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment: 
Index_comment: 
1 row in set (0.00 sec)

mysql> desc select * from t where content like 'beijing%' \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t
         type: ALL
possible_keys: idx_blob
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3
        Extra: Using where
1 row in set (0.00 sec)
Copy after login

(3). Do not retrieve large BLOB or TEXT fields unless necessary.

(4). Separate BLOB or TEXT fields into separate tables.

3. Floating-point numbers and fixed-point numbers

Floating-point numbers are generally used to represent values ​​that contain decimal parts. When a field is defined as a floating point type, if the precision of the inserted data exceeds the actual precision defined for the column, the inserted value will be rounded to the actual defined precision value. Then insert, the rounding process will not report an error. Float and double (real) in MySQL are used to represent floating point numbers.

Fixed-point numbers are different from floating-point numbers. Fixed-point numbers are actually stored in the form of strings, so fixed-point numbers can store data more accurately. If the precision of the inserted data is greater than the actual defined precision, MySQL will issue a warning, but the data will be rounded according to the actual precision before being inserted (if it is inserted in traditional mode, an error will be reported). In MySQL, decimal (or numerical) is used to represent fixed-point numbers.

Using floating point numbers to store data will cause errors. In scenarios with high accuracy requirements (such as currency), fixed point numbers should be used to store data. For example:

mysql> create table b (c1 float(10,2),c2 decimal(10,2));
Query OK, 0 rows affected (0.37 sec)

mysql> insert into b values (131072.32,131072.32);
Query OK, 1 row affected (0.00 sec)

mysql> select * from b;
+-----------+-----------+
| c1        | c2        |
+-----------+-----------+
| 131072.31 | 131072.32 |
+-----------+-----------+
1 row in set (0.00 sec)
Copy after login

四、日期类型

MySQL提供的常用的日期类型有:date、time、datetime、timestamp,日期类型的选用原则:

(1)、应根据实际需要选择能够满足应用的最小存储的日期类型;

(2)、如果要记录年月日时分秒,且年代比较久远,最好使用datetime类型;

(3)、如果记录的日期要被多时区的用户所使用,那么最好使用timestamp类型。


The above is the detailed content of Comprehensive understanding of data types in MySQL. For more information, please follow other related articles on the PHP Chinese website!

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)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

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.

See all articles