SQL INSERT INTO Command
Once the table has created, now that is ready for inserting the data. To insert a new record or a row of data into a table, we have to use the SQL INSERT INTO statement. The SQL INSERT INTO statement is used to insert a new row of data into the table in the database.
Syntax
There are two kinds of syntax used for writing a INSERT INTO statement.
1. INSERT INTO Table-name(column 1, column 2, column 3....column N) VALUES(value 1, value 2, value 3, ....value N); 2. INSERT INTO Table-name VALUES ( value 1, value 2, value 3, ....value N);
When you want to enter the data for specific columns then you can use the first syntax. Otherwise, you can use the second syntax to add data for all columns. In the second one, you don't need to specify the column names, but you must ensure the order of the values will be the same as the order of the columns in the table.
Example
Following is the example [student] table.
+--------+---------+------+-----------+ | RollNo | Name | Age | City | +--------+---------+------+-----------+ | 1 | Aruna | 18 | Chennai | | 2 | Varun | 19 | Bangalore | | 3 | Ara | 19 | Kochi | | 4 | Markdin | 18 | Mumbai | +--------+---------+------+-----------+
By using first syntax, you can insert the new record into the student table.
mysql> INSERT INTO student (RollNo, Name, Age, City) VALUES(5,'Kannan', 20, 'Kerale'); Query OK, 1 row affected (0.11 sec)
By using SELECT statement, we can see the entire student table in which the new record should be added. We will discuss the SELECT statement later in this tutorial.
mysql> SELECT * FROM student; +--------+---------+------+-----------+ | RollNo | Name | Age | City | +--------+---------+------+-----------+ | 1 | Aruna | 18 | Chennai | | 2 | Varun | 19 | Bangalore | | 3 | Ara | 19 | Kochi | | 4 | Markdin | 18 | Mumbai | | 5 | Kannan | 20 | Kerala | +--------+---------+------+-----------+ 5 rows in set (0.19 sec)
We can also use the second syntax to add a record into the student table.
mysql> INSERT INTO student VALUES(6,'Kanika', 18, 'Chennai'); Query OK, 1 row affected (0.44 sec) mysql> SELECT * FROM student; +--------+---------+------+-----------+ | RollNo | Name | Age | City | +--------+---------+------+-----------+ | 1 | Aruna | 18 | Chennai | | 2 | Varun | 19 | Bangalore | | 3 | Ara | 19 | Kochi | | 4 | Markdin | 18 | Mumbai | | 5 | Kannan | 20 | Kerala | | 6 | Kanika | 18 | Chennai | +--------+---------+------+-----------+ 6 rows in set (0.03 sec)