To copy the contents of one table (Table A) to another table (Table B) in MySQL, you can use the INSERT INTO
statement. Here’s an example of how to do it:
INSERT INTO table_b SELECT * FROM table_a;
This statement selects all rows from Table A and inserts them into Table B. The column names and data types of Table A and Table B should match for this to work correctly.
If you want to copy only specific columns from Table A to Table B, you can specify the column names in the SELECT
statement:
INSERT INTO table_b (column1, column2, column3) SELECT column1, column2, column3 FROM table_a;
In this example, only column1
, column2
, and column3
will be copied from Table A to Table B.
Make sure that Table B already exists with the correct structure (columns and data types) before running the INSERT INTO
statement. If Table B doesn’t exist, you can create it using the CREATE TABLE
statement before copying the data.
Note that the INSERT INTO
statement will append the data to Table B, so if Table B already contains data, the new data will be added after the existing rows. If you want to replace the data in Table B completely, you can truncate the table before copying the data:
TRUNCATE TABLE table_b;
INSERT INTO table_b SELECT * FROM table_a;
The TRUNCATE TABLE
statement removes all rows from Table B before inserting the data from Table A.
Remember to backup your data before performing any operations that modify or copy data in your database.