So we're moving on in this video.
We're talking about default values.
What's up with this part right here?
Default.
So as we talked about a little bit in the last video, it's helpful to be able to decide or to set default
values.
So if something is not specified that there will be a fallback.
And here's how we do it.
We use default.
So you can see both of these when we're defining a table default and then we specify a default.
So for name, if you leave off name, I just said, okay, we'll call it unnamed or name to be determined.
Sometimes if this is a cat shelter in the real world, sometimes they decide on a name a little bit
later once they figure out the cat's personality, and then for age, if this is more of a silly example.
But if we wanted to if we didn't specify age, we could always set it to be 99.
Not sure why, but for the sake of this exercise, we're going to do that.
So let's give it a shot.
Re Making a new cat's table cats three.
Here we go.
So create table cats three and I'll do a new line here.
And we will have name for our car.
Let's do 20.
Even though my slides say 100.
It's okay.
Default and our default will be.
See.
No name provided.
How about that?
It's important that whatever you put here matches this data type.
So we couldn't say the default is the number 100.
And then we'll have our comma.
Age and age is an int.
Default should be 99.
Perfect.
Close that.
Now if we do our describe cats three.
Here we go.
So we have default.
No name provided and default 99 for H.
Awesome.
So now let's give it a shot.
Let's do insert into cat three and I'll do age only and no name and age will be let's do 13.
Just like that.
What do you think will happen?
I'm going to insert a new cat into Cats three, which is right here.
Only providing age and name has a default value of no name provided I did it.
Now let's look.
Select star from Cats three and I get no name provided.
Perfect.
So we have a default value that's working great.
We could do the same thing for age.
Feel free to skip this if you feel like you know exactly what's going to happen.
I'll leave.
Let's leave both off this time and certainty.
Catch three values.
Now when we select, we have another unnamed cat this time who is 99 years old by default.
So the next thing that I want to talk about here is another version of creating a table for cats for
this time.
And I combined not null alongside default.
Otherwise it's the same.
So we have name of our car default is unnamed age int default is 99 but I added in not null.
And as I wrote here, you might be thinking isn't this redundant?
Because if we have a default value, it's already saying it won't be null.
If we go back and we look at cats, regular cats, if we do an insert into cats just like this and if
you can remember all the way back to cats, we have no default provided and I do select star from cats.
We get no, no, there's no default values.
And then when we have our Cat three, when I insert something like this right here, insert into cats
three, it's totally empty.
We have those defaults.
So when I select star
oops from cat three, we get no name provided 99 So why would we want to add not null?
And the answer is that we could still manually set something to be null.
So if I did that here, let's do insert into cats three and I'll do name and age, name and age values.
And I will say there's no name or let's say name is what's another state in Montana.
It's not too bad for a cat's name.
Great state, by the way.
And we'll leave age as null, explicitly null, just like that.
Let's do select star from Cat three and you can see that there was nothing stopping us from setting
age to be null.
All that we have is a default value.
If you don't specify something, it will be 99, but you're allowed to say no.
I'm allowed to say this is explicitly an empty value.
So that's where this comes in.
Name var not null and a default.
And I won't make you sit through me typing that whole thing so I'll just copy it.
But to walk through it again.
Same thing here.
Same with the default we just added.
Not null and not no.
So let's go ahead, paste this in.
This is cats for.
This time if I do describe cats for we now have null is no on both and we have a default on both.
So we're almost there.
I know it's been a little bit of a long video.
Let's do a insert into cats for and we will specify name and then age.
And this time if I do values.
Oops.
And I say, name is Watson.
Let's do Cali.
Fine.
Sort of a state name and age is null.
If I do this.
This time we get an error.
So not just a warning, but an error saying it cannot be null.
And that's what not null specifies.
So important to note that you'll use not know pretty often.
It's a way of basically requiring that something has a value, but also saying that it's not allowed
to be null itself, which I also included as a slide here.
In Cat three, we could manually insert something as No.