I welcome you back again to another video lecture in MySQL.
And in this video lecture, let's go ahead and look at the default constraint.
All right.
So we have this table called employees.
And we're going to use that to explain what the default constraint is all about.
Now the default constraint is a constraint that helps us to insert a particular condition that helps
to maintain a status quo in your database.
So let me say that you have this, uh, employee database, and we don't want to consider your cGPA
anymore.
So anybody can enroll and we don't want to take record of GPA.
And we don't want this to also be null, but we want that to just like 0.0.
And we don't want to be entering that 0.0 by ourself.
Let me go ahead and say I have this table or this that I want to include, and look at this data right
in here.
So I have insight into an employee's values.
Uh, employee ID for George, 0.0, employee five, id id five, and first name is Steve 0.0.
And uh, the next one is six -0.0.
So if I should go ahead and run this then you can see that over here.
I have, uh, this right.
And get now this is what we want to get.
We want to have 0.0, 0.00.0.
We don't consider the cGPA anymore, but we don't want to go ahead and start typing this by ourselves.
We don't want to start typing, uh, premises employee ID for George.
Then we go ahead and add 0.0.
We don't want we don't just want it anymore.
We just want to enter only your employee ID there, your first name, and then it'll go ahead and automatically
include 0.0.
So how can we be able to do that?
First of all let me go ahead and delete this.
Then I'll go ahead and remove that from the table.
To delete that I'll go ahead and say this.
So delete uh, from employees.
So that is from employees.
Then where we have the employee ID what the employee ID is greater than or equal to four and put a semicolon
at the end.
Now let's go ahead and run this and check it out.
Now we have remove that and want to repeat what we just did right now because I used that to explain
how this works.
So if I go ahead and uh say I want to maybe create a new table.
So if I want to create a new table on this, all I need to do is, uh, I'll go over here and then go
ahead and delete this.
I'll go ahead and put this.
Now I'll go away and I'll create a new employee table called employee ID and their first name equal
to Charles, then CGP equal to decimal.
And that five precision one.
Then I will set the default to be zero.
Or I'll go ahead and say 0.01 because I have this table right here.
I don't need to create that again.
So if I should go ahead to create this table, you can see that table employee already exists.
You can see that that is the error message.
Go ahead and drag this up.
So you can see we have this error message.
Employee table already exists.
Now the reason why I didn't want to create this table from the beginning is because throughout this
course I've been teaching you how to actually do that on an existing table.
So now I want to actually implement it by yourself by setting this up on existing table.
So how can we do that on existing table by setting this.
So let's go ahead and try it over again.
Now I will also has already existed what we want to actually set the CGP 8 to 0.0 for any name that
has been entered, or any employee ID with the name, what I need to do is I'll go ahead and see Alter
Table, and the name of the table you want to choose is a employees.
And then I need to.
alter the CDP and then set the defaults to be equal to 0.0.
The semicolon at the end.
Now, instead of.
There is no need for this 0.0, because you have already set a CGP to be decimal and five precision
one.
What we've created is that we initially can go ahead and set.
The default is equal to 0.0.
And that is all.
Now let's go ahead and execute this.
And down here you can see that we have this returned.
Okay.
So we have this returned.
And tick green means we have set it.
Now if we want to go ahead and, uh, check this out, what I need to do is let's go ahead and insert
this paragraph and say insert into insert into employees.
Then I will go ahead and, uh, see values.
I want this to be in uppercase so valleys.
And then I'll go ahead and give the values.
So I have a comma and the next one.
Put a camera then the next one.
Just watch only three of these.
Oh I saw the last one constant semicolon at the end.
So the first id I need is four.
Then the first one is George.
And uh let's go ahead and put the fifth.
The next one and uh is Steve.
So remember to put a comma right in here and that this is our Steve.
Then the next one is y say this is uh.
And Manuel.
Now we have these IDs.
Um, I will have this employees.
Now, if we go ahead and, uh, set these, we're are going to get an error because we didn't actually
specify any of the kernel for CGP.
Now let's try to run this code and see if that works out fine.
Now.
Now here you can see we have an error.
Say column card doesn't match value card at row one.
Now what happens is because we didn't consider the CGP.
So what you need to do is go back right inside, insert into employees and give the condition that this
is going to be for employee.
Employee underscore ID and just the first name.
So first underscore name.
So that is where we just want to insert right.
And let's go ahead and uh enter this.
And uh it says this is employee ID and not the employee ID.
All right.
So that has been corrected.
And go ahead and insert this.
Oh and now so you can see we have this.
Then I want to drive this a little bit down here.
So you can see that once we have Michael is still 4.7.
Crystal is still at 4.8.
Mike is 4.1.
Then George, Steve and the memo which we just entered are 4.0, 0.0, 0.0, 0.0 respectively.
And you see, we didn't enter that by ourselves.
This is because we have already set the default to be zero.
Okay.
So we have set a default to zero.
So anytime you include any of these things let's say enter any data into this.
It will head and set HPA to 0.0.
And that is really very interesting.
So I hope that is cool.
Now I'll go ahead and take this a little bit.
And let's go ahead and add another column right here.
And let that column be the enrollment date.
So that any time we employ a new person into the company without we going to insert the date, which
automatically gives us the current date and time, we depress it in rules.
Now let's go ahead and delete this.
And for us to add a colon.
So go ahead and say alter.
All right.
Let's go ahead and make this uppercase.
So this is alter table.
And the table went over to the employees table.
And then we go ahead and uh add the let's say this name is going to be employment underscore date.
So employment date and the the data type is going to be date time.
I don't want that to be date or time alone I want that to be date time.
So it's going to give us a date and a time as well.
Now I want to set this.
All right.
So let's go ahead and say set this defaults and uh let it default be null.
So in this case we have now set this to have given it a default null means that it's going to give all
the time or the date which the person is being enrolled.
Now let's go ahead and uh, Enter these and let's check it out.
So you can see that we didn't type anything.
It gave us the employment date for everybody for everyone that's been enrolled.
So in the next one I want to check this out.
It gives you the current time you understand.
So in next minutes many more years to come.
Next year it's always update itself.
So anytime you enroll someone into the company, automatically the date time will be entered in the
database.
And you don't need to go ahead and start repeating this.
So by default, I've already set this, and I've already set the GPS to record 0.0 because we're not
considering cGPA on your employment.
You know, uh, again, so what's your input?
You go ahead and get started.
Nobody consider your cGPA.
And we've also taken the time that you enrolled or into the company, or the time you're employed would
get your employment date and your GPA is negligible.
So that is cool.
I hope that is cool.
Oh I so you can go ahead and drop any of these by writing drop.
I will drop that.
Go ahead and put it down.
If you want to drop these things, all I need to do is I'll go ahead and drop.
Let me go ahead and say, uh, drop.
When I say drop.
Uh, employee employment underscore date.
So if I drop colon so going to say drop colon and permit underscore date.
And I will go ahead and drop this.
So that is it for now.
We're done with this.
And I hope you are familiar.
And you understood what employment the default actually does.
So the default constraint helps us to be able to set our defaults.
And we actually work whatever we set without repeating that any time.
Again.
So that is it for now.
So thank you so much.
And I'm going to see you in the next video lecture.