WEBVTT

00:07.820 --> 00:08.540
All right.

00:08.540 --> 00:13.070
So let's go ahead and look at primary keys in PostgreSQL.

00:13.310 --> 00:18.920
So in PostgreSQL we have this uh data set we inserted.

00:18.920 --> 00:25.940
And if we go over and check out the students let's go ahead and say students and hit enter.

00:27.200 --> 00:29.510
We have these students PK.

00:29.510 --> 00:31.400
And that is the primary key.

00:31.430 --> 00:36.350
So we can see we have this primary key and that is the students ID.

00:36.620 --> 00:43.970
And over here you can see the stress ID pk the bigint is a primary key.

00:43.970 --> 00:48.800
And uh we want to learn more about this primary key.

00:49.010 --> 00:59.120
Now this primary key helps us to be able to restrict to datas with same information.

00:59.120 --> 01:04.980
So nobody in this place, we have the same primary key of like one and N1.

01:05.220 --> 01:07.350
Let me go ahead and show this.

01:07.380 --> 01:12.540
This is what we have in the previous lecture and this is what we inserted.

01:12.540 --> 01:15.360
And we have this guy right in here.

01:15.360 --> 01:17.520
And this is a very fast one.

01:17.520 --> 01:23.730
And it has the name Mark on and the ID is one.

01:23.760 --> 01:24.990
Let's go ahead and check it out.

01:24.990 --> 01:27.570
So the ID of this guy is one.

01:27.570 --> 01:35.580
Now you can see it has a primary key of one uh ID one that is stress ID and the stress ID two.

01:35.610 --> 01:39.780
Now we can have the same person having the same primary key.

01:39.810 --> 01:41.550
Like this guy here.

01:41.550 --> 01:43.500
It is not possible at all.

01:43.590 --> 01:47.070
If we go ahead and try that, we're going to run into an error.

01:47.400 --> 01:51.690
Let's go ahead and open up the Visual Studio.

01:51.720 --> 01:56.970
And I'm going to say I want to insert into this.

01:57.000 --> 02:02.730
Let me go ahead and say insert into students and I'm going to add a student.

02:05.190 --> 02:08.130
That is a stress underscore ID.

02:08.740 --> 02:17.020
and now if I go over here and add this ID one here and try to copy this, let me go ahead and copy this

02:17.020 --> 02:18.910
and insert that.

02:18.940 --> 02:21.640
I want to insert this.

02:21.670 --> 02:23.530
And now I copied this.

02:23.530 --> 02:25.750
And I'm going to minimize this right in here.

02:25.750 --> 02:33.460
If I go over to this place and I try to insert that into the students table, I'm going to get an error.

02:33.460 --> 02:37.360
So go ahead and clear this and I'm going to press what I just copied right in here.

02:37.360 --> 02:41.260
And then go ahead and reduce this value so I can be able to see it.

02:41.860 --> 02:42.220
All right.

02:42.220 --> 02:44.080
So insert into students.

02:44.080 --> 02:52.690
And now with our students ID then first name last name email gender error date country and values is

02:52.690 --> 02:53.680
equal to one.

02:53.680 --> 02:55.390
Now let's go ahead and execute this query.

02:55.390 --> 02:56.260
Let's check it out.

02:56.290 --> 02:58.240
Now you can see you have an error.

02:58.240 --> 03:03.910
And what this error said is duplicate key value varies unique constraint.

03:03.940 --> 03:06.880
Students peak t.

03:07.060 --> 03:09.940
So that is the student ID.

03:09.970 --> 03:15.110
It's also said here that our a stress ID equal to one already exists.

03:15.140 --> 03:17.150
That's duplicate key value.

03:17.180 --> 03:21.530
Whereas the unique constituents which is the primary key.

03:21.710 --> 03:27.920
Now you can see that we have a very big problem because I'm not a not a problem at all.

03:27.920 --> 03:34.910
So this helps us to actually make sure I would not have these two, uh data bit inserted.

03:34.910 --> 03:42.740
And in that case it's going to give us a very big issue, because in a case where two persons have the

03:42.740 --> 03:46.250
same ID, we are going to always run into problem.

