Syncing primary key between two tables stored in two different database - mysql

I have two MySQL database that contain two table, let's call them TABLE_A and TABLE_B. Both these tables have as fields id and title. Both databases are on the same server and the same user can access both.
Now, TABLE_A is a subset of TABLE_B for what concern the title field. It means that every title in TABLE_A is present in TABLE_B, too. While id fields of the two table are in no way related.
What I need is to sync id fields in TABLE_A with id fields in TABLE_B, according to the title, i.e. same title, same id. If it's not clear, I have to save TABLE_B id and override TABLE_A ones. And I DON'T have to add missing title from TABLE_B to TABLE_A.
Someone suggested to use a temporal table where to copy all TABLE_B fields in common with TABLE_A and then rename it as TABLE_A. I cannot follow this way, as TABLE_A actually has also other fields that I need to maintain. So, I cannot entirely drop the old TABLE_A.
Moreover, id is the primary key for both tables. It means that I cannot simply copy from TABLE_B to TABLE_A as the query will fail as soon as I try to change one id to another one that is already present in TABLE_A but linked to a different title.
I know how to write a Perl or PHP script to do it, but I would like to know if a pure MySQL solution exists.

You can do this
CREATE TABLE TableA_TMP AS
SELECT * FROM TableA;
ALTER TABLE TableA_TMP ADD id_new int;
UPDATE TableA_TMP A INNER JOIN TableB B ON lower(A.title) = lower(B.title)
SET id_new = B.id;
RENAME TABLE TableA TO TableA_backup;
CREATE TableA AS
select id_new as id, title,.... from TableA_TMP;

Related

How to merge two db with same structure but different data

I'm working with phpmyadmin and I have to merge two db with same structure but different data.
The db have relation between tables (foreign key).
The data in two db may have same id, and so their foreign key.
I would like to know if it's possible merge the two db keeping all data, so, if a row already "exist", insert it with new id and update its foreign key.
thanks a lot
No easy way unfortunately. If you have TableA as a foreign key to TableB, you will need to
1) Insert data from source tableA to target tableA
2) create a (temp) table to store the mapping between source tableA ids and target tableA ids
3) Use this mapping table when inserting data from tableB to convert the tableA ids to the new ones in the target db
... and so on. It can get quite hairy if you have a deep hierarchy of tables, but hopefully you get the idea. Take backups before you start.
Another idea that you might want to consider is using a cursor:
Assume table A is the one that you want to keep and table B is the one you want to remove.
Declare a cursor for table B and select all the records.
Loop each record selected from the cursor and check.
Case 1: If the ID is exists on table A, insert the record to table A with same details.
Case 2: If the ID is exists on table B, insert the record and modify the ID and foreign key.
Once all the records have been checked, drop table B.
Sorry, I just can give an idea at the moment.

How do I migrate and sync up a table efficiently?

I have a table A, it has millions of records and it's growing. A new column needs be added to table A with indexing, but it could be headache to migrate such a large table. So table B is created at some point from table A, question is how to sync up these 2 tables efficiently?
There're multiple scenarios new records will be added to table A.
To 'Sync' up two tables without actually merging them you can create a UNION VIEW. A VIEW can be used just like a table for calculation, manipulation, data storage etc. This is assuming that both tables have the same amount of rows, if not you'll need to create primary and foreign keys.
CREATE OR REPLACE VIEW viewname AS
SELECT * FROM TABLE_A
UNION ALL
SELECT * FROM TABLE_B
Now if both tables don't share the same amount of rows you'll need at least 1 field in common between the two tables called primary and foreign keys, to join the tables using the primary and foreign keys you'll need to use a JOIN like this:
CREATE OR REPLACE VIEW viewname AS
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
LEFT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
UNION ALL
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
RIGHT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
It depends on what type of join you want but I think FULL JOIN will give you the best results, FULL JOIN's aren't supported in MySQL but using LEFT JOIN RIGHT JOIN and UNION ALL mimics the same results.
Or if you simply want to copy all the records from table A to table B you could use this.
INSERT INTO TableB
SELECT * FROM TableA;

Map Values from Column in Table A to Corresponding Column in Table B

Part 1:
In MySQL suppose I have Table A which has more columns than Table B. I want to transfer values from Table B to Table A where the id row in A matches the id Row in B and update the values in table A from the values in table B.
Part 2:
Table B is a superset of table A, so how does one insert ids and their corresponding values from table B into table A while also updating id's that are in table A.
Like FreshPrinceOfSO already mentioned in the comments, you won't get code for free here.
But here are at least the steps. Two possibilities. Either you split the work up in two statements, one update then one insert statement. Or you could work with
INSERT ... ON DUPLICATE KEY UPDATE ...
You would have to have an unique index on the table for this to work.
For the first solution mentioned you'd inner join the tables for the update first, that's trivial. Then for the insert you'd use a select with a left join and with is null checking for entries that are not already in the table.
Good luck...

Inserting values into a third table by CROSS JOINing two tables

I want to insert values into a Table_C by cross joining two tables Table_A and Table_B.
Table_A contains two attributes ID and item.
Table_B contains two attributes ID and color.
Table_C contains four attributes ID,item,color,quantity.
All IDs have AUTO INCREMENT.
suppose each item can have all color and I need to create a relation about it.
How should I write a query for this? How could I reference a relation cross joining item and color.
What my solution is create an intermediate third table joining these two tables and then use that table to insert values into Table_C. But I am pretty sure that there is a better optimized solution for this.
Thanks in advance.
No need for a temp table... You can do:
insert into ...
select ... from ...
Write the query you'd need to "fill" that temp table you mention, and insert the rows directly into your final table.
Here is the query which worked for me.
INSERT INTO Table_C (SELECT null, Table_A.item, Table_B.color, null FROM
Table_A CROSS JOIN Table_B);

Update multiple tables using a Stored Procedure

I want use a stored procedure to update multiple tables in a db. Each table has a GUID as the PK and there are FK's between the tables.
For example, one table is "Tool" with a column ID (the guid) and another table is "Type" with ID as guid again. There is a column in Tool called "TYPE_ID" that is a FK to the table Type with Type's Guid stored in it. I want to first update the Tool table and then after, update the Type table based on that FK.
UPDATE Tool
SET Name=#Name, [Enabled]=#Enabled, TestMode=#TestMode, SerialNumber=#SerialNumber,
Andon=#Andon, ChimeZone=#ChimeZone, Number=#ToolNumber
WHERE ID=#ID
Update Type
SET Type=#Type
WHERE Tool.ID=#ID AND
Tool.TYPE_ID=Type.ID
I know that this code is incorrect for the second update, but this is the gist of how I would like to be able to do it. Is there a way to not have to SELECT the FK Guid, store it, and use it the next update? If that is the only way, how do I do that?
Write your second update like this, joining the Tool table to the Type table:
UPDATE ty
SET Type = #Type
FROM Tool to
INNER JOIN Type ty
ON to.TYPE_ID = ty.ID
WHERE to.ID = #ID
Actually you can UPDATE FROM clause:
UPDATE Type
SET Type=#Type
FROM Tool INNER JOIN TYPE
ON Tool.TYPE_ID=Type.ID
WHERE Tool.ID=#ID;
See http://msdn.microsoft.com/en-us/library/ms177523.aspx for full UPDATE syntax.