Last Updated: 19 July, 2023
In MySQL, we insert one or more records into a table using the INSERT INTO statement.
The syntax for the INSERT INTO statement is as follows:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Here's a breakdown of the different parts of the statement:
INSERT INTO specifies the name of the table where we want to insert records.
table_name is the name of the table where we want to insert records.
(column1, column2, column3, ...) specifies the names of the columns in the table where we want to insert records. If we don't specify the column names, we must provide values for all the columns in the order they appear in the table.
VALUES specifies the values we want to insert into the table.
(value1, value2, value3, ...) specifies the values we want to insert into the table. The values must be in the same order as the columns.
Here's an example of using the INSERT INTO statement to insert a new row into a table:
INSERT INTO student (name, class, section, roll) VALUES ("Ram", "UKG", "B", 5);
MySQL also allows us to insert more than one record into the table using the INSERT INTO statement.
The syntax for inserting multiple records using the INSERT statement is as follows:
INSERT INTO table_name ( column1, column2,...columnN ) VALUES ( value1, value2,...valueN ), ( value1, value2,...valueN ), ( value1, value2,...valueN );
Example:
INSERT INTO student (name, class, section, roll, age, gender) VALUES ("Ram", "UKG", "B", 18, 6, "M"), ("Pooja", "PLAY", "A", 38, 3, "M"), ("Raju", "UKG", "C", 7, 6, "M"), ("Gopi", "LKG", "A", 4, 6, "M"), ("Rani", "PLAY", "B", 9, 6, "F");
After inserting the records successfully into tables, we can see the inserted records using the SELECT statement.
mysql> select * from student; +----+-------+-------+---------+------+------+--------+ | id | name | class | section | roll | age | gender | +----+-------+-------+---------+------+------+--------+ | 1 | Ram | UKG | B | 18 | 6 | M | | 2 | Pooja | PLAY | A | 38 | 3 | M | | 3 | Raju | UKG | C | 7 | 6 | M | | 4 | Gopi | LKG | A | 4 | 6 | M | | 5 | Rani | PLAY | B | 9 | 6 | F | +----+-------+-------+---------+------+------+--------+ 5 rows in set (0.02 sec)
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com