Search This Blog

MySQL INSERT Query

To insert data into MySQL table you would need to use SQL INSERT INTO command. You can insert data into MySQL table by using mysql> prompt or by using any script like PHP.

Syntax:
Here is generic SQL syntax of INSERT INTO command to insert data into MySQL table:

INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );


To insert string data types it is required to keep all the values into double or single quote, for example:- "value".

Inserting Data from Command Prompt:
This will use SQL INSERT INTO command to insert data into MySQL table tutorials_tbl

Example:
Following example will create 3 records into tutorials_tbl table:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn PHP", "John Poul", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn MySQL", "Abdul S", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("JAVA Tutorial", "Sanjay", '2007-05-06');
Query OK, 1 row affected (0.01 sec)
mysql>


NOTE: Please note that all the arrow signs (->) are not part of SQL command they are indicating a new line and they are created automatically by MySQL prompt while pressing enter key without giving a semi colon at the end of each line of the command.

In the above example we have not provided tutorial_id because at the time of table create we had given AUTO_INCREMENT option for this field. So MySQL takes care of inserting these IDs automatically. Here NOW() is a MySQL function which returns current date and time.