So when we create a check constraint, which we've done twice, MySQL automatically comes up with a
name for that constraint, which is what we see for our palindromes example palindromes underscore check
C-H k underscore one is violated, so it just goes in order.
If this is the first constraint that it finds and it's in the palindromes table, it's going to call
it palindromes check one.
If I had a second one, it would be palindromes check two, but we don't.
So that, I guess, is fine.
But what you'll see more often than not is a custom name added for the constraints.
And that looks like this.
We use the constraint keyword and then a name.
And you generally keep this short.
It's not going to be a whole message or something, but something like age over 18 and then the check
and then whatever check expression we want.
So this example is going to create the same check constraint as we had here for age.
Age has to be greater than 18.
But instead of being called parties, check one, the check constraint will be called age over 18.
So then we'll see something like age over 18 violated, which is a little bit better.
And also it makes it easier.
If you ever wanted to remove a constraint, I'm going to show you how to alter tables later in this
section.
It's easier when you know the name is age over 18.
Instead of having to guess partiers, check one or partiers check two.
So let me show you this.
Let's do the same thing for our users.
So I'll keep the original version.
And here's our second version where it will say age has to be greater than zero.
I'm going to get rid of that and then do a constraint down here with the constraint keyword.
We'll call it age positive or age not negative, whatever we want.
Like that check and then age greater than I guess is equal to zero.
Probably you can be zero years old and that should do it.
And I already have a user's table, so I'll call this users too, which is stupid, but fine.
And let's insert into users two and I'm just going to go ahead and insert something I know won't work.
So let's have username age.
And then values.
How about, I don't know, Chicken lady and age will be negative nine.
And what do we see?
Check constraint.
Age not negative is violated.
So slightly better compare that to when I insert into the original users table, which has the same
logic, but it's not a named constraint.
Did I get rid of right here?
This constraint here what we see is check constraint users check one is violated.
Same logic behind the scenes, but this is easier to work with or at least understand what went wrong.
So that's how we can add a name to a constraint.
We could do the same thing for a palindromes.
I don't know what we would call this though.
Word has to be a var char and then constraint
word.
Is, pal.
Maybe word is palindrome.
Sure.
And we'll call this palindromes, too.
And I'll make this table.
I need a comma.
There.
There we go.
Let's go ahead and make this table here.
Paste it in semicolon.
So now, if I try an insert into palindromes to.
Something that is not a palindrome.
Like mom is a palindrome that should work.
Just verify it does work.
But Mama is not a palindrome.
It's not the same forwards and backwards.
It doesn't work.
And this time we see Czech constraint.
Word is palindrome is violated compared to if we did palindromes one or palindromes the original table,
it just says palindromes check one is violated.
So that's how we can add those custom names to a constraint.
That also makes it easier.
Like I said, if you want to remove those constraints later on once we talk about alter table.