1 00:00:00,690 --> 00:00:02,540 So we're moving on in this video. 2 00:00:02,550 --> 00:00:04,890 We're talking about default values. 3 00:00:05,190 --> 00:00:07,200 What's up with this part right here? 4 00:00:07,230 --> 00:00:07,890 Default. 5 00:00:07,890 --> 00:00:12,990 So as we talked about a little bit in the last video, it's helpful to be able to decide or to set default 6 00:00:12,990 --> 00:00:13,530 values. 7 00:00:13,530 --> 00:00:17,910 So if something is not specified that there will be a fallback. 8 00:00:18,690 --> 00:00:19,740 And here's how we do it. 9 00:00:20,190 --> 00:00:22,380 We use default. 10 00:00:23,010 --> 00:00:28,170 So you can see both of these when we're defining a table default and then we specify a default. 11 00:00:28,170 --> 00:00:35,300 So for name, if you leave off name, I just said, okay, we'll call it unnamed or name to be determined. 12 00:00:35,310 --> 00:00:41,820 Sometimes if this is a cat shelter in the real world, sometimes they decide on a name a little bit 13 00:00:41,820 --> 00:00:47,610 later once they figure out the cat's personality, and then for age, if this is more of a silly example. 14 00:00:47,610 --> 00:00:52,020 But if we wanted to if we didn't specify age, we could always set it to be 99. 15 00:00:52,920 --> 00:00:56,820 Not sure why, but for the sake of this exercise, we're going to do that. 16 00:00:57,060 --> 00:00:58,380 So let's give it a shot. 17 00:00:58,560 --> 00:01:01,410 Re Making a new cat's table cats three. 18 00:01:03,690 --> 00:01:04,459 Here we go. 19 00:01:04,470 --> 00:01:11,340 So create table cats three and I'll do a new line here. 20 00:01:13,470 --> 00:01:17,340 And we will have name for our car. 21 00:01:18,120 --> 00:01:19,170 Let's do 20. 22 00:01:19,200 --> 00:01:20,820 Even though my slides say 100. 23 00:01:20,820 --> 00:01:21,540 It's okay. 24 00:01:21,690 --> 00:01:24,900 Default and our default will be. 25 00:01:25,900 --> 00:01:26,360 See. 26 00:01:26,700 --> 00:01:29,070 No name provided. 27 00:01:29,280 --> 00:01:29,970 How about that? 28 00:01:30,930 --> 00:01:34,500 It's important that whatever you put here matches this data type. 29 00:01:34,500 --> 00:01:38,130 So we couldn't say the default is the number 100. 30 00:01:39,360 --> 00:01:41,340 And then we'll have our comma. 31 00:01:43,230 --> 00:01:46,200 Age and age is an int. 32 00:01:47,580 --> 00:01:50,550 Default should be 99. 33 00:01:51,150 --> 00:01:51,900 Perfect. 34 00:01:52,770 --> 00:01:53,730 Close that. 35 00:01:55,020 --> 00:01:59,070 Now if we do our describe cats three. 36 00:02:00,090 --> 00:02:00,900 Here we go. 37 00:02:01,230 --> 00:02:03,260 So we have default. 38 00:02:03,270 --> 00:02:07,050 No name provided and default 99 for H. 39 00:02:07,770 --> 00:02:08,490 Awesome. 40 00:02:09,000 --> 00:02:11,310 So now let's give it a shot. 41 00:02:12,000 --> 00:02:26,010 Let's do insert into cat three and I'll do age only and no name and age will be let's do 13. 42 00:02:26,220 --> 00:02:27,140 Just like that. 43 00:02:27,150 --> 00:02:28,410 What do you think will happen? 44 00:02:28,440 --> 00:02:31,920 I'm going to insert a new cat into Cats three, which is right here. 45 00:02:33,420 --> 00:02:41,490 Only providing age and name has a default value of no name provided I did it. 46 00:02:41,520 --> 00:02:42,960 Now let's look. 47 00:02:42,990 --> 00:02:49,650 Select star from Cats three and I get no name provided. 48 00:02:49,890 --> 00:02:50,550 Perfect. 49 00:02:50,550 --> 00:02:52,910 So we have a default value that's working great. 50 00:02:52,920 --> 00:02:54,840 We could do the same thing for age. 51 00:02:55,080 --> 00:02:59,010 Feel free to skip this if you feel like you know exactly what's going to happen. 52 00:03:00,300 --> 00:03:00,930 I'll leave. 53 00:03:00,930 --> 00:03:03,330 Let's leave both off this time and certainty. 54 00:03:03,330 --> 00:03:05,460 Catch three values. 55 00:03:08,330 --> 00:03:14,740 Now when we select, we have another unnamed cat this time who is 99 years old by default. 56 00:03:14,750 --> 00:03:21,860 So the next thing that I want to talk about here is another version of creating a table for cats for 57 00:03:21,890 --> 00:03:22,490 this time. 58 00:03:22,490 --> 00:03:26,840 And I combined not null alongside default. 59 00:03:26,870 --> 00:03:27,890 Otherwise it's the same. 60 00:03:27,890 --> 00:03:35,600 So we have name of our car default is unnamed age int default is 99 but I added in not null. 61 00:03:37,070 --> 00:03:41,180 And as I wrote here, you might be thinking isn't this redundant? 62 00:03:41,630 --> 00:03:45,620 Because if we have a default value, it's already saying it won't be null. 63 00:03:46,970 --> 00:03:57,980 If we go back and we look at cats, regular cats, if we do an insert into cats just like this and if 64 00:03:57,980 --> 00:04:08,960 you can remember all the way back to cats, we have no default provided and I do select star from cats. 65 00:04:10,280 --> 00:04:13,670 We get no, no, there's no default values. 66 00:04:14,090 --> 00:04:21,920 And then when we have our Cat three, when I insert something like this right here, insert into cats 67 00:04:21,920 --> 00:04:23,990 three, it's totally empty. 68 00:04:24,350 --> 00:04:25,880 We have those defaults. 69 00:04:25,880 --> 00:04:27,620 So when I select star 70 00:04:30,080 --> 00:04:40,040 oops from cat three, we get no name provided 99 So why would we want to add not null? 71 00:04:40,040 --> 00:04:45,470 And the answer is that we could still manually set something to be null. 72 00:04:46,010 --> 00:04:58,910 So if I did that here, let's do insert into cats three and I'll do name and age, name and age values. 73 00:04:59,330 --> 00:05:06,200 And I will say there's no name or let's say name is what's another state in Montana. 74 00:05:06,380 --> 00:05:08,000 It's not too bad for a cat's name. 75 00:05:08,030 --> 00:05:09,320 Great state, by the way. 76 00:05:10,010 --> 00:05:16,280 And we'll leave age as null, explicitly null, just like that. 77 00:05:17,900 --> 00:05:26,960 Let's do select star from Cat three and you can see that there was nothing stopping us from setting 78 00:05:26,960 --> 00:05:28,040 age to be null. 79 00:05:28,160 --> 00:05:30,050 All that we have is a default value. 80 00:05:30,050 --> 00:05:35,000 If you don't specify something, it will be 99, but you're allowed to say no. 81 00:05:35,630 --> 00:05:38,840 I'm allowed to say this is explicitly an empty value. 82 00:05:39,890 --> 00:05:42,620 So that's where this comes in. 83 00:05:42,860 --> 00:05:47,000 Name var not null and a default. 84 00:05:47,420 --> 00:05:51,230 And I won't make you sit through me typing that whole thing so I'll just copy it. 85 00:05:52,100 --> 00:05:53,720 But to walk through it again. 86 00:05:53,840 --> 00:05:55,010 Same thing here. 87 00:05:55,520 --> 00:05:56,840 Same with the default we just added. 88 00:05:56,840 --> 00:05:58,430 Not null and not no. 89 00:05:59,510 --> 00:06:01,790 So let's go ahead, paste this in. 90 00:06:03,170 --> 00:06:04,550 This is cats for. 91 00:06:06,190 --> 00:06:15,640 This time if I do describe cats for we now have null is no on both and we have a default on both. 92 00:06:16,270 --> 00:06:17,740 So we're almost there. 93 00:06:17,740 --> 00:06:19,930 I know it's been a little bit of a long video. 94 00:06:19,960 --> 00:06:31,150 Let's do a insert into cats for and we will specify name and then age. 95 00:06:32,260 --> 00:06:35,830 And this time if I do values. 96 00:06:36,100 --> 00:06:36,640 Oops. 97 00:06:37,820 --> 00:06:41,480 And I say, name is Watson. 98 00:06:41,630 --> 00:06:42,460 Let's do Cali. 99 00:06:42,470 --> 00:06:43,030 Fine. 100 00:06:43,740 --> 00:06:47,360 Sort of a state name and age is null. 101 00:06:47,600 --> 00:06:48,770 If I do this. 102 00:06:50,310 --> 00:06:52,080 This time we get an error. 103 00:06:52,350 --> 00:06:56,730 So not just a warning, but an error saying it cannot be null. 104 00:06:56,730 --> 00:06:59,160 And that's what not null specifies. 105 00:06:59,820 --> 00:07:04,320 So important to note that you'll use not know pretty often. 106 00:07:04,320 --> 00:07:09,780 It's a way of basically requiring that something has a value, but also saying that it's not allowed 107 00:07:09,780 --> 00:07:13,710 to be null itself, which I also included as a slide here. 108 00:07:14,250 --> 00:07:17,340 In Cat three, we could manually insert something as No.