Insert/Update from 2 database

Discussion in 'MySQL' started by Julian_lock, May 23, 2011.

  1. #1
    Hi All guru's, if I want to insert multiple row into current table from other database is it <insert into databaseA.table where databaseB.table = 'p9999'> does this query will insert new row into databaseA table taken from databaseB table with the info = to p9999???

    Is there any way that update the data such as search for missing row and update/insert into the table???
     
    Julian_lock, May 23, 2011 IP
  2. nvidura

    nvidura Well-Known Member

    Messages:
    1,780
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    150
    #2
    You need to insert missing rows only? Then you can use SQL NOT IN statement. But your way is not the optimum way to synchronize 2 databases.
     
    nvidura, May 24, 2011 IP
  3. Julian_lock

    Julian_lock Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes I do know its not the optimum way, but what is the best way to do it?
     
    Julian_lock, May 24, 2011 IP
  4. nvidura

    nvidura Well-Known Member

    Messages:
    1,780
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    150
    #4
    Search tutorials for "MySQL Replication" you got to define one database as a Master and other one as a Slave. When it comes to replication there are various approaches, each approach has its own set of pros and cons. Some are efficient for data insertion, some are efficient for data deletion, updation some others are efficient for data selection. If your application is written in Java you can use ehcache and terracotta.

    You might wonder why data replication is so complicated. Imagine 2 databases are living on 2 hosts. One is up and other one is down or busy. Then you request to make a database transaction, First database will execute the transaction, But 2nd will not. So they will not get synchronized properly.... Your request can be a MySQL select, update, delete, or insert command.. How are you going to handle this? Thats what make it complicated.
     
    nvidura, May 24, 2011 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    INSERT IGNORE INTO databaseA.table (SELECT * FROM databaseB.table);
    Code (markup):
     
    koko5, May 24, 2011 IP