WEBVTT

00:07.610 --> 00:10.580
I welcome you back again to another video lecture in MySQL.

00:10.580 --> 00:16.940
And in this video lecture let's go ahead and learn about auto increment of I.

00:16.940 --> 00:20.270
So in the previous lecture we discussed about the primary keys.

00:20.300 --> 00:25.910
Now if you want to add the auto increment that auto increment should be associated with your primary

00:25.910 --> 00:26.180
key.

00:26.180 --> 00:32.780
So any column that you add the primary key to you go ahead and add auto increment to that.

00:32.780 --> 00:35.420
So that is why it's always good to add that to the ID.

00:35.420 --> 00:38.420
So our ID will automatically be incremented.

00:38.420 --> 00:42.200
So we don't just need to add the ID by ourselves.

00:42.200 --> 00:49.340
So whenever we want to insert a name or anything inside our database, the ID will automatically increment.

00:49.340 --> 00:51.020
And that is how it's supposed to be.

00:51.050 --> 00:58.280
We don't need to always add that maybe 123456, because your database might get up to a million and

00:58.280 --> 00:59.600
you don't even know where you stopped.

00:59.600 --> 01:03.920
So how can you be able to add the ID by yourself every time?

01:04.130 --> 01:08.120
So in order to overcome this issue by adding the ID ourself.

01:08.120 --> 01:13.490
We just need to impute people's data and we go ahead and get the ID automatically inserted.

01:13.490 --> 01:19.280
And then governments know you by your ID, and that ID will automatically be there and it will automatically

01:19.280 --> 01:20.480
be created for you.

01:20.510 --> 01:22.460
So how can we be able to do that?

01:22.640 --> 01:27.830
Oh, and before we do that, you can see I've deleted the teachers table I created here.

01:27.860 --> 01:33.470
You can go ahead and use maybe the employees table or the students table or anyone, but I deleted or

01:33.470 --> 01:39.260
dropped the teachers table so that I can actually use the teachers table to help us to understand what

01:39.260 --> 01:39.950
we are doing.

01:39.980 --> 01:43.580
We just need to insert only the ID and the first name.

01:43.580 --> 01:50.090
You can go ahead and add last name or salary, GP, anything and so on.

01:50.510 --> 01:53.630
So now in a table has been dropped.

01:53.660 --> 01:59.060
I need to create a new teachers table with a primary key and auto increment there.

01:59.060 --> 02:03.110
So in order to do that let's go ahead and create a new table.

02:03.110 --> 02:04.790
So go ahead and say table.

02:04.790 --> 02:08.810
And the table name is going to be teachers.

02:08.930 --> 02:12.610
And we're going to have less Mr. Macron at the end of this.

02:12.880 --> 02:20.140
So inside the teacher's table, we go ahead and, uh, add a touch ID, right?

02:20.170 --> 02:25.000
So touch ID now the add is going to be integer variable.

02:25.000 --> 02:29.020
And the data type is integer and not integer variable.

02:29.020 --> 02:34.030
So the teacher's id has a data type of int for integers.

02:34.030 --> 02:37.750
And then it's going to also be the primary key.

02:37.750 --> 02:45.100
And now we have added a primary key to the teacher's ID we go ahead and add the auto increment.

02:45.100 --> 02:48.250
So go ahead and say auto increment.

02:48.250 --> 02:51.370
And I'll put a comma at the end of that.

02:51.370 --> 02:56.260
Now the auto increment will help the ID to always increment on its own.

02:56.260 --> 02:59.710
We don't need to actually impute that by ourselves.

02:59.740 --> 03:05.350
Now we go ahead and get the critical for the first name.

03:05.350 --> 03:09.190
So I'm going to say first underscore name.

03:09.340 --> 03:12.670
And the data type is varchar.

03:12.970 --> 03:19.990
And the character length is going to be deity and that is all.

03:20.020 --> 03:23.980
Now let's go ahead and select the insert movie to see what we just created.

03:23.980 --> 03:32.680
So select all from teachers and that is all semicolon at the end.

03:32.680 --> 03:34.480
And go ahead under query.

03:34.750 --> 03:39.010
And you can see we have a teacher's ID I will have the first name right in there.

03:39.010 --> 03:41.050
So we have now to create this table.

03:41.050 --> 03:47.200
And what we need right now is to go ahead and insert some rules into this.

03:47.200 --> 03:52.690
And in creating these rules, we are not going to insert the teacher's ID by ourselves.

03:52.720 --> 03:55.750
Now let's go ahead and remove whatever we have right in there.

03:55.780 --> 03:59.620
Now let's go ahead and insert the rules.

03:59.620 --> 04:05.050
So go ahead and say insert into teachers.

04:05.050 --> 04:06.370
So into the table.

04:06.370 --> 04:10.930
Go ahead and uh insert the values.

04:12.520 --> 04:17.770
And the values I'm going to add is just going to be the first name.

04:18.040 --> 04:18.490
All right.

04:18.490 --> 04:23.880
So and uh I'll go ahead and add.

