Transferring all rows of a MySQL table to another - mysql

I want to bulk insert all rows from one table to another. I am confused on how to use Select with Insert. Is there a way that a new table is automatically created if it does not exist?

There are two ways to do this:
One is to INSERT INTO ... SELECT, which will insert the resultset of your query into an existing table with the same data structure as your query
INSERT INTO MyTable_Backup
SELECT * FROM MyTable
The other is to CREATE TABLE ... SELECT ..., which will create a new table based on the data structure of your query an insert the resultset.
CREATE TABLE MyTable_Backup
SELECT * FROM MyTable;
However one thing to note is that this will not match the indexes of the source table. If you need indexes, you need to add them manually.

trigger and/or select into are recommended here

Related

remove Duplicated data from very huge table

I have a table contains more than 500 millions records in MySQL database ,
i need to remove duplicated from it ,
i tried this query on table contain 20 millions , it was ok but for the 500 millions it take very long time :
-- Create temporary table
CREATE TABLE temp_table LIKE names_tbles;
-- Add constraint
ALTER TABLE temp_table ADD UNIQUE(name , family);
-- Copy data
INSERT IGNORE INTO temp_table SELECT * FROM names_tbles;
is there better solution ?
One option is aggregation rather than insert ignore. That way, there is no need for the database to manage rejected records:
insert into temp_table(id, name, family)
select min(id), name, family
from names_tbles
group by id, family;
I would take one step further and suggest adding the unique constraints only after the table is populated, so there is no need for the database to check for duplicates (the query guarantees that already), which should speed up the insert statement.

how can i move all data of one table to another table

I need to transfer all data of one table to another dumping table.
My purpose is to get table ready for daily transaction and previous data should be moved to another table which stores every days data.
i need mysql syntax for this, thank you in advance for your support and help
You can try these queries:
This query will copy the data and structure, but the indexes are not included:
CREATE TABLE new_table SELECT * FROM old_table;
To copy everything, including database objects such as indexes, primary key constraint, foreign key constraints, triggers run these queries:
CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * FROM old_table;
To insert data into an existing table, use this :
INSERT INTO table2 SELECT * FROM table1

Query to insert into multple table from one select query

Probably a noob question and there are workaround, but just to know if any SQL expert can provide a better solution for this:
We know about this query:
Insert INTO table1 (column1, column2)
Select column1, column2
FROM table2
But i was wondering if there is a way to insert into multiple table using this query? As i've a select statement that provide a table of data which i needed to insert into multiple table. This option is purely for data migration case, and i don't want to use cursor. So any alternatives?
No, you cannot insert records into multiple tables inside one query.
What you can do though is to insert data into a temporary table first. Then you can insert into multiple tables from your temp table (inserting into one table at the time). This way you won't have to select the data multiple times.

How to update all fields from one table to another table in the same database using mySQL?

There are 2 tables in the same database with the same structure. I want to copy all data from one table to the other table using mySQL. The source table may have the same, less or more number of rows of the destination table.
I tried searching. I found 2 approaches:
Approach #1
TRUNCATE destination;
INSERT INTO destination SELECT * FROM source
Approach #2
DROP TABLE destination;
CREATE TABLE destination SELECT * FROM source
Isn't there any other approach involving UPDATE?
Update I don't think so.
You can do Insert
Insert into destination
(
column_1,
column_2,
....
)
SELECT
column_1,
column_2,
....
FROM source
Note: No. of columns mention in destination = No. of columns mention in source
By the approach #1 will not work always.
and approach #2 will always work

Is it possible to append the columns (structure and content) of one mysql table to another?

I would like to copy the structure and the content of one mysql table to another, adding all of the columns and values of that table to the already existing ones in the other table.
I could do it manually, but since I'm talking about a large amount of columns, it would be great if there were some sort of ALTER statement to help me do that.
EDIT:
To explain myself better:
I first need to add the columns contained in table B (column_name, data_type) to table A (which already has its own set of columns). Once that is done, I can copy the content, which is easy.
I guess the real question is: is there a way to add the columns contained in table B to another table (table A) which has columns of its own?
To build on flavianatill's second solution, it seems to me that the export/import step is not needed. If I understand the problem correctly, the following one-liner should do it.
CREATE TABLE IF NOT EXISTS merged_table AS (SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id);
Sorry, I would have put this in a comment but I lack the reputation!
This will copy all data from a source table to a target table. You can specify which columns should go to which. by changing the names of targetColumn.. and sourceColumn....
INSERT INTO targetTable (
targetColumn1
targetColumn1
targetColumn1
....
targetColumnN
)
SELECT
sourceColumn1
sourceColumn1
sourceColumn1
....
sourceColumnN
FROM sourceTable
You can also create sourceTable by doing
CREATE TABLE targetTable LIKE sourceTable
EDIT A method to pull all data from sourceTable to targetTable, however removing targetTable if it exists
DROP TABLE IF EXISTS targetTable;
CREATE TABLE targetTable LIKE sourceTable;
INSERT INTO targetTable SELECT * FROM sourceTable;
EDIT If you need to keep old data, you may need to remap it but you can merge in other tables
CREATE TABLE targetTable LIKE sourceTable;
INSERT INTO targetTable SELECT * FROM sourceTable;
INSERT INTO targetTable ( fieldsToInsertTo ) SELECT fieldsToSelectFrom FROM oldTargetTable ON DUPLICATE KEY ......;
DROP TABLE IF EXISTS oldTargetTable;
RENAME TABLE targetTable TO oldTargetTable;
This will however potentially either require ON DUPLICATE KEY UPDATE ..... logic, or simply INSERT IGNORE on the second if you are happy throwing away any PRIMARY/UNIQUE key conflicting rows. This assumes you have sourceTable you want to copy and merge with data from oldTargetTable. The table targetTable is just a temporary name.
If you wanted to prefer data from the old table then just swap the order you perform the INSERTs of course