Hy Folks, Today we will learn how to insert a row after a specific row in MySqlWorkbench.We already know simple insert query to inserting the rows row in MySQL but lot's of peoples confused about how to insert a row in the middle of the rows or specific rows. In this post, I will cover your Doubts and Confusions and give you the best solution.


Task -  You have DB and Table where 6 rows inserted and you have to insert new rows after the 3rd rows.


So Let's Complete the task and learn it.

I have made some points with the help of you can easily understand.

Note*  Please do follow the Steps. 


I have uploaded a video of it here. you can watch and easily understand. Click here


Step - 1  

Open your MySql workbench or phpMyAdmin Tool. I have MySqlWorkbench so I will demonstrate it to you.


Step - 2  

 First, create a Database and table I hope you have already created. I have created DB name is test and table name is demo_table. I have inserted 6 rows in the table as shown in the image below.


How to insert row after specific row in MySqlWorkbench ?  | How to insert a row in the middle of the rows in MySQLWorkbench ? | MySQL | ShortTutorials007Website



Step - 3  

As per the above task we have to insert a new row after the third row ( column name Id ). let's think about how can we do it.


Logic 

First, we have to update and plus one increment of those id's which is greater than 3 because we want to insert a row after the 3rd rows.

So let's write the query for it.


UPDATE  test.demo_table set id = id +1 WHERE  id  >= 4 order by id desc;


How to insert row after specific row in MySqlWorkbench ?  | How to insert a row in the middle of the rows in MySQLWorkbench ? | MySQL | ShortTutorials007Website

After executing this query you will see all those ids has been incremented which was greater than 3. which id was 4 before. now that id has been a change.


OUTPUT - 

How to insert row after specific row in MySqlWorkbench ?  | How to insert a row in the middle of the rows in MySQLWorkbench ? | MySQL | ShortTutorials007Website


Video -




Step - 4  

Now we have a row where we can insert the new row after the 3rd row which id will be 4. so just we have to insert a new row after the 3rd row.

 So let's write the Insert Query 


INSERT INTO test.demo_table (id,name,status,demo_tablecol) VALUE (' 4' , ' new entry for 4th row ' , 0,' ' );


OUTPUT - 


How to insert row after specific row in MySqlWorkbench ?  | How to insert a row in the middle of the rows in MySQLWorkbench ? | MySQL | ShortTutorials007Website


You can see in the above image where we have successfully inserted a new row and its value.

Finally, we have completed the Task and learned how can we insert a row after a specific row.

I hope you understood this and like this article. if you like it so, please give feedback in the comment section.



Post a Comment

Please do not enter any spam link in the comment section.

Previous Post Next Post