Solved

MySQL: Merge Tables on non primary column

Posted on 2014-01-22
3
402 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

21 Experts available now in Live!

Get 1:1 Help Now