In MySQL, The INSERT INTO statement is used to add new records (rows) to a table.
INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2, ..., valueN);
table_name: The name of the table to which we want to insert data.
column1, column2, ..., columnN: The columns into which we want to insert data.
VALUES: The keyword used to specify the values to be inserted.
value1, value2, ..., valueN: The values to be inserted into the corresponding columns.
INSERT INTO customers (first_name, last_name) VALUES ('Alice', 'Collin');
This query inserts a new row into the customers table with values for the "first_name", "last_name" columns.
INSERT INTO customers (customer_id, customer_name, email) VALUES (CUS1001, 'Alice Inc.', '[email protected]');
This query inserts a new row into the "customers" table with values for the "customer_id", "customer_name", and "email" columns.
INSERT INTO products (product_name, price) VALUES ('iPhone XR', 1200), ('onePlus 9x', 800), ('Samsung Flip', 500);
This query inserts multiple rows into the "products" table with values for the "product_name" and "price" columns.
We can use the INSERT INTO ... SELECT statement to insert data into a table based on the results of a SELECT query.
INSERT INTO new_customers (customer_id, customer_name, email) SELECT customer_id, customer_name, email FROM old_customers WHERE created_at > '2024-01-01';
This query inserts rows into the new_customers table based on the result set of the SELECT query.