I move from one system to another, and in the process I will start both systems at the same time. I should be able to synchronize unidirectionally from one table to another, while preserving the primary keys of each table.
In this example, I have two tables (A) and (B). I need to synchronize value1 and value2 (below) from table B to table A based on a common foreign key (match1 and match2 below). Table A will have additional fields than in Table B, and there are several fields in B that will not be synchronized.
How can I do the following:
- Paste in the values ββthat were added to B
- Delete from A records deleted from B
- Update A with changed fields from B
Here are some demo data:
DROP TABLE IF EXISTS `a`; CREATE TABLE IF NOT EXISTS `a` ( `id1` int(10) unsigned NOT NULL, `match1` int(10) unsigned NOT NULL, `value1` varchar(255) NOT NULL, PRIMARY KEY (`id1`) ); INSERT INTO `a` (`id1`, `match1`, `value1`) VALUES (1, 1, 'A'), (2, 2, 'A'), (3, 3, 'B'), (4, 4, 'C'), (5, 5, 'C'); DROP TABLE IF EXISTS `b`; CREATE TABLE IF NOT EXISTS `b` ( `id2` int(10) unsigned NOT NULL, `match2` int(10) unsigned NOT NULL, `value2` varchar(255) NOT NULL, PRIMARY KEY (`id2`) ); INSERT INTO `b` (`id2`, `match2`, `value2`) VALUES (1, 1, 'A'), (2, 2, 'A'), (3, 3, 'B'), (4, 5, 'D'), (5, 6, 'D'), (6, 7, 'F');
The direction I'm going to right now is to create a joined table between two tables and build queries accordingly. For instance:
create table ab as (select * from a, b);
What do you suggest?