WEBVTT

00:07.580 --> 00:11.300
I will call you back again to another video lecture in MySQL.

00:11.570 --> 00:16.520
And in this video lecture we go ahead and look at the primary keys.

00:17.150 --> 00:17.390
Alright.

00:17.390 --> 00:23.030
So the primary keys are very important because it helps us to be able to manage our database very well

00:23.030 --> 00:24.500
without mixing up things.

00:24.530 --> 00:30.110
It helps each data to be able to have a unique value.

00:30.140 --> 00:34.760
So if you have a IDs especially, this is where you.

00:34.760 --> 00:41.090
I will advise you to always use the primary keys on your IDs so that you can actually differentiate

00:41.090 --> 00:42.320
your data.

00:42.320 --> 00:50.330
So you can have different IDs, have maybe some first name, the same last name with different IDs.

00:50.360 --> 00:54.050
Let's go ahead and do something very interesting here.

00:54.080 --> 00:58.520
I'm going to create a new table and I'm going to use that to explain everything.

00:58.550 --> 01:03.660
Then you go ahead and apply that to the employees table Stable and the student table.

01:03.690 --> 01:08.670
Now I'll go ahead and create a table and I'm going to call that table.

01:08.700 --> 01:10.860
I'm going to say table.

01:10.860 --> 01:14.700
And the name of the table is going to be teachers table.

01:14.910 --> 01:15.240
Alright.

01:15.240 --> 01:20.580
So I'm going to list all the teachers in the school because we have our school database.

01:20.610 --> 01:27.510
Now all teachers in school, I want to put them together and in order what I'm going to create this

01:27.510 --> 01:31.530
teachers I might have teachers that have the same first name or the same last name.

01:31.560 --> 01:35.730
I can still have teachers that have the same first name and last name.

01:35.760 --> 01:37.410
Yes, it's very possible.

01:37.410 --> 01:45.570
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

01:45.570 --> 01:46.830
an integer.

01:47.010 --> 01:47.340
All right.

01:47.370 --> 01:52.920
Now it's going to be an integer because they're going to take like 1234, five, 678 and cool.

01:52.920 --> 02:03.240
And now the next one is going to be the first name I would say the first underscore name.

02:03.270 --> 02:03.600
All right.

02:03.600 --> 02:05.340
So I'm going to stop in the first name.

02:05.340 --> 02:07.560
And this is going to be cha.

02:08.340 --> 02:10.950
And it's going to be three characters.

02:11.760 --> 02:18.240
So you can go ahead and add a last name salaries and so on.

02:18.270 --> 02:19.680
Or for the quality purpose.

02:19.680 --> 02:25.080
And for the purpose of this, I just want to stop at this so it can be easier for us to actually understand

02:25.110 --> 02:25.890
what's going on.

02:25.890 --> 02:28.200
So I can add a permanent key to the first name.

02:28.200 --> 02:32.340
Maybe I know what every teacher to have the same first name, which is not possible.

02:32.370 --> 02:38.130
You understand that is not possible, that teachers will not have the same first name, because two

02:38.130 --> 02:40.710
S's might answer the same thing or the same name.

02:40.710 --> 02:46.380
So let's go ahead and put the primary key to be under the teacher's ID, meaning that I'm going to have

02:46.380 --> 02:47.730
these different numbers.

02:47.730 --> 02:53.520
So I'm going ahead and saying the primary go ahead and make this to be uppercase.

02:55.080 --> 02:57.600
So the primary key.

02:57.810 --> 03:03.630
So if I add this primary key it means that even a null value cannot be added.

03:03.630 --> 03:04.080
De.

03:04.110 --> 03:05.040
Yes.

03:05.080 --> 03:07.360
It does not accept a no value.

03:07.390 --> 03:08.050
Okay.

03:08.080 --> 03:11.410
Now let's go ahead and uh select.

03:12.520 --> 03:16.660
Oh let's go ahead and select all from.

03:19.660 --> 03:20.440
Teacher.

03:20.920 --> 03:21.670
Okay.

03:21.700 --> 03:23.170
Select all from teachers.

03:23.170 --> 03:25.360
And go ahead and press semicolon at the end.

03:25.390 --> 03:27.610
Now let's go ahead and execute this query.

03:27.610 --> 03:29.350
And we have the table.

03:29.470 --> 03:31.600
Teacher ID first name.

03:31.870 --> 03:35.380
And uh at the moment nothing is in there.

03:35.530 --> 03:40.840
So now I have this teacher ID and I set the teacher ID to be null.

