INSERT ... SELECT copies rows returned by a query into an existing destination table. This is useful when the target table already exists and you want to copy all rows, selected rows or selected columns.
student : 35 records
student2 : 0 records
The examples assume compatible destination column types and a primary key on id.
INSERT INTO student2
SELECT *
FROM student;
For production code, explicit column lists are safer because they make the mapping clear if a table definition changes.
INSERT INTO student2 (id, name, class, mark, gender)
SELECT id, name, class, mark, gender
FROM student
WHERE class = 'Four';
INSERT INTO student2 (id, name, class, mark)
SELECT id, name, class, mark
FROM student;
If an omitted destination column permits NULL or has a default, MySQL supplies the appropriate value. Otherwise the insert can fail.
#1062 - Duplicate entry '3' for key 'PRIMARY'
A normal INSERT fails when an incoming row conflicts with a destination PRIMARY KEY or UNIQUE key. Decide explicitly whether a conflict should be rejected, updated or replaced.
REPLACE INTO student2 (id, name, class, mark, gender)
SELECT id, name, class, mark, gender
FROM student
WHERE class = 'Four';
INSERT INTO student2 (id, name, class, mark, gender)
SELECT id, name, class, mark, gender
FROM student
WHERE id = 3
ON DUPLICATE KEY UPDATE mark = 5;
This inserts the row when no conflict exists and updates mark when the incoming key already exists.
INSERT INTO student2 (id, name, class, mark, gender)
SELECT src.id, src.name, src.class, src.mark, src.gender
FROM (
SELECT id, name, class, mark, gender
FROM student
WHERE class = 'Four'
) AS src
ON DUPLICATE KEY UPDATE
mark = 200,
gender = src.gender;
The derived-table form provides an unambiguous name for values coming from the source query and avoids relying on the deprecated VALUES(column) form.
INSERT INTO plus2_inv_stock (p_id, qty, price_sell)
SELECT p_id, 0, 0
FROM plus2_inv_products;
The source and destination do not need identical layouts; the selected expressions only need to match the destination column list in number and compatible data type.
TRUNCATE TABLE student2;
TRUNCATE TABLE removes all rows, so use it only when deleting the complete destination dataset is intentional.
Copy data into a new table · ON DUPLICATE KEY UPDATE · WHERE filtering · Rename tables · Export records as CSV
Download the student table SQL dump
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| nikhil | 20-05-2009 |
| plz tell me about, database? what is the procedure to copy one database to annother database in mysql. | |
| shekhar sinha | 06-08-2009 |
| can constraints be copied from one table to another table? | |
| shekhar sinha | 06-08-2009 |
| can only primary key data be deleted or dropped? | |
| shekhar sinha | 06-08-2009 |
| can we define more than one primary key in one table? | |
| prasath | 18-01-2010 |
| select * into "new table name" from database.dbo.tablename | |
| eliazar espina | 17-07-2010 |
| can you help me with copying data from table1 to table2 for example in postgres database.. | |
| nikita | 18-08-2010 |
| can we copy content of a table to another table using || | |
| swapna.k | 07-10-2010 |
| Could any one tell me the answer how to Create one table from another table without copying the data from the first table. | |
| Sromana Mukhopadhyay | 16-03-2018 |
| I really like the REPLACE SELECT statement. | |