OK 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 OK 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 you didn't specify age we could always set it to be ninety nine.
Not sure why but for the sake of this exercise we're going to do it.
So let's give it a shot.
Remaking a new cats table cat 3.
OK here we go.
So create table cats 3 and I'll do a new line here.
And we will have a name for our car.
Let's do 20 even though my slides say 100.
It's OK.
Default in our default will be.
That's a no name provided.
How about that.
It's important that whatever you put here matches the data type so we couldn't say the default is the
number 100.
And then we'll have our Khama age and age isn't it.
Default should be 99 perfect clothes that.
Now if we do our describe cats 3 Here we go.
So we have default no provided default 99 for H.
Awesome.
OK so now let's give it a shot let's do insert into Cat 3 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 going to insert a new cat into Cat 3 which is right here only for fighting
age and name has a default value of no name provided.
Ok I did it.
Now let's look like Star from Cat 3.
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 certain to catch three values.
Now when we select.
We have another unnamed cat this time who is ninety nine 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 know all along side default.
Otherwise it's the same so we have name our car default this unnamed age it default that's 99.
But I didn't not no.
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 no.
If we go back and we look at cat's regular cat 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 you select star from
cats we get.
No no there's no default values.
And then when we have our cat 3 when I insert something like this right here incertain to Cat 3 it's
totally empty.
We have those defaults.
So when I select star ups from Cat 3 we get no name provided.
Ninety nine.
So why would we want to add.
Not no.
And the answer is that we could still manually set something to be no.
So if I did that here let's do insert into Cat 3 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 no explicitly no.
Just like that.
That to do so like star from got three and you can see that there is nothing stopping us from setting
age to be no all that we have it's 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.
OK.
So that's where this comes in name for which are not.
No.
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 did not know and not know.
So let's go ahead paste this in.
This is Katz for this time.
If I do describe cat 4 we now have no is no on both and we have a default on both.
So we're almost there.
I know spend a little bit of a long video.
Let's do a insert into cat 4 and we will specify name and then age.
And this time if I do values AUPs and I say name is what's in it.
Let's do Cali fine.
Sort of a state name and age is no.
If I do this this time we get an error.
So not just a warning but an error saying it cannot be No.
And that's what not.
No 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 know itself which I also included as a slide here in Cat 3.
We can manually insert something as.