03:40.870 --> 03:45.400
And let's say you have this assistant table because most of us will be like, okay, should I go ahead

03:45.400 --> 03:48.310
and create a new database of employees or students?

03:48.340 --> 03:48.790
No.

03:48.790 --> 03:53.710
If you have a table already, just go ahead and do what I'm going to do right here.

03:53.740 --> 03:55.840
I'll go ahead and remove everything here.

03:57.370 --> 03:58.870
So go ahead and delete this.

03:58.870 --> 04:04.900
And I'll go ahead and say alter your table and you select the table name maybe the employees or the

04:04.900 --> 04:05.530
student table.

04:05.530 --> 04:11.870
Which one of them Go ahead and say maybe it'll go ahead on the table.

04:12.080 --> 04:15.830
And if I refresh this, I'll have to start over in here.

04:15.860 --> 04:25.190
So if I go ahead and set adjustable I'll go ahead and, uh, add the constraint and then the constraint

04:25.190 --> 04:27.950
which is the primary key.

04:27.980 --> 04:34.700
Then you specify where you want the constraint to be applied and say it's going to be under teacher

04:34.820 --> 04:36.020
underscore ID.

04:37.580 --> 04:42.110
And then plus one at the end of that if we go ahead and run this we're going to get an error because

04:42.110 --> 04:45.740
you have already um set the primary key for that.

04:45.740 --> 04:46.640
So go ahead and run this.

04:46.640 --> 04:47.630
Let's check it out.

04:47.690 --> 04:53.270
And down here you can see I have multiple primary key defined because I have already defined the primary

04:53.300 --> 04:53.750
key.

04:53.780 --> 04:55.310
The first time I created this table.

04:55.310 --> 04:59.540
So I'm just showing you this in case if you have a table you want to add a primary key to, just go

04:59.540 --> 05:01.970
ahead and add constraint primary key.

05:02.000 --> 05:05.060
Then select the column you want to add this primary key to.

05:05.090 --> 05:05.960
Do you understand.

05:05.990 --> 05:10.370
Now let's go ahead and attempt to add some things under here.

05:10.400 --> 05:15.410
So if I go ahead and run this code, I can still see my teachers ID first name right in here.

05:15.440 --> 05:18.740
Now I'll go ahead and set some things inside here.

05:18.770 --> 05:23.000
Let me go ahead and say go ahead and insert into.

05:25.400 --> 05:30.560
Insert into teachers and our ahead and insert values.

05:32.570 --> 05:34.370
Then the value.

05:34.400 --> 05:34.880
Go ahead.

05:35.060 --> 05:36.290
Semicolon at the end.

05:36.320 --> 05:41.570
Then I'm going to add the workforce parameter 1 or 1 for the first ID.

05:41.780 --> 05:45.530
And I'll let the name be equal to.

05:48.320 --> 05:49.310
George.

05:49.340 --> 05:49.910
All right.

05:49.940 --> 05:52.070
Now let's go ahead and execute this query.

05:52.070 --> 05:59.090
And I have 101 George and I consider the first name is George and the ID is 101.

05:59.510 --> 05:59.960
All right.

05:59.990 --> 06:04.340
Now let's go ahead and change this to like our 102.

06:04.340 --> 06:08.240
And then we change the name to like Steve.

06:08.240 --> 06:14.460
And go ahead and execute cutest query and have One02 and Steve and you can see that.

06:14.610 --> 06:16.740
Now I want to have the same name.

06:16.770 --> 06:18.840
All right, let's go ahead and give the same name.

06:18.930 --> 06:21.480
I say go ahead and say 103.

06:21.510 --> 06:28.410
And uh, the judge again then is a 3D query.

06:28.440 --> 06:32.910
You can see I have George the first time, first name and second name George.

06:33.060 --> 06:35.040
What I have different IDs.

06:35.040 --> 06:37.380
And that is really very interesting.

06:37.410 --> 06:43.650
What if I should go ahead and attempt to add A10?

06:43.980 --> 06:49.080
Let me say 101, but different name.

06:49.530 --> 06:50.340
Steve.

06:50.430 --> 06:51.720
Steve is already a.

06:51.720 --> 06:53.940
And let's go ahead and execute this.

06:53.970 --> 06:56.880
Or let's go ahead and give it a different mentality.

06:56.910 --> 06:58.320
I'm going to say memoir.

06:58.320 --> 07:02.070
And I have to add 0101.

07:02.100 --> 07:03.480
Go ahead and execute this.

07:03.480 --> 07:07.830
And now I'll drag this up so you can be able to see an error message.

07:07.860 --> 07:11.580
It says Duplicates key.

