October 202305:5212.3k views

Database enums are weird

About

Enums are great for listing out specific choices because they give you the readability of a string but the underlying storage of an integer. There are a few gotchas though!

Transcript

Everything I said

00:00

I was reading the docs yesterday. This is a true story. This is not a bit. MySQL documentation. And I came across something that really surprised me. It had to do with the MAX function and the way that the MAX function interacts with ENUMS. And honestly, it was not what I expected. So we'll look at the, the right down the middle use case of ENUMS, and then I'll talk about some of the weird things and things you have to look out for.

00:26

ENUM is short for enumeration or enumerated list, and so it's a column type where you have a set defined list, and you get to read it as a string. So as a human, you can look at it and see a list of strings, but the database looks at it and sees a list of integers. And so the storage on disk is really compact, even though you get the readability of a string.

00:50

That's kind of awesome. You get a little bit of data validation because you can only choose one of the items from the list. But there are a few weird things about ENUMS that we're gonna look at. This is very much a right down the middle use case right here. You've got your orders table with a size column, which is an ENUM, and then these are the legal values that can go in that column.

01:11

One of these five values is the only thing that is allowed, extra small, small, medium, large, extra large. So you get this little bit of data validation from MySQL that the user has chosen a legal option. So let's create this table, and then we'll insert into orders just the size, and we'll insert a small.

01:31

Sounds good. And then if we read that back, we see the small was inserted, right? Interesting video. Thumbs up. Please like and subscribe. Like, that's... Who cares about that, right? But what is interesting is I told you, you get the readability of a string with the underlying storage of an integer. So is there a way to prove that?

01:53

You better believe that there is. So we'll select size, and then we'll select size plus zero, which is gonna just coerce it into the underlying integer. And we'll see that the label is small, but the integer is actually two. What if we were to insert, let's do an extra small, an extra large, and a large, and then we were to read these back?

02:18

You'll see that we have all these different underlying integers. Where do these integers come from? Let's go ahead and sort by that integer value. These integers are the position that we originally declared the ENUM column with. So extra small was declared first, so it got item or ID number one, position number one.

02:39

Small was two. Jumping to the end, extra large was the fifth item in the list, and so its underlying integer is five, even if the label is extra large. Why does this matter? Well, because when you order, when you order by an ENUM, it orders by the underlying integer and not the string.

03:03

So if we say select size from orders order by size, you'll see this, and this looks very bizarre, doesn't it? These two Xs, in my mind, those should go together. If I'm ordering by size, I would expect the two Xs to be ordered near each other.

03:24

But if we turn this back on, we'll see it's actually ordering, it's actually ordering by that integer, and so this makes a lot more sense. So this was the first surprise to me with ENUMS. This is not the most surprising part. This is the first surprise, is that when you order, you could get back a list that looks unordered, but in fact is ordered by a thing you cannot see unless you do this weird type juggling thing that we did, right?

03:50

This may be exactly what you're looking for. If you need to order a list in a specific order, an ENUM is great for that. Just make sure that you declare it in the order that you want it ordered. The thing that actually surprised me, the thing that I discovered in the docs was if you put a MAX or MIN function on it, for some reason, instead of using the underlying integer, it uses the string for that.

04:16

So you're ordering by the underlying integer, but if you run a MAX over it, it uses the string. That is... It makes no sense to me. Let me see if I can prove this one to you. If we were to say select MAX size from orders, based on everything we just learned about ordering, you think it would be extra large because that's position number five. In fact, it's extra small because alphabetically, that is the max string.

04:42

That is the biggest string. That is the string latest in the alphabet, even though it is in position number one. So ENUMS, kind of weird, right? Feels a little bit inconsistent to me to have the ordering work on the underlying integer and the MAX, MIN to work on the display string. But hey, if it's fine with you, it's fine with me. They're really powerful.

05:02

They're really useful. There's one other thing that you'll need to remember, and that's that you cannot add a new option. You cannot change the options at all without altering the schema of your table, and that can be kind of a pain. If that is a pain, you might consider PlanetScale. We have zero downtime migrations. But you do need to keep that in mind.

05:23

So that's a, that's one strike against it, uh, as compared to a character column or even just an integer column, is you would have to alter that table. I hope that this was helpful to you. I read every single comment, and so does my boss, so please enumerate the reasons you liked this video, and let me know what you want to see next. You can find us at planetscale.com/youtube.

05:46

Until next time, see ya.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues