How to copy table without primary key to another one
Instead of SELECT *
, spell out the columns that you want:
INSERT INTO Data (time, serial_number...)
SELECT (time, serial_number...) FROM Temp_data;
You have to define the columns name in your query otherwise it will insert all the columns
INSERT INTO tbl2 (column1, column2)
SELECT tbl1.col1, tbl1.col2 FROM tbl1
This is a really annoying problem since every time you need to update the columns you also need to update the code that copies. Here's the solution I use to get around the problem and future proof the code:
DROP TABLE IF EXISTS TempData;
CREATE TEMPORARY TABLE TempData LIKE Data;
INSERT INTO TempData SELECT * FROM Data;
ALTER TABLE TempData CHANGE COLUMN `id` `id` INT(11) NULL, DROP PRIMARY KEY;
UPDATE TempData SET id = 0;
INSERT INTO Data SELECT * FROM TempData;
DROP TABLE IF EXISTS TempData;
Fast, easy and once written, providing the source table name is the same, works every time.