By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Navigating MySQL Data Types: Sets and Enums | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Navigating MySQL Data Types: Sets and Enums | HackerNoon
Computing

Navigating MySQL Data Types: Sets and Enums | HackerNoon

News Room
Last updated: 2025/07/15 at 7:10 PM
News Room Published 15 July 2025
Share
SHARE

Related Articles

Introduction

This article continues my series on MySQL data types. Previously, I covered numeric types and date/time types. This article focuses on SET and ENUM types, specialized data types that can be used for limited lists of data and make schemas more understandable, but can also cause problems if not used carefully. I’ll first look at what they are, then give examples of common mistakes and how to avoid them.

What Are ENUM and SET Types?

ENUM

  • A string object that can hold a single value chosen from a predefined list.
  • Example use case: order statuses ('pending', 'paid', 'shipped', 'cancelled').
  • Internally stored as an integer index (1-based).
  • Supports up to 65,535 elements (like SMALLINT).

SET

  • A string object that can hold any combination of values from a predefined list.
  • Example use case: user permissions ('read', 'write', 'delete').
  • Internally stored as a bitmap (each bit represents whether a value is present).
  • Supports up to 64 elements (like BIT(64)).

Syntax and Examples

Here is an example table for orders with status field that used ENUM type to specify available order statuses:

CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
status ENUM(‘pending’,’paid’,’shipped’,’cancelled’) NOT NULL DEFAULT ‘pending’
);

To insert two orders to the table:

INSERT INTO orders () VALUES ();
INSERT INTO orders (status) VALUES (‘paid’);

Selecting all rows with an additional numeric representation of the status field:

SELECT id, status, status+0 FROM orders;
+—-+———+———-+
| id | status | status+0 |
+—-+———+———-+
| 1 | pending | 1 |
| 2 | paid | 2 |
+—-+———+———-+
2 rows in set (0.000 sec)

Here is an example table with SET field:

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
permissions SET(‘read’,’write’,’delete’) NOT NULL
);

Inserting a user with multiple permissions:

INSERT INTO users (permissions) VALUES (‘read,write’);

Common Pitfalls And How To Avoid Them

Of course, I can begin by noting that if you are using very old MySQL versions or settings that are not sufficiently strict, you may overlook errors related to writing values that are not included in the field definition. In this case, an empty value will be written into the field. But I hope you can use fairly recent MySQL versions. I also hope that you understand these field types range limitation and do not try to store more than 64 values in sets.

So, the most common problem is related to changes in field definitions. Imagine someone adding a new value to the middle of the ENUM variants list:

CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
status ENUM(‘pending’,’paid’,’shipped’) NOT NULL DEFAULT ‘pending’
);
INSERT INTO orders (status) VALUES (‘paid’);
SELECT id, status, status+0 FROM orders;
+—-+———+———-+
| id | status | status+0 |
+—-+———+———-+
| 1 | paid | 2 |
+—-+———+———-+
1 rows in set (0.000 sec)
ALTER TABLE orders MODIFY status ENUM(‘pending’,’accepted’,’paid’,’shipped’) NOT NULL DEFAULT ‘pending’;

As MySQL stores ENUM fields as integers internally, here is what we will have:

SELECT id, status, status+0 FROM orders;

+—-+———-+———-+
| id | status | status+0 |
+—-+———-+———-+
| 1 | accepted | 2 |
+—-+———-+———-+
1 rows in set (0.000 sec)

OOOPS! You need to remember that reordering or inserting a new variant anywhere but the end changes the index mapping, leading to data mismatches.

The second thing I want to mention is that while MySQL stores enums and sets as integers, it sorts and compares them as collated strings when selecting. This can surprise developers expecting numeric order.

Conclusion

ENUM and SET can be powerful tools for constraining values directly in your schema. But with their convenience come caveats around ordering, defaults, and silent coercion. By understanding their behavior and following best practices, you can avoid many of the common pitfalls and keep your data consistent.

Stay tuned for more articles in this series exploring other MySQL data types and how to use them effectively.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Data Centers Are 'AI Factories.' Google and Meta Are Spending Big Bucks to Build Them
Next Article Faster Qi2.2 wireless power banks are on the way
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

👨🏿‍🚀 Daily – Mediamax, max layoffs |
Computing
Google announces Pixel 10 launch event will take place on August 20
News
Mastering the Art of Social Media Storytelling
Computing
Galaxy Watch users might want to stay away from testing QPR1 betas
News

You Might also Like

Computing

👨🏿‍🚀 Daily – Mediamax, max layoffs |

3 Min Read
Computing

Mastering the Art of Social Media Storytelling

20 Min Read
Computing

Employee Advocacy on Social Media: A Complete Guide

18 Min Read
Computing

Cisco Warns of Critical ISE Flaw Allowing Unauthenticated Attackers to Execute Root Code

3 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?