03:46.250 --> 03:54.740
So if you want to actually do that, all you need to do is you have to go ahead and, uh, drop the

03:54.740 --> 03:57.050
primary key if you want to insert this.

03:57.050 --> 04:04.730
And if I want to drop the primary key or I need to do is to go ahead and drop this constraint, call

04:04.760 --> 04:07.310
the answer key.

04:07.610 --> 04:08.900
Go ahead I see it.

04:09.080 --> 04:12.620
So I'm going to start by deleting everything I have right in here.

04:12.620 --> 04:16.630
And then I'll go ahead and use a keyword called quarter.

04:16.630 --> 04:20.560
And then I'm going to alter the turbo and the turbo.

04:20.590 --> 04:25.390
I want to alter is this to stable students.

04:25.570 --> 04:30.520
And then I'll go ahead and drop using drop keyword.

04:30.520 --> 04:32.950
And what I'm going to drop is this constraint.

04:32.980 --> 04:33.400
All right.

04:33.430 --> 04:35.500
Because you can see unique constraint.

04:35.530 --> 04:38.980
So why I say constraints.

04:41.170 --> 04:45.070
And now what is the name of the constraint that is to name the key.

04:45.220 --> 04:56.590
So I'll go ahead and say the students uh underscore p key and put a semicolon at the end.

04:57.040 --> 05:01.360
Now if I have this I'll go ahead and this to this and check it out.

05:01.390 --> 05:03.850
Our table query returned successfully.

05:03.850 --> 05:10.540
And you can see that now if I go ahead and delete everything I have here and, uh, paste back what

05:10.540 --> 05:17.680
I just copied from Visual Studio and execute this query, we can see insights are zero.

05:17.710 --> 05:19.990
One means return successfully.

05:20.030 --> 05:28.640
And if I should go ahead and delete everything here and I'm going to say select all from students,

05:28.640 --> 05:31.910
I put a semicolon at the end and the score had I checked out.

05:31.910 --> 05:37.400
And now if you go down let's go ahead and move down.

05:37.430 --> 05:42.800
Down here you can see we have our this has moved over to 1001.

05:42.800 --> 05:46.430
And last we can see Mark con is equal to this.

05:46.550 --> 05:53.870
And if we go ahead and select that from here I can go ahead and say select from students.

05:53.870 --> 05:59.480
Where I'm going to say we are students.

05:59.510 --> 06:03.380
Underscore ID is equal to one.

06:03.380 --> 06:08.300
And if I should go ahead and run this, we can see we have Mark home.

06:08.540 --> 06:11.090
And it appears twice.

06:11.900 --> 06:17.390
So but this is not really very nice because you're not supposed to have this right.

06:17.390 --> 06:18.890
So I hope that is very cool.

06:18.890 --> 06:25.290
So this is the importance of the ID that is the primary key.

06:25.320 --> 06:25.980
This is important.

06:26.010 --> 06:29.910
The primary key in query or in PostgreSQL.

06:29.940 --> 06:36.810
It helps you to avoid having two data and conflicting data, because there is no how you can know who

06:36.810 --> 06:37.710
is Marco.

06:37.740 --> 06:38.610
Right in here.

06:38.610 --> 06:41.520
And who has this or who is this?

06:41.520 --> 06:47.490
Because everything is just the same and you don't want to run into that problem.

06:48.210 --> 06:54.090
So we have the first name, the last name, the same email address, same gender, uh, enrolled date

06:54.090 --> 06:55.680
and from the same country.

06:55.710 --> 06:58.020
And now you have a very big problem.

06:58.020 --> 07:05.310
And if you have different stress ID, you can actually use that to separate them and know who is who.

07:05.310 --> 07:06.930
And that would have been better.

07:07.200 --> 07:09.480
So let's go ahead and put this done.

07:09.480 --> 07:10.230
Practice with it.

07:10.230 --> 07:14.910
And in the next video lecture we'll go ahead and put this back again and have a primary key right in

07:14.910 --> 07:18.450
here because that is very, very, very important.

07:18.480 --> 07:19.020
Thank you.

07:19.020 --> 07:21.780
And I'm going to see you in the next video lecture.
