8

We have the following scenario where foo has been renamed as foo1.

foo.col1 has been renamed as foo1.col11.

foo.col2 has been removed

In fact these used to be similar tables and I would like to copy data from A to B for these tables. How would I go about doing a simple migration given that the table/column names have undergone a change.

Database 'A'
create table foo {id pk, col1 varchar(255), col2 tinyint(1), col3 datetime);
create table foo_bar1 (id pk, foo_id fk, col4 datetime, col5 varchar(255));

Database 'B'
create table foo1 {id pk, col11 varchar(255), col3 datetime);
create table foo1_bar1 (id pk, foo1_id fk, col4 datetime, col5 varchar(255));
1
  • INSERT INTO B.foo (id, col11, col3) SELECT id, col1, col3 FROM A.foo Commented Nov 1, 2011 at 9:55

4 Answers 4

12

you should be able to do:

INSERT INTO B.foo1 (id, col11, col3) 
 SELECT id,col1,col3 FROM A.foo;

INSERT INTO B.foo1_bar1 (id, foo1_id, col4, col5) 
 SELECT id,foo_id,col4,col5 FROM A.foo_bar1;
Sign up to request clarification or add additional context in comments.

7 Comments

Is there a way to pass the database names as arguments to the sql script which will be executed on database 'B'
if you look at the sql statements above you can see that the database name is already included, you don't need to select a database in your sql script. eg. insert into <databasename>.<tablename> ... works even if you have not selected "<databasename>" before
No, is there a way to pass the source database as an argument (e.g. for 'A')
@priya: you could dynamically generate the statements in a language of your choice (php, ruby, Perl, Java...), or using MySQL's PREPARE FROM.
just found a possible way.. I added it as a new answer, it's a bit too long for a comment
|
1
BEGIN;
INSERT INTO B.foo1 (id, col11, col3) 
   SELECT id, col1, col3 
   FROM A.foo;
INSERT INTO B.foo1_bar1 (id, foo1_id, col4, col5) 
   SELECT id, foo_id, col4, col5 
   FROM A.foo_bar1;
COMMIT;

Comments

1

IF you know the columns that have been removed you can do a straight insert of query results:

INSERT INTO B.foo1 (id, col11, col3) (SELECT id, col1, col3 FROM A.foo);
INSERT INTO B.foo_bar1 (id, foo1_id, col4, col5) (SELECT id, foo_id, col4, col5 FROM A.foo_bar1);

Comments

1

another solution that includes your 2nd question to set the source database name dynamically

create your script 'bla.sql':

SET @q = CONCAT('INSERT INTO B.foo1 (id, col11, col3) 
  SELECT id,col1,col3 FROM ',@sourcedb,'.foo');
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

(add additional statements as needed)

then run from console:

mysql -e "SET @sourcedb:='A' ; source bla.sql;" B -u root

source for this solution: variable database name and koneraks comment ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.