Insert 3 rows by a single MySQL insert statement
MySQL insert into Statement: Exercise-5 with Solution
5. Write a MySQL query to insert 3 rows by a single insert statement.
Sample Solution:
-- Inserting multiple records into the 'countries' table
INSERT INTO countries VALUES('C0001', 'India', 1001),
('C0002', 'USA', 1007), ('C0003', 'UK', 1003);
Let execute the above code in MySQL command prompt.
Here is the structure of the table:
mysql> SELECT * FROM COUNTRIES; +------------+--------------+-----------+ | COUNTRY_ID | COUNTRY_NAME | REGION_ID | +------------+--------------+-----------+ | C0001 | India | 1001 | | C0002 | USA | 1007 | | C0003 | UK | 1003 | +------------+--------------+-----------+ 3 rows in set (0.00 sec)
Explanation:
The above MySQL code inserts multiple records into the 'countries' table with distinct values for 'country_id', 'country_name', and 'region_id'. Three records are inserted, representing countries with IDs 'C0001', 'C0002', and 'C0003', along with their respective names and region IDs.
Go to:
PREV :Write a MySQL query to insert NULL values against region_id column for a row of countries table.
NEXT :
Write a MySQL query insert rows from country_new table to countries table.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?