Web Info and IT Lessons Blog...

Monday, 27 October 2014

MySql Insert Query

The previous lesson of MySql Lessons series was about Introduction and Basics of MySql and in this lesson we will learn to insert record in a table using MySql query.

MySql Insert Query

We will use the table we created in the previous lesson and insert data in it by executing a MySql query. The table we created in the previous lesson looks like this:

Insert Table Data

There are several methods to write a MySql insert query. The first method to write MySql insert is this:


INSERT INTO `test table` VALUES('','Micheal','micheal@gmail.com')

The above query will insert record in a table named "test table" containing 3 columns id, name and email. The VALUES part of the query contains the data to be inserted in the test table. The first value is empty because id is auto increment and the other two values specified in the query will be inserted in the table.

The second method to write an insert query is this:


INSERT INTO `test table` (id,Name,Email) 
VALUES('','Albert','albert@gmail.com')

The above method is same as the first method except for the fact that we have specified the columns of the table before the VALUES part.

The third method to write an insert query is this:


INSERT INTO `test table` SET Name = 'Tom', Email = 'tom@gmail.com'

The meaning and output of all the three methods is the same. The difference lies only in the syntax. You can use any of the above methods to insert record in a table.

How to execute a query in MySql?

In the above methods we learnt to write the insert query but to insert record in a table we will have to run the query in the SQL panel. To run a query click on the SQL tab in the top menu of phpMyAdmin. You will see a screen like this:

SQL Panel

You can see in the above image that there is already a query written in the text area. Remove the query already written and write your own insert query in the text area. Click on the "Go" button below to run the query. To see the new record inserted in the table, click on the Browse tab in the top menu.

MySql Inserted Records



No comments:

Post a Comment