?
Solved

MySQL: Merge Tables on non primary column

Posted on 2014-01-22
3
Medium Priority
?
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Assisted Solution

by:johanntagle
johanntagle earned 600 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 600 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 800 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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