Data synchronization between tables in mysql - mysql

I have two tables (let's call them A and B) with same structure and i need to synchronize data in them...
There's one primary key field, with same value in both tables, and several fields with value in table A and null (or obsolete value that need to be replaced with current value from table A) in table B... I need to copy value from table A to table B.
Is there any easy way (other than replication) to do this in mySQL 4.1?
thanks in advance

Try this -
UPDATE table_b b, table_a a
SET b.field1 = a.field1, b.field2 = a.field2
WHERE b.primary_key = a.primary_key
add the fields as required.

Can you just do:
INSERT INTO table1 (field1,field2,field3)
SELECT field1,field2,field3
FROM table2;
Or do you actually already have data in table2 and you need to update it rather than insert new columns?

Related

Best way to write SQL delete statement, deleting pairs of records

I have a MySQL database with just 1 table:
Fields are: blocknr (not unique), btcaddress (not unique), txid (not unique), vin, vinvoutnr, netvalue.
Indexes exist on both btcaddress and txid.
Data in it looks like this:
I need to delete all "deletable" record pairs. An example is given in red.
Conditions are:
txid must be the same (there can be more than 2 records with same txid)
vinvoutnr must be the same
vin must be different (can have only 2 values 0 and 1, so 1 must be 0 other must be 1)
In a table of 36M records, about 33M records will be deleted.
I've used this:
delete t1
from registration t1
inner join registration t2
where t1.txid=t2.txid and t1.vinvoutnr=t2.vinvoutnr and t1.vin<>t2.vin;
It works but takes 5 hours.
Maybe this would work too (not tested yet):
delete t1
from registration as t1, registration as t2
where t1.txid=t2.txid and t1.vinvoutnr=t2.vinvoutnr and t1.vin<>t2.vin;
Or do I forget about a delete query and try to make a new table with all non-delatables in and then drop the original ?
Database can be offline for this delete query.
Based on your question, you are deleting most of the rows in the table. That is just really expensive. A better approach is to empty the table and re-populate it:
create table temp_registration as
<query for the rows to keep here>;
truncate table registration;
insert into registration
select *
from temp_registration;
Your logic is a bit hard to follow, but I think the logic on the rows to keep is:
select r.*
from registration r
where not exists (select 1
from registration r2
where r2.txid = r.txid and
r2.vinvoutnr = r.vinvoutnr and
r2.vin <> r.vin
);
For best performance, you want an index on registration(txid, vinvoutnr, vin).
Given that you expect to remove the majority of your data it does sound like the simplest approach would be to create a new table with the correct data and then drop the original table as you suggest. Otherwise ADyson's corrections to the JOIN query might help to alleviate the performance issue.

MySQL two table dump, merge, between two instances

Is there a method for comparing data between 2 tables between 2 database instances and then merging them?
So there was everything in both.
Thank You.
In mysql you can select data between table that are in different datababase simply using a query as :
select A.col1, B.col1
from databaseA.tableA as A
inner join databaseB.tableB as B on A.colkey1 = b.colkey1
for merge you can use JOIN or UNION depending by you needs
and last you can use a INSERT SELECT for populate the table you need
If your table schemas are the same, you can just take a dump of one and do a simple INSERT INTO ... SELECT ... ON DUPLICATE KEY UPDATE <your key clash logic> to the other.

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

MySQL - how to insert values from one table into another with string matching?

MySQL noob here; looked around first but couldn't find the answer to this question.
So I have two tables in MySQL, one (Table1) which consists of one column containing unique list of names (Col1) and another containing corresponding binary values (Col2). The second table (Table2) contains a list of recurring names with a empty column waiting to be filled with binary values from the first table. What I want to do is, for each instance of a recurring name in Table2, insert the binary value from Col2 associated with the matching unique name in Table1.
I know how to do this in Excel—you just place the following VLOOKUP statement next to every row containing a recurring name. In the following code snippet A2 is a recurring name, unique names are contained in column B, and the binary values are contained in column C.
=VLOOKUP(A2,$B$2:$C$106095,2,FALSE)
But I can't for the life of me figure out how to reproduce this effect in MySQL. I can't use Excel because there's too much data. Anyone have any ideas? Thanks in advance!
I think that you want something like this (I don't know what the Excel statement does):
UPDATE table2 JOIN table1 ON table1.col1 = table2.col1
SET table2.col2 = table2.col2
WHERE table2.col2 IS NULL
This will update each row table2 that has col2 empty, searching for the corresponding row in table1 based on matching col1 columns.
Btw, do you have a reason to do this? Why not just join both tables when selecting the data? For example:
SELECT table2.col1, table1.col2
FROM table2 JOIN table1 ON table1.col1 = table2.col1

Mysql Faster UPDATE

I have 2 Innodb tables. IN TableA I have a column (guidNew) that I want to assign its' values to a column in TableB (owner) depending on the relation between the column in TableA (guid) and TableB (owner).
Basically Tabl6eB (owner) has multiple entries that correspond to one TableA (guid). This is a Many to One relation. I want to change the TableB(owner) value to the new TableA(guidNew) values.
This is an example of the query:
UPDATE `TableB`, `TableA`
SET
`TableB`.`owner` = `TableA`.`guidNew`
WHERE `TableB`.`guid` != 0
AND `TableB`.`owner` = `TableA`.`guid`;
Now I do not know if this is working or not because there are more than 2 million entries. Is there a way to know the progress it has AND more important, a way to do it faster.
Make sure that you have indexed the guid and owner columns.
Try using the EXPLAIN command to see how the query is being performed
EXPLAIN SELECT TableB.owner, TableA.guidNew
FROM TableB, TableA
WHERE TableB.guid != 0
AND TableB.owner = TableA.guid