04:23.910 --> 04:28.650
Let me say, George, why did I add George?

04:28.650 --> 04:32.700
And then if I should put it in, this is going to give you an error.

04:32.730 --> 04:34.050
Let's go ahead and check it out.

04:34.080 --> 04:35.550
We're going to get an error.

04:36.030 --> 04:37.920
We can see down here George.

04:37.950 --> 04:42.720
Column count does not match of value counts at row one.

04:42.720 --> 04:48.000
So you need to specify that you're adding this just to the first name only.

04:48.000 --> 04:51.810
So go right in here and say first underscore name.

04:51.810 --> 04:53.400
And that is it.

04:53.400 --> 04:55.440
So go ahead and execute a query again.

04:55.470 --> 04:57.480
And you can see I have first name George.

04:57.480 --> 05:02.010
And look at it just ID it automatically imputes one.

05:02.010 --> 05:10.290
So if I should go ahead and add Steve and execute this query you can see it just ID incremented to two.

05:10.350 --> 05:18.390
Now if I should continue and add one and then check it out teachers ID three.

05:18.420 --> 05:20.220
And let's go ahead and change the last one.

05:20.220 --> 05:22.290
What we see global.

05:22.290 --> 05:23.430
And um.

05:25.800 --> 05:32.970
Go ahead and Helen is a query to just ID is for so you can know to see how this works.

05:32.970 --> 05:37.650
And the one interesting part of this is that the auto increment can start any way.

05:37.680 --> 05:41.430
Very very interesting path of this auto increment.

05:41.430 --> 05:43.890
So you can actually start from 100.

05:43.920 --> 05:45.120
You can start from ten.

05:45.150 --> 05:46.350
You can start from 20.

05:46.380 --> 05:49.050
You can start from 25 and 18.

05:49.080 --> 05:51.000
Any number of your choice to 1 million.

05:51.000 --> 05:53.370
You can start from 1 million or 1000.

05:53.490 --> 05:58.770
Now let's go ahead and experiment with this and see how that works.

05:58.770 --> 06:03.480
So if I should do that I'll first of all alter the table, go ahead and alter the table and want to

06:03.480 --> 06:06.570
say alter the table.

06:06.570 --> 06:11.910
And the name of the table is a the teachers table.

06:11.940 --> 06:19.230
And if I go ahead I will set auto increment to be equal to any number of I choice.

06:19.260 --> 06:23.700
Let's go ahead and say one dot to be from 100 and put a semicolon at the end.

06:23.730 --> 06:26.250
Now let's go ahead and execute a query.

06:26.250 --> 06:35.550
And down here you can see we set that to 100 Now I'll go ahead and jump this table so I can start afresh.

06:35.580 --> 06:36.060
All right.

06:36.060 --> 06:41.130
So first of all go ahead and delete from.

06:42.840 --> 06:44.490
So delete from teachers.

06:44.490 --> 06:48.600
And if I should delete from teachers I'm going to delete every row.

06:48.630 --> 06:50.310
Go ahead and execute the query.

06:50.310 --> 06:53.760
And now teachers table is now empty again.

06:53.910 --> 07:01.620
So I'll go ahead and insert uh some of the stuff we did previously.

07:01.620 --> 07:07.560
So go ahead and uh, insert uh into teachers.

07:08.340 --> 07:10.260
So go ahead and insert into teachers.

07:10.290 --> 07:15.510
We have already known we're going to put in the first underscore name, because if we don't do that

07:15.510 --> 07:16.920
we're going to get an error.

07:16.920 --> 07:18.600
So the values.

07:21.180 --> 07:27.900
So the values and the semicolon at the end of these.

07:27.900 --> 07:31.650
So let's go ahead and start again with George.

07:32.490 --> 07:41.060
So rest it again with uh so go ahead and start with George and let's go ahead and check it out.

07:41.150 --> 07:42.920
George starts from hundred.

07:43.580 --> 07:44.780
You can see that.

07:44.930 --> 07:49.400
So Steve let's go ahead and execute the query 101.

07:49.730 --> 07:52.130
And now that is interesting.

07:52.160 --> 07:53.540
Go ahead and check it out.

07:54.230 --> 07:59.030
102 and then global.

07:59.030 --> 08:02.840
And that is 103.

08:02.870 --> 08:05.060
So you can see it's very interesting.

08:05.060 --> 08:10.640
And you can start your autoincrement from anywhere to go ahead and alter your table and then begin to

08:10.670 --> 08:14.270
insert your data into your rows.

08:14.270 --> 08:15.950
And it's going to work out fine.

08:15.980 --> 08:18.320
So that is it about Autoincrement.

08:18.320 --> 08:19.790
And I hope that is very interesting.

08:19.790 --> 08:21.620
So play around with it, check it out.

08:21.620 --> 08:25.610
And if you have any question why had I used the question and answer section.

08:25.610 --> 08:28.220
And I'm going to get back to you as soon as possible.

08:28.250 --> 08:29.270
Thank you so much.

08:29.270 --> 08:32.510
And I'm going to see you in the next video lecture.
