MySQL Tutorial
The INSERT INTO statement is used to insert new records in a table.
It is used to insert a single or a multiple records in a table.
There are two ways to insert data in a table.
This method is to specify only the value of data to be inserted without the column names.
INSERT INTO table_name
VALUES (value1, value2, value3....);
In this method we will specify both the columns which we want to fill and their corresponding values.
INSERT INTO table_name (column1, column2, column3....)
VALUES (value1, value2, value3.....);
column1, column2, column3,... are the names of the columns in the table into which you want to insert the data.
First we already create table name "students"
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
Below is a selection from the "students" table
Students table available in your database which you can use to store the required information related to Students.
The empty "Students" table will now look like this:
The following MySQL statement inserts a new record in the "students" table.
INSERT INTO students VALUES (‘1′,’Will’,’C++’);
INSERT INTO students VALUES (‘2′,’Sam’,’Python’);
INSERT INTO students VALUES (‘3′,’Sara’,’HTML’);
The table students will now look like:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML |
Now we will specify both the columns which we want to fill and their corresponding values.
The following SQL statement inserts a new record in the "students" table.
INSERT INTO students (ROLL_NO, NAME, SUBJECT)
VALUES (‘1′,’Will’,’C++’),(‘2′,’Sam’,’Python’);
INSERT INTO STUDENTS (ROLL_NO, NAME, SUBJECT)
VALUES (‘4′,’Rim’,’Java’ );
INSERT INTO STUDENTS (ROLL_NO, NAME, SUBJECT)
VALUES (‘5′,’Micheal’,’SQL’);
INSERT INTO STUDENTS (ROLL_NO, NAME)
VALUES (‘6′,’Lara’,);
The table students will now look like:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara |