search
HomeDatabaseMysql TutorialHow to show that each MySQL enumeration has an index value?

How to show that each MySQL enumeration has an index value?

In fact, elements listed in the ENUM column specification are assigned index numbers starting from 1. The term "index" here refers to the position in the list of enumeration values, they have nothing to do with the table index. With the following example, we can show that each MySQL enumeration has an index value -

By inserting the index number instead of the enumeration value

We can show that by inserting their index number instead of writing Enter a value to insert a value in the ENUM column. For example, in the table below, we have two ENUM values ​​'pass' and 'fail'. Since 'pass' is written first in the enumeration list, it gets the index number '1', and the index number of 'fail' is '2'. Now, we can also insert the value by inserting the index number as shown below -

mysql> Insert into marks(id,name,result)values(103,'Daksh','1');
Query OK, 1 row affected (0.06 sec)

mysql> Insert into marks(id,name,result)values(104,'Shayra','2');
Query OK, 1 row affected (0.07 sec)

mysql> Select * from marks;
+-----+---------+--------+
| id  | Name    | Result |
+-----+---------+--------+
| 101 | Aarav   | Pass   |
| 102 | Yashraj | Fail   |
| 103 | Daksh   | Pass   |
| 104 | Shayra  | Fail   |
+-----+---------+--------+
4 rows in set (0.00 sec)

In the above query, we have used the index number 1 and 2 to represent the enumeration values ​​pass and fail respectively.

By inserting an empty string instead of an enumeration value

The index value of an empty string is 0. The SQL mode cannot be TRADITIONAL, STRICT_TRANS_TABLES or STRICT_ALL_TABLES before inserting the empty string. We can understand it with the following example −

mysql> SET SESSION sql_mode = '';
Query OK, 0 rows affected (0.00 sec)

mysql> Insert into marks(id, name, result) values(105,'Yashraj','');
Query OK, 1 row affected, 1 warning (0.06 sec)

The above query will insert an empty string at the position of the enum value. MySQL inserts an empty string with the following warning.

mysql> Show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1265 | Data truncated for column 'Result' at row 1 |
+---------+------+---------------------------------------------+
1 row in set (0.00 sec)

Now when we inspect the table, we can see that there is an empty string in the ENUM field.

mysql> Select * from marks;
+-----+---------+--------+
| id  | Name    | Result |
+-----+---------+--------+
| 101 | Aarav   | Pass   |
| 102 | Yashraj | Fail   |
| 103 | Daksh   | Pass   |
| 104 | Shayra  | Fail   |
| 105 | Yash    |        |
+-----+---------+--------+
5 rows in set (0.00 sec)

mysql> Select result+0 As result_index from marks;
+--------------+
| result_index |
+--------------+
| 1            |
| 2            |
| 1            |
| 2            |
| 0            |
+--------------+
5 rows in set (0.01 sec)
From the output of above query, it is clear that the index value of the empty string is 0.

By inserting NULL instead of the enumeration value

We can insert NULL in the place of the enumeration value because we do not specify NOT NULL for the ENUM column. The index value of NULL is NULL. For example, with the help of the following query, we insert NULL in the enum column of table "result" and can check its index value.

mysql> Create table result(Id INT PRIMARY KEY NOT NULL, Name Varchar(10), GradeENUM('POOR','GOOD'));
Query OK, 0 rows affected (0.25 sec)

mysql> Insert into result(id, name, grade) values(100, 'Rahul', NULL);
Query OK, 1 row affected (0.06 sec)

The above query inserted NULL value at the position of the enumeration value, you can check it using the following help query -

mysql> Select * from result;
+-----+-------+-------+
| Id  | Name  | Grade |
+-----+-------+-------+
| 100 | Rahul | NULL  |
+-----+-------+-------+
1 row in set (0.00 sec)

Now, with the help of the next query, we can observe the NULL The index value is NULL.

mysql> Select Grade+0 As Grade_index from result;
+-------------+
| Grade_index |
+-------------+
| NULL        |
+-------------+
1 row in set (0.00 sec)

The above is the detailed content of How to show that each MySQL enumeration has an index value?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL?How do you handle large datasets in MySQL?Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement?How do you drop a table in MySQL using the DROP TABLE statement?Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns?How do you create indexes on JSON columns?Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do you represent relationships using foreign keys?How do you represent relationships using foreign keys?Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools