2012-03-15 5 views
1

저자와 도서로 두 개의 테이블이 있습니다. 저자 테이블의 데이터를 책 테이블에 삽입하고 싶습니다. 삽입이 성공적이면 작성자 테이블의 데이터가 삭제되고 성공하지 못하면 작성자 테이블에서 데이터가 삭제되지 않습니다. 롤백을 사용하고 싶지만 여기서는 작성자와 서적이 다른 서버에있는 두 개의 서로 다른 데이터베이스입니다. php와 mysql에 모든 빌딩 함수가 존재합니다.롤백의 PHP-mysql 대체 기능

+0

두 데이터베이스가 모두 InnoDB입니까? – biziclop

+0

지금까지 어떤 코드를 작성 했습니까? 우리가 당신을 도울 수 있도록 코드를 보여주십시오. –

+0

yes.two 데이터베이스는 모두 InnoDB – salma

답변

0

먼저 삽입 저자 테이블의 모든 행이 테이블 예약하기

insert into database1.book (fields_BookTable) 
    select fields_aAuthor_inBookTable from database2.author 

데이터가 책을 테이블에 삽입 된 경우 다음 id 필드를 사용하여 확인

delete from database2.author where id in (select id from database1.book); 
0

거래 하나의 "데이터베이스"에서 분리되어 .

실제로 수행해야하는 작업은 단일 데이터베이스 연결을 사용하여 두 데이터베이스를 모두 사용하는 것입니다.

"데이터베이스"는 다른 서버의 "카탈로그"또는 "스키마"와 같습니다. 사용 권한이 허용하는 것과 동일한 연결에서 다른 데이터베이스의 테이블을 사용할 수 있습니다. http://dev.mysql.com/doc/refman/5.0/en/commit.html

+0

입니다. 데이터베이스는 현재 두 개의 다른 서버 – salma

0

PHP 의해 제어되는 용액의 mysql 확장 및 숫자 PK 열을 가정하면 (NB는 것이다 :이

$objConnect = mysql_connect("localhost","root","root") or die(mysql_error()); 
$objDB = mysql_select_db("mydatabase"); 

//*** Start Transaction ***// 
mysql_query("BEGIN"); 

$query = "query1 comes here...";  

if(mysql_query($query)) { 
    // Commit Transaction 
    mysql_query("COMMIT"); 
    echo "Save Done."; 
} 
else { 
    // RollBack Transaction 
    mysql_query("ROLLBACK"); 
    echo "Error Save [".$strSQL."]"; 
} 
mysql_close($objConnect); 

그리고 확인 : PHP (안된)을 통해 거래 관리

예를 들어 두 개의 다른 서버가있는 경우와 같이 트랜잭션에서 랩핑 할 수없는 경우에만이 작업을 수행하십시오.

// Server connections 
$srcConn = mysql_connect('somewhere', 'user', 'pass'); 
$dstConn = mysql_connect('elsewhere', 'user', 'pass'); 

// Fetch data to move into an array 
$query = " 
    SELECT * 
    FROM `srcdb`.`table` 
"; 
$result = mysql_query($query, $srcConn); 
$data = array(); 
while ($row = mysql_fetch_assoc($result)) $data[] = $row; 
mysql_free_result($result); 

// Rows to handle per cycle 
// A larger number reduces the number queries, but you need to respect the MySQL 
// max_allowed_packet and it will also increase PHP memory usage 
$rowsPerCycle = 100; 

// Name of the primary key column 
$pKName = 'id'; 

// This will hold the column names in the right order, without the PK 
// You could make the list static if you know what it will be 
// You may also need to keep the PK value 
$colNames = array(); 
foreach ($data[0] as $colName => $val) { 
    if ($colName != $pKName) { // Remove me if you need to keep the PK values 
    $colNames[] = $colName; 
    } 
} 

// Keep looping while $data still has some elements 
while ($data) { 

    // Remove $rowsPerCycle elements from the beginning of $data 
    $cycleData = array_splice($data, 0, $rowsPerCycle); 

    // This array holds a list of the primary keys we are migrating 
    $cycleIds = array(); 

    // Build the base query 
    $query = " 
    INSERT INTO `destdb`.`table` 
     (`".implode("`, `", $colNames)."`) 
    VALUES 
    "; 

    // Loop the rows and append them to the query (after escaping them, of course...) 
    foreach ($cycleData as $row) { 
    $cycleIds[] = $row[$pKName]; 
    unset($row[$pKName]); // Remove me if you need to keep the PK values 
    $query .= "\n('" . implode("', '", array_map('mysql_real_escape_string', $row, array_fill(0, count($row), $dstConn)) . "'),"; 
    } 

    // Do the insert 
    if (!mysql_query($query, $dstConn)) { 
    // Handle insert errors here 
    trigger_error('MySQL Error: '.mysql_error($dstConn).'; Query: '.$query); 
    } 

    // Do the delete 
    $query = " 
    DELETE FROM `srcdb`.`table` 
    WHERE `$pKName` IN (".implode(', ', $cycleIds).") 
    "; 
    if (!mysql_query($query, $srcConn)) { 
    // Handle insert errors here 
    trigger_error('MySQL Error: '.mysql_error($srcConn).'; Query: '.$query); 
    } 

}