Solved

MySQL: Merge Tables on non primary column

Posted on 2014-01-22
3
403 Views
Last Modified: 2014-01-27
I have two MySQL tables with identical structures that need to be merged.

The primary key is "bookkey" however the two tables should be merged on the column `isbn`.

I want rows from `booksrevised` to be added to `books` if the isbn value is not already in `books` and I want the rows in `books` to be updated to contain the values from `booksrevised` when the value of isbn is the same.
CREATE TABLE `books` (
  `bookkey` int(11) NOT NULL,
  `isbn` varchar(13) NOT NULL,
  `title` varchar(50) NOT NULL,
  `description` varchar(50) NOT NULL,
  PRIMARY KEY (`bookkey`)
);
INSERT INTO `books`(`bookkey`,`isbn`,`title`,`description`) VALUES
(1,'9780000000001','Hello','Hello World'),
(2,'9780000000009','Test','Example'),
(3,'9780000000022','Demo','Testing'),
(4,'9780000001412','Word','Test'),
(5,'9780000004235','Zooph','Hi');

CREATE TABLE `booksrevised` (
  `bookkey` int(11) NOT NULL,
  `isbn` varchar(13) NOT NULL,
  `title` varchar(50) NOT NULL,
  `description` varchar(50) NOT NULL,
  PRIMARY KEY (`bookkey`)
);
INSERT INTO `booksrevised`(`bookkey`,`isbn`,`title`,`description`) VALUES
(1,'9780000000001','Hello World','Hello World 123'),
(2,'9780000000126','New','New Record'),
(3,'9780000000022','Demo','Testing'),
(7,'9780000001419','ABC','XYZ'),
(8,'9780000004235','Zooph','Hi');

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 24

Assisted Solution

by:johanntagle
johanntagle earned 150 total points
ID: 39802052
I assume here it can never happen that you have the same isbn but different bookkey - only new books will have a bookkey that doesnt exist in books?  Maybe you should consider using auto_increment for your primary key?

Although books.isbn is not primary, that values for this column are still unique, right?  Then you can add a unique key to it.  

ALTER TABLE books add unique key (isbn);

Open in new window


This way you can use INSERT ON DUPLICATE KEY UPDATE:

INSERT books (`bookkey`,`isbn`,`title`,`description`) 
SELECT `bookkey`,`isbn`,`title`,`description`
FROM booksrevised
ON DUPLICATE KEY UPDATE title=values(title), description=values(description);

Open in new window


Come to think of it if the same isbn will also have the same bookkey between the two tables, then there's no longer any need for a unique key for isbn.  But if this is not the case, then you should use auto_increment for bookkey and do not select it from booksrevised.  This way you can avoid conflicts.

See http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html for more details
0
 
LVL 8

Assisted Solution

by:Surrano
Surrano earned 150 total points
ID: 39802297
If my understanding is correct bookkey is some artificial identifier and it *should* be the same in the two tables but it is not.

Also, I assume same ISBN  means same title and description, at least that's what you'd like to achieve with this merge.

I think the structure of your data is incorrect and that's what's causing you headaches.

Try the following:

1. Create one table containing details like title and description, with ISBN as primary key
2. Merge there
3. Drop columns title and description from the other two tables
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 200 total points
ID: 39805521
Do you need to maintain bookkey? If not, you could do this:

alter table books add unique (isbn);
alter table books modify column bookkey int(11) auto_increment;
replace books (isbn, title, description) SELECT isbn, title, description from booksrevised;

http://sqlfiddle.com/#!2/69fa9/1
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now