Home > Database > Mysql Tutorial > Why shouldn't we store numbers into MySQL ENUM columns?

Why shouldn't we store numbers into MySQL ENUM columns?

WBOY
Release: 2023-09-04 12:29:09
forward
1274 people have browsed it

为什么我们不应该将数字存储到 MySQL ENUM 列中?

MySQL stores ENUM values ​​internally as integer keys (index numbers) to reference ENUM members. The main reason for not storing integer values ​​in ENUM columns is that obviously MySQL ends up referencing the index rather than the value and vice versa.

Example

The following example may clarify:

mysql> Create table enmtest(Val ENUM('0','1','2'));
Query OK, 0 rows affected (0.18 sec)

mysql> Insert into enmtest values('1'),(1);
Query OK, 2 rows affected (0.19 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> Select * from enmtest;
+-----+
| Val |
+-----+
| 1   |
| 0   |
+-----+
2 rows in set (0.00 sec)
Copy after login

Here, we inserted '1' as a string and accidentally also inserted a The number 1 without quotes. MySQL confusingly uses our numeric input as the index value, which is an internal reference to the first item in the member list (i.e. 0).

The above is the detailed content of Why shouldn't we store numbers into MySQL ENUM columns?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template