I will call you back again to another video lecture in MySQL.
And in this video lecture we go ahead and look at the unique constraint in MySQL.
So we are creating a database.
You want to make one of your columns to be unique.
It means that, uh, two of each data are not going to be the same.
Each of the datas are going to be different.
So let us go ahead and create a new table.
I'm going to leave the student table we have right in here.
So that's one I'm creating this table.
I'm going to add the unique constraint as well as this uh student.
You can actually go ahead and specify one of these columns to be unique.
Well let me go ahead and start afresh so we can be able to see it.
So let's go ahead and uh, create a new table and let the name of the table be employees.
Right.
Semicolon end.
And uh, we go here and add in some data.
Okay.
So let me go ahead and say I need the employee ID.
So and the ID is going to be an integer.
And then the first name I just want to make a simple table.
Go ahead and say the first name.
And that is varchar because it's going to be character.
And I'll let the character be 30.
Okay.
I just need 30 of them.
And then the last thing I need is your salary.
All right.
So I just made a simple table.
We can see that this is really very simple right.
And we're going to give the data type for the salary as int.
And that is O.
Now I'm going to choose one of these one of these columns which I'm just creating here.
So I'm going to choose one of them to be to have a unique constraint.
Or I'll go ahead and sign assign a unique constraint to one of them.
And if I want to do that, I'm going to use a simple keyword called unique.
So I can now choose the employee ID or the employee first name or the salary.
Let's go ahead and say I'm assuming I'm going to use the first name.
What I'm going to do is go ahead and say unique.
So this keyword unique, we actually make the first name to be unique, meaning that no employee or
two employees will never get the same first name.
And you know it's not possible, right?
But I'm doing this for the purpose of this course, so you can assign this to the employee ID, meaning
that no employee will have the same ID, but for the purpose of clarity.
And as a beginner, I'm assigning this the first name so you can see how the unique keyword actually
works.
Now let's go ahead and create this table.
All right.
You can see that we have created this table.
And if I refresh this I have employees right in here.
And the columns employee ID the first name and the salaries are right in here.
So you can see that it works fine right now assuming that you have this students table.
And let's go ahead and open this table right in here again.
So I have this students table right in here.
And I did assign Anyone.
So what I need to do is just like here, I don't need to create the table.
I'm going to use this employee table to play around.
So you can actually assign that on your own student table.
What I need to do is that I will ahead and delete this.
So if I delete that I'm going to alter the table I just created.
So let's go ahead and say alter.
And go ahead and say table and choose the table you want to alter.
And that is the employees.
All right.
So I'm going to say employees.
And then I'll go ahead and add constraints.
All right.
Because you have a keyword called constraint.
And go ahead and add a unique.
And then where do you want to assign this unique.
Because that is the next thing that you need to specify.
Remember that everything can be in one straight line.
But I just want us to see this here.
Now let's go ahead and see the first underscore name.
All right.
So that is if you go right in here we have a column called First Name.
So that is we want to assign this now overhead.
Now run this ahead and run this and check it out.
Okay.
Down here you can see that we didn't have a red tick.
We had a warning.
Yes.
It's a running another red tick.
What does it mean.
Go over here and you can see duplicate index first name to define on the table MySQL employees, meaning
that I am specifying a unique constraint to a column that has already been specified.
You get that.
It means that I don't need to specify it again, because I've already done that.
So this can only be done if you have created your table and you didn't assign a unique constraint at
the very first time.
So you actually did this when you have created your table.
So you can go to your students table and use this alter table and then assign any of your columns a
unique constraint, and it means that two of them can never be the same.
Remember that everything can be in a straight line.
Okay, so you can see that what it can just go ahead and put them in a separate line and it's still
walk.
So I'm making everything to be clean.
That is why I move it to the next line.
So now you have seen that we have already assigned that and that is all.
Can't assign that again.
This guy had to select this guy and to select all from our table.
So select from employees and put a semicolon at the end.
And let's go ahead and run this.
And this is employee ID first name and salary.
Now let's go ahead and add some things inside here.
Let's go ahead and want to add some values.
All you need to do is to go ahead and insert.
So this is a static keyword into employees.
So and go ahead and add the values.
So the values want to add a skarhead and, uh, with these, with the comma.
And let's go ahead and add more glaucoma with a comma.
A comma okay.
So.
I mean, let me go ahead and make this to be the last because we just want to keep everything to be
simple.
So the first one the employee ID number one and uh, let employee name be let me say Michael.
Oh okay.
So go ahead and change this.
So this is, uh, Michael.
And then we kind of set a salary is a 2400.
And, uh, this guy said a second one and then the name is going to be equal to Kizito.
And they let Kizito salary be equal to 3200.
And, uh, the next one we have is, uh, let me say.
The name is going to be James.
So James and the late James.
Sorry we got to the $7,000.
Now, let's assume I didn't know that I have to already there.
I'll go ahead and make the photo ID, and I'll go ahead and put the same first name.
Let's say this is a mistake.
I didn't know it actually exists.
And let me say, the salary is a 1100.
And now, if I should try to insert this data into my table, I'm going to get an error.
The error is going to tell me that Cachito has been, uh.
I'm trying to assign it twice.
Okay.
And then I'm trying to enter Casita twice, which is not going to be possible because everything under
the first name has to be unique.
Now let's go ahead and run this query and check it out.
So down here you can see I have this.
This is not only a warning but an error.
It shows duplicate entry for key employee first name.
It means that we have already made this and that is the first name.
The common first name.
We have already given it a unique constraint.
All right.
That is why we cannot be able to assign the same first name under here.
Now let's go ahead and remove zero and say this is a guest and not Kizito.
So there's a difference between Cassette and Kizito.
Let's go ahead and check it out.
Go ahead and execute this query.
And I can see my query has been executed.
And if I should go ahead and select these let's go ahead I select this.
So select all from.
Employees.
So select all from employees I put a semicolon and go ahead and run this.
Let's check it out.
So you can see I have a first employee ID Michael Kizito James and Kizzy.
But I was able to enter Kizito two times.
So that is a way to assign a unique constraint to any of your columns, and no two data will ever be
the same.
Jonathan.
But this is for qualitative purpose.
You've known about unique constraint.
Now it is your job and assignment to actually add that to your employee ID and not your first name and
not your salary, because you can have employees that get the same name.
You can have employees that have the same salary, but two employees should not have the same ID so
that you can actually differentiate them.
All right.
That is cool.
Go ahead and put it down.
And if you have any questions on that please use the question and answer section.
And I'm going to get back to you as soon as possible.
Thank you so much.
And I'm going to see you in the next video lecture.