---
title: Using MySQL to stop freeloaders (in only 8 minutes)
url: "https://aaronfrancis.com/videos/using-mysql-to-stop-freeloaders-in-only-8-minutes"
path: "/videos/using-mysql-to-stop-freeloaders-in-only-8-minutes"
type: video
author: Aaron Francis
published: 2023-10-19
topic: Databases
keywords: [mysql, generated columns, email addresses, uniqueness constraints, planetscale, preventing fraud, users table, database design]
youtube_url: "https://www.youtube.com/watch?v=goC5BdyCvms"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Using MySQL to stop freeloaders (in only 8 minutes)

## Chapters

- Infinite email addresses
- Users table example
- How to prevent it
- Coming up with the formula
- The generated column
- Enforcing uniqueness
- Is this a good idea?

## Transcript

**Aaron Francis:** You know how when there's a SaaS with a free trial that's kind of limited and you wanna work around it, you can just add a plus sign to your email address and sign up as if you're a new user? No? No, me either. I would never do such a thing. So for those of you that don't know, for those of you that are team good guys like me, what you can do is if you have a Gmail address or a Google Apps for domain, whatever they call it, if you have one of those addresses, you can add a plus and then put anything you want in your email address, and it'll still go to you.

**Aaron Francis:** If my email was Aaron@gmail.com, which unfortunately it's not, I could add +YouTube, I could add +like, +subscribe, and all of these email addresses would go directly to Aaron@gmail.com. This works with all G Suite email addresses. So if your company runs on G Suite, let's say your email is Aaron@planetscale.com, which may or may not be right, but please don't email me, you could do that same trick with your company account.

**Aaron Francis:** And now you have unlimited free addresses to sign up for things, and it all still goes to your inbox. So now you know what the bad guys are doing. Not me, the bad guys. I'm team good guys. We're gonna be team good guys. We're gonna figure out how to stop this, prevent this, catch this at the database layer. Is the database layer the right place to do this? Eh, I could go either way on that, but it's gonna be interesting and it's gonna be fast.

**Aaron Francis:** So the first thing that we're gonna do is just create a table called users, and in that table, we're just gonna put one column. We'll say email. It is a string, two fifty-five long, which seems long enough. And we'll go ahead and insert into users the email column, a value of, we'll just use that Aaron@gmail address.

**Aaron Francis:** So if we run that, we can also insert plus one, plus two, plus three. Let's read all of those back. Select star from users, and you'll see there are all of the email addresses that are functionally exactly the same, except for that little plus trick. So now we need to start looking at how we're gonna prevent this from happening.

**Aaron Francis:** And what I don't wanna do, what I feel like is kind of user hostile, is to drop out the plus from whatever they put in. They may have a perfectly legitimate reason for putting a plus in there. They may be team good guys, and they just like to filter their inbox in a certain way. So we're not gonna mess with that. What we're going to do instead is add an additional column that is derived off of the email column, but removes the plus sign so that we can add a unique constraint to that.

**Aaron Francis:** And we're gonna do that with something called a generated column. You've heard me talk about generated columns before. I've, I've talked about them in a few other videos. And if you haven't seen the other videos, what are you doing? Help me out here. A generated column is a column where we tell MySQL, "Here's a formula. Here's a formula to calculate the value that I want you to put in this column." And then MySQL takes that formula and says, "Yes, I will do this forever." So we don't control that, the value in that column anymore.

**Aaron Francis:** We can't change it. MySQL is gonna keep that up to date for us, and that's exactly what we wanna do here. So now we gotta come up with our formula. What formula are we gonna hand MySQL and say, "Keep this up to date"? Let's start with select email from users. That's a good start. And what we'll do here is we're gonna use this substring-index function. And what this does is you give it a delimiter and a number of occurrences, and this will give you the string before the delimiter.

**Aaron Francis:** So now we have chopped off the domain, which is a pretty good start. And so what we're gonna do is we're just gonna keep going with substring-index. We're gonna say substring-index of that value, so we'll indent that, and we'll say, actually, now the delimiter is the plus sign. And we still want just one occurrence. We can pretty that up. And if we run that, there we go.

**Aaron Francis:** Okay, so now we have just the username part of that email without the plus sign. And if-- Let's go ahead and bring back email here. You can see that it works when there is a plus sign and when there's not a plus sign. So this is what we're looking for. But along the way, we have lost the domain. So if we come down here, and we'll add substring-index again, and this time we're gonna go from the end of the string working backwards.