07:11.610 --> 07:15.570
1014 teacher for teachers table.

07:15.600 --> 07:17.220
That is the primary key.

07:17.250 --> 07:20.910
So 101 for key teachers does primary.

07:20.940 --> 07:26.100
That is you are not authorized because it's already duplicate.

07:26.220 --> 07:26.910
Okay.

07:26.910 --> 07:31.110
So we have A101 and we have another 101.

07:31.110 --> 07:32.970
So it is already duplicated.

07:33.000 --> 07:38.220
Now what if I say okay now I have known that we have duplicates.

07:38.250 --> 07:40.620
And what if I change that to be null.

07:41.400 --> 07:44.580
While I change this to be null.

07:44.610 --> 07:50.220
And I'm going to call this maybe a Steve.

07:50.430 --> 08:00.150
And if I run this, then now again, because you have an error, it says teacher ID cannot be null.

08:01.170 --> 08:03.870
So teacher ID cannot be no.

08:03.900 --> 08:10.980
So you can either go ahead and change that to 104 and go ahead and click on that.

08:10.980 --> 08:13.570
And you can see we have done here.

08:14.500 --> 08:16.720
101102.

08:16.750 --> 08:17.950
Steve 103.

08:17.980 --> 08:19.210
George 104.

08:19.240 --> 08:19.870
Steve.

08:19.900 --> 08:23.620
So they can have the same first name, the same last name, and so on.

08:23.620 --> 08:26.440
But the ID need to be different.

08:26.470 --> 08:26.860
Okay.

08:26.890 --> 08:28.330
That is very important.

08:28.360 --> 08:31.930
Now let's go ahead and find the child with an ID 103.

08:31.930 --> 08:34.390
So go ahead and delete this.

08:34.390 --> 08:35.620
And I'm go ahead and say.

08:37.960 --> 08:39.490
Teach us ID.

08:39.910 --> 08:47.050
So select ID from teachers.

08:47.080 --> 08:50.140
Then we're going to set this guy.

08:50.200 --> 08:53.500
Let's say we.

08:56.140 --> 08:57.220
Teach us okay.

08:57.250 --> 08:58.390
We're the first name.

09:02.620 --> 09:10.210
So select teachers ID from teachers where the first name is going to be equal to George.

09:13.910 --> 09:16.190
So listen, we can at the end of this.

09:16.220 --> 09:18.440
Now let's go ahead and run this and check it out.

09:20.780 --> 09:22.580
So go ahead and open this.

09:22.610 --> 09:23.930
So this is teachers ID.

09:23.990 --> 09:29.330
You see teachers ID first one is 101 and 103.

09:29.510 --> 09:36.050
So if we change that to teachers ID where it is Steve let's go ahead and run this again and check it

09:36.050 --> 09:36.680
out.

09:37.490 --> 09:38.960
Let's go ahead and open up this.

09:38.960 --> 09:42.290
And that is 102 and 104.

09:42.560 --> 09:43.040
All right.

09:43.040 --> 09:43.610
Cool.

09:43.850 --> 09:57.500
So what it is you can see select a from first name via teachers ID or we have teacher ID is equal to

09:58.970 --> 10:01.550
101.

10:01.940 --> 10:02.300
All right.

10:02.300 --> 10:04.190
So go ahead and run this and check it out.

10:06.140 --> 10:06.950
Open up this.

10:06.950 --> 10:08.510
And that is George.

10:08.540 --> 10:13.580
So if we check now if I were to select individually two.

10:14.660 --> 10:16.490
And the guy hadn't executed.

10:16.490 --> 10:19.370
This guy hadn't opened it up.

10:19.370 --> 10:21.860
And you see first name Steve.

10:23.060 --> 10:28.160
So in that way, you can actually use the IDs separately to check all this.

10:28.160 --> 10:32.600
And anyone I check to open up this, that is one zero is also George.

10:32.600 --> 10:39.020
So in that way you can be able to use a primary key and work with anything you ever want to work with.

10:39.020 --> 10:48.860
And it will help you to make sure that no two teachers have the same ID, and no teacher has a no value

10:48.860 --> 10:55.340
in their teacher ID, so the probability is very important in your database design and ensure you master

10:55.340 --> 10:56.690
that play around with it.

10:56.720 --> 11:00.290
And if you have any questions, go ahead and use the question and answer section.

11:00.290 --> 11:02.930
And I'm going to get back to you as soon as possible.

11:02.960 --> 11:04.070
Thank you so much.

11:04.070 --> 11:07.340
And I'm going to see you in the next video lecture.
