I will call you back again to another video lecture in MySQL.
And in this video lecture we go ahead and look at the primary keys.
Alright.
So the primary keys are very important because it helps us to be able to manage our database very well
without mixing up things.
It helps each data to be able to have a unique value.
So if you have a IDs especially, this is where you.
I will advise you to always use the primary keys on your IDs so that you can actually differentiate
your data.
So you can have different IDs, have maybe some first name, the same last name with different IDs.
Let's go ahead and do something very interesting here.
I'm going to create a new table and I'm going to use that to explain everything.
Then you go ahead and apply that to the employees table Stable and the student table.
Now I'll go ahead and create a table and I'm going to call that table.
I'm going to say table.
And the name of the table is going to be teachers table.
Alright.
So I'm going to list all the teachers in the school because we have our school database.
Now all teachers in school, I want to put them together and in order what I'm going to create this
teachers I might have teachers that have the same first name or the same last name.
I can still have teachers that have the same first name and last name.
Yes, it's very possible.
So I can say the first one is going to be teachers ID or teacher ID that is okay and it's going to be
an integer.
All right.
Now it's going to be an integer because they're going to take like 1234, five, 678 and cool.
And now the next one is going to be the first name I would say the first underscore name.
All right.
So I'm going to stop in the first name.
And this is going to be cha.
And it's going to be three characters.
So you can go ahead and add a last name salaries and so on.
Or for the quality purpose.
And for the purpose of this, I just want to stop at this so it can be easier for us to actually understand
what's going on.
So I can add a permanent key to the first name.
Maybe I know what every teacher to have the same first name, which is not possible.
You understand that is not possible, that teachers will not have the same first name, because two
S's might answer the same thing or the same name.
So let's go ahead and put the primary key to be under the teacher's ID, meaning that I'm going to have
these different numbers.
So I'm going ahead and saying the primary go ahead and make this to be uppercase.
So the primary key.
So if I add this primary key it means that even a null value cannot be added.
De.
Yes.
It does not accept a no value.
Okay.
Now let's go ahead and uh select.
Oh let's go ahead and select all from.
Teacher.
Okay.
Select all from teachers.
And go ahead and press semicolon at the end.
Now let's go ahead and execute this query.
And we have the table.
Teacher ID first name.
And uh at the moment nothing is in there.
So now I have this teacher ID and I set the teacher ID to be null.
And let's say you have this assistant table because most of us will be like, okay, should I go ahead
and create a new database of employees or students?
No.
If you have a table already, just go ahead and do what I'm going to do right here.
I'll go ahead and remove everything here.
So go ahead and delete this.
And I'll go ahead and say alter your table and you select the table name maybe the employees or the
student table.
Which one of them Go ahead and say maybe it'll go ahead on the table.
And if I refresh this, I'll have to start over in here.
So if I go ahead and set adjustable I'll go ahead and, uh, add the constraint and then the constraint
which is the primary key.
Then you specify where you want the constraint to be applied and say it's going to be under teacher
underscore ID.
And then plus one at the end of that if we go ahead and run this we're going to get an error because
you have already um set the primary key for that.
So go ahead and run this.
Let's check it out.
And down here you can see I have multiple primary key defined because I have already defined the primary
key.
The first time I created this table.
So I'm just showing you this in case if you have a table you want to add a primary key to, just go
ahead and add constraint primary key.
Then select the column you want to add this primary key to.
Do you understand.
Now let's go ahead and attempt to add some things under here.
So if I go ahead and run this code, I can still see my teachers ID first name right in here.
Now I'll go ahead and set some things inside here.
Let me go ahead and say go ahead and insert into.
Insert into teachers and our ahead and insert values.
Then the value.
Go ahead.
Semicolon at the end.
Then I'm going to add the workforce parameter 1 or 1 for the first ID.
And I'll let the name be equal to.
George.
All right.
Now let's go ahead and execute this query.
And I have 101 George and I consider the first name is George and the ID is 101.
All right.
Now let's go ahead and change this to like our 102.
And then we change the name to like Steve.
And go ahead and execute cutest query and have One02 and Steve and you can see that.
Now I want to have the same name.
All right, let's go ahead and give the same name.
I say go ahead and say 103.
And uh, the judge again then is a 3D query.
You can see I have George the first time, first name and second name George.
What I have different IDs.
And that is really very interesting.
What if I should go ahead and attempt to add A10?
Let me say 101, but different name.
Steve.
Steve is already a.
And let's go ahead and execute this.
Or let's go ahead and give it a different mentality.
I'm going to say memoir.
And I have to add 0101.
Go ahead and execute this.
And now I'll drag this up so you can be able to see an error message.
It says Duplicates key.
1014 teacher for teachers table.
That is the primary key.
So 101 for key teachers does primary.
That is you are not authorized because it's already duplicate.
Okay.
So we have A101 and we have another 101.
So it is already duplicated.
Now what if I say okay now I have known that we have duplicates.
And what if I change that to be null.
While I change this to be null.
And I'm going to call this maybe a Steve.
And if I run this, then now again, because you have an error, it says teacher ID cannot be null.
So teacher ID cannot be no.
So you can either go ahead and change that to 104 and go ahead and click on that.
And you can see we have done here.
101102.
Steve 103.
George 104.
Steve.
So they can have the same first name, the same last name, and so on.
But the ID need to be different.
Okay.
That is very important.
Now let's go ahead and find the child with an ID 103.
So go ahead and delete this.
And I'm go ahead and say.
Teach us ID.
So select ID from teachers.
Then we're going to set this guy.
Let's say we.
Teach us okay.
We're the first name.
So select teachers ID from teachers where the first name is going to be equal to George.
So listen, we can at the end of this.
Now let's go ahead and run this and check it out.
So go ahead and open this.
So this is teachers ID.
You see teachers ID first one is 101 and 103.
So if we change that to teachers ID where it is Steve let's go ahead and run this again and check it
out.
Let's go ahead and open up this.
And that is 102 and 104.
All right.
Cool.
So what it is you can see select a from first name via teachers ID or we have teacher ID is equal to
101.
All right.
So go ahead and run this and check it out.
Open up this.
And that is George.
So if we check now if I were to select individually two.
And the guy hadn't executed.
This guy hadn't opened it up.
And you see first name Steve.
So in that way, you can actually use the IDs separately to check all this.
And anyone I check to open up this, that is one zero is also George.
So in that way you can be able to use a primary key and work with anything you ever want to work with.
And it will help you to make sure that no two teachers have the same ID, and no teacher has a no value
in their teacher ID, so the probability is very important in your database design and ensure you master
that play around with it.
And if you have any questions, go ahead and 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.