**Aaron Francis:** And if we look at that, we have a problem, a trailing comma. So if we look at that, we'll see we've got a domain here now, so we gotta put those two guys together. And we will use con... Let's go all caps. We'll use CONCAT with separator, and our separator is going to be the at sign. And we're gonna put this value and this value together and there we go.

**Aaron Francis:** So now we've reconstructed an email address, but we've dropped out that plus sign delimiter or that plus sign modifier in there. Now that we have our formula, we can plug that into a generated column, and MySQL will keep that up to date, and we can do queries against that. But the question is, when we create this generated column, do we wanna create it as a virtual column or a stored column?

**Aaron Francis:** A virtual generated column is the default, and what that means is anytime you need this new value, this new generated column, MySQL's just gonna recompute it. A stored generated column is actually written to the disk, and so it doesn't have any recomputation time, but it does take up a little bit of disk space.

**Aaron Francis:** And so in this case, we're gonna make it virtual because it's pretty fast to calculate. It's just a few string functions. There are times where it's very expensive to calculate, and you would wanna write that to disk. The other thing is we're gonna put an index on this, and so this value actually will be written into the B-tree of the index.

**Aaron Francis:** And so it will be stored even though we're gonna declare it virtual. It's just gonna be stored in the structure of the index. So to create this guy, we're gonna take this column that we derived together. We'll cut that out, and we'll say alter table users, add column. And we can just say, let's call it email normal. We'll make it the same data type of character column, and then to declare it as a generated column, we say generated always as, and then we open the parentheses, and we put our formula inside

**Aaron Francis:** of it. So if we run that and then we read it back, select star from users, users, you'll see we have our email column and our email normal column, and so now MySQL is maintaining that column for us. The next question is, how are we going to enforce uniqueness? Eh, we're just gonna do it the same way we always would. We're gonna put a unique index on it. The question, I think, before we do this, the question is, is that pushing too much business logic down to the database layer?

**Aaron Francis:** Is enforcing a unique normal email at the database layer too much? I don't know. If you decide that that's too much validation that happens at the database layer, you could just add an index on it and do all of your validations on the app side, but use that index for a lot faster lookups. I'm just gonna put a unique index on it because I want to, and that's kind of the whole point of the video.

**Aaron Francis:** So let me show you how we would do that. The first thing we would do is just delete the entire table 'cause there are already duplicates in there. That's fine. Now we can say alter table users add unique index on email normal. That's our generated column. So we'll run that, and then we'll say insert into users, uh, into the email column values aaron@gmail.com.

**Aaron Francis:** We run that. That works just fine. We run plus one. That fails. Duplicate entry for key users email_normal. So we've done it. We have used a generated column to prevent people from using this plus trick to circumvent our free tier. Is this a good idea for your product? I don't know.

**Aaron Francis:** Is it totally awesome? Yeah, it is totally awesome. It's up to you to decide if this is a good idea for your product. It's up to me to decide if this is totally awesome, and I have decided this is totally awesome. So please let me know how awesome you think this is. I read every single comment. I show the funniest ones to my wife, and she's like, "Shouldn't you be working right now?" So please leave a comment.

**Aaron Francis:** Let me know what you think. Let me know what you want to see next. Visit us at planetscale.com/youtube for more, and until next time, see ya.

---

## Related on aaronfrancis.com

- [Making videos developers actually want to watch](/podcast/making-videos-developers-actually-want-to-watch-6e3ad549) *(Podcast · 2024-06-03 · shared: mysql, planetscale, database design)*
- [MySQL for application developers](/about/speaking/mysql-for-application-developers) *(Talk · 2023-04-20 · shared: mysql, planetscale, database design)*
- [MySQL for Developers with Aaron Francis](/podcast/mysql-for-developers-with-aaron-francis-n6unbd) *(Podcast · 2023-04-04 · shared: mysql, planetscale, generated columns)*
- [MySQL for Developers](/courses) *(Course · 2023-03-01 · shared: mysql, database design, planetscale)*
- [You don't always need JOINs](/videos/you-dont-always-need-joins) *(Video · 2023-11-22 · shared: mysql, planetscale, database design)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[Mastering Postgres](https://databaseschool.com)** (overview on site: [/courses](/courses)): Deep-dive video course on PostgreSQL indexing, query performance, JSONB, full-text search, locking, and production operations.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `mysql` ([M-N](/llms/keywords/m-n.txt)), `generated columns` ([F-G](/llms/keywords/f-g.txt)), `email addresses` ([D-E](/llms/keywords/d-e.txt)), `uniqueness constraints` ([T-Z](/llms/keywords/t-z.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
