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
Solved

MySQL: Merge Tables on non primary column

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple shopping cart database structure 5 71
change database name 2 36
SELECT query on two levels (detail and summary) 13 57
INDEX does not make a difference, why? 10 59
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.

808 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