I welcome you back again to another video lecture in MySQL.
And in this video lecture let's go ahead and learn about auto increment of I.
So in the previous lecture we discussed about the primary keys.
Now if you want to add the auto increment that auto increment should be associated with your primary
key.
So any column that you add the primary key to you go ahead and add auto increment to that.
So that is why it's always good to add that to the ID.
So our ID will automatically be incremented.
So we don't just need to add the ID by ourselves.
So whenever we want to insert a name or anything inside our database, the ID will automatically increment.
And that is how it's supposed to be.
We don't need to always add that maybe 123456, because your database might get up to a million and
you don't even know where you stopped.
So how can you be able to add the ID by yourself every time?
So in order to overcome this issue by adding the ID ourself.
We just need to impute people's data and we go ahead and get the ID automatically inserted.
And then governments know you by your ID, and that ID will automatically be there and it will automatically
be created for you.
So how can we be able to do that?
Oh, and before we do that, you can see I've deleted the teachers table I created here.
You can go ahead and use maybe the employees table or the students table or anyone, but I deleted or
dropped the teachers table so that I can actually use the teachers table to help us to understand what
we are doing.
We just need to insert only the ID and the first name.
You can go ahead and add last name or salary, GP, anything and so on.
So now in a table has been dropped.
I need to create a new teachers table with a primary key and auto increment there.
So in order to do that let's go ahead and create a new table.
So go ahead and say table.
And the table name is going to be teachers.
And we're going to have less Mr. Macron at the end of this.
So inside the teacher's table, we go ahead and, uh, add a touch ID, right?
So touch ID now the add is going to be integer variable.
And the data type is integer and not integer variable.
So the teacher's id has a data type of int for integers.
And then it's going to also be the primary key.
And now we have added a primary key to the teacher's ID we go ahead and add the auto increment.
So go ahead and say auto increment.
And I'll put a comma at the end of that.
Now the auto increment will help the ID to always increment on its own.
We don't need to actually impute that by ourselves.
Now we go ahead and get the critical for the first name.
So I'm going to say first underscore name.
And the data type is varchar.
And the character length is going to be deity and that is all.
Now let's go ahead and select the insert movie to see what we just created.
So select all from teachers and that is all semicolon at the end.
And go ahead under query.
And you can see we have a teacher's ID I will have the first name right in there.
So we have now to create this table.
And what we need right now is to go ahead and insert some rules into this.
And in creating these rules, we are not going to insert the teacher's ID by ourselves.
Now let's go ahead and remove whatever we have right in there.
Now let's go ahead and insert the rules.
So go ahead and say insert into teachers.
So into the table.
Go ahead and uh insert the values.
And the values I'm going to add is just going to be the first name.
All right.
So and uh I'll go ahead and add.
Let me say, George, why did I add George?
And then if I should put it in, this is going to give you an error.
Let's go ahead and check it out.
We're going to get an error.
We can see down here George.
Column count does not match of value counts at row one.
So you need to specify that you're adding this just to the first name only.
So go right in here and say first underscore name.
And that is it.
So go ahead and execute a query again.
And you can see I have first name George.
And look at it just ID it automatically imputes one.
So if I should go ahead and add Steve and execute this query you can see it just ID incremented to two.
Now if I should continue and add one and then check it out teachers ID three.
And let's go ahead and change the last one.
What we see global.
And um.
Go ahead and Helen is a query to just ID is for so you can know to see how this works.
And the one interesting part of this is that the auto increment can start any way.
Very very interesting path of this auto increment.
So you can actually start from 100.
You can start from ten.
You can start from 20.
You can start from 25 and 18.
Any number of your choice to 1 million.
You can start from 1 million or 1000.
Now let's go ahead and experiment with this and see how that works.
So if I should do that I'll first of all alter the table, go ahead and alter the table and want to
say alter the table.
And the name of the table is a the teachers table.
And if I go ahead I will set auto increment to be equal to any number of I choice.
Let's go ahead and say one dot to be from 100 and put a semicolon at the end.
Now let's go ahead and execute a query.
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.
All right.
So first of all go ahead and delete from.
So delete from teachers.
And if I should delete from teachers I'm going to delete every row.
Go ahead and execute the query.
And now teachers table is now empty again.
So I'll go ahead and insert uh some of the stuff we did previously.
So go ahead and uh, insert uh into teachers.
So go ahead and insert into teachers.
We have already known we're going to put in the first underscore name, because if we don't do that
we're going to get an error.
So the values.
So the values and the semicolon at the end of these.
So let's go ahead and start again with George.
So rest it again with uh so go ahead and start with George and let's go ahead and check it out.
George starts from hundred.
You can see that.
So Steve let's go ahead and execute the query 101.
And now that is interesting.
Go ahead and check it out.
102 and then global.
And that is 103.
So you can see it's very interesting.
And you can start your autoincrement from anywhere to go ahead and alter your table and then begin to
insert your data into your rows.
And it's going to work out fine.
So that is it about Autoincrement.
And I hope that is very interesting.
So play around with it, check it out.
And if you have any question why had I used 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.