1
00:00:00,210 --> 00:00:06,480
So when we create a check constraint, which we've done twice, MySQL automatically comes up with a

2
00:00:06,480 --> 00:00:13,860
name for that constraint, which is what we see for our palindromes example palindromes underscore check

3
00:00:13,890 --> 00:00:18,680
C-H k underscore one is violated, so it just goes in order.

4
00:00:18,690 --> 00:00:23,610
If this is the first constraint that it finds and it's in the palindromes table, it's going to call

5
00:00:23,610 --> 00:00:25,370
it palindromes check one.

6
00:00:25,380 --> 00:00:28,470
If I had a second one, it would be palindromes check two, but we don't.

7
00:00:28,620 --> 00:00:31,260
So that, I guess, is fine.

8
00:00:31,260 --> 00:00:36,870
But what you'll see more often than not is a custom name added for the constraints.

9
00:00:36,870 --> 00:00:38,030
And that looks like this.

10
00:00:38,040 --> 00:00:41,850
We use the constraint keyword and then a name.

11
00:00:41,850 --> 00:00:43,620
And you generally keep this short.

12
00:00:43,620 --> 00:00:49,830
It's not going to be a whole message or something, but something like age over 18 and then the check

13
00:00:49,830 --> 00:00:52,350
and then whatever check expression we want.

14
00:00:52,560 --> 00:00:58,950
So this example is going to create the same check constraint as we had here for age.

15
00:00:58,950 --> 00:01:00,570
Age has to be greater than 18.

16
00:01:00,570 --> 00:01:07,680
But instead of being called parties, check one, the check constraint will be called age over 18.

17
00:01:07,800 --> 00:01:13,880
So then we'll see something like age over 18 violated, which is a little bit better.

18
00:01:13,890 --> 00:01:15,570
And also it makes it easier.

19
00:01:15,570 --> 00:01:20,220
If you ever wanted to remove a constraint, I'm going to show you how to alter tables later in this

20
00:01:20,220 --> 00:01:20,880
section.

21
00:01:20,880 --> 00:01:24,420
It's easier when you know the name is age over 18.

22
00:01:24,420 --> 00:01:30,480
Instead of having to guess partiers, check one or partiers check two.

23
00:01:30,810 --> 00:01:32,340
So let me show you this.

24
00:01:32,340 --> 00:01:35,820
Let's do the same thing for our users.

25
00:01:35,820 --> 00:01:38,460
So I'll keep the original version.

26
00:01:38,460 --> 00:01:42,540
And here's our second version where it will say age has to be greater than zero.

27
00:01:42,570 --> 00:01:47,460
I'm going to get rid of that and then do a constraint down here with the constraint keyword.

28
00:01:48,090 --> 00:01:54,180
We'll call it age positive or age not negative, whatever we want.

29
00:01:55,610 --> 00:02:02,120
Like that check and then age greater than I guess is equal to zero.

30
00:02:02,690 --> 00:02:07,090
Probably you can be zero years old and that should do it.

31
00:02:07,100 --> 00:02:13,040
And I already have a user's table, so I'll call this users too, which is stupid, but fine.

32
00:02:13,040 --> 00:02:18,650
And let's insert into users two and I'm just going to go ahead and insert something I know won't work.

33
00:02:18,650 --> 00:02:21,050
So let's have username age.

34
00:02:21,730 --> 00:02:23,230
And then values.

35
00:02:23,740 --> 00:02:31,450
How about, I don't know, Chicken lady and age will be negative nine.

36
00:02:32,110 --> 00:02:33,240
And what do we see?

37
00:02:33,250 --> 00:02:34,570
Check constraint.

38
00:02:34,600 --> 00:02:36,880
Age not negative is violated.

39
00:02:36,880 --> 00:02:43,690
So slightly better compare that to when I insert into the original users table, which has the same

40
00:02:43,690 --> 00:02:46,630
logic, but it's not a named constraint.

41
00:02:47,590 --> 00:02:49,090
Did I get rid of right here?

42
00:02:49,120 --> 00:02:55,450
This constraint here what we see is check constraint users check one is violated.

43
00:02:55,450 --> 00:03:01,690
Same logic behind the scenes, but this is easier to work with or at least understand what went wrong.

44
00:03:02,890 --> 00:03:05,110
So that's how we can add a name to a constraint.

45
00:03:05,110 --> 00:03:07,360
We could do the same thing for a palindromes.

46
00:03:08,680 --> 00:03:10,540
I don't know what we would call this though.

47
00:03:10,540 --> 00:03:14,200
Word has to be a var char and then constraint

48
00:03:16,450 --> 00:03:17,590
word.

49
00:03:18,430 --> 00:03:19,900
Is, pal.

50
00:03:19,930 --> 00:03:22,550
Maybe word is palindrome.

51
00:03:22,570 --> 00:03:23,170
Sure.

52
00:03:23,440 --> 00:03:25,390
And we'll call this palindromes, too.

53
00:03:25,810 --> 00:03:27,430
And I'll make this table.

54
00:03:27,430 --> 00:03:28,070
I need a comma.

55
00:03:28,090 --> 00:03:28,410
There.

56
00:03:28,420 --> 00:03:29,080
There we go.

57
00:03:30,040 --> 00:03:32,110
Let's go ahead and make this table here.

58
00:03:32,500 --> 00:03:34,560
Paste it in semicolon.

59
00:03:34,570 --> 00:03:38,110
So now, if I try an insert into palindromes to.

60
00:03:38,980 --> 00:03:40,660
Something that is not a palindrome.

61
00:03:41,050 --> 00:03:44,470
Like mom is a palindrome that should work.

62
00:03:45,140 --> 00:03:46,730
Just verify it does work.

63
00:03:46,730 --> 00:03:48,800
But Mama is not a palindrome.

64
00:03:48,800 --> 00:03:50,620
It's not the same forwards and backwards.

65
00:03:50,630 --> 00:03:51,800
It doesn't work.

66
00:03:51,800 --> 00:03:53,690
And this time we see Czech constraint.

67
00:03:53,690 --> 00:04:01,520
Word is palindrome is violated compared to if we did palindromes one or palindromes the original table,

68
00:04:01,550 --> 00:04:04,700
it just says palindromes check one is violated.

69
00:04:04,970 --> 00:04:07,640
So that's how we can add those custom names to a constraint.

70
00:04:07,640 --> 00:04:08,780
That also makes it easier.

71
00:04:08,780 --> 00:04:13,670
Like I said, if you want to remove those constraints later on once we talk about alter table.

