저자와 도서로 두 개의 테이블이 있습니다. 저자 테이블의 데이터를 책 테이블에 삽입하고 싶습니다. 삽입이 성공적이면 작성자 테이블의 데이터가 삭제되고 성공하지 못하면 작성자 테이블에서 데이터가 삭제되지 않습니다. 롤백을 사용하고 싶지만 여기서는 작성자와 서적이 다른 서버에있는 두 개의 서로 다른 데이터베이스입니다. php와 mysql에 모든 빌딩 함수가 존재합니다.롤백의 PHP-mysql 대체 기능
1
A
답변
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);
}
}
관련 문제
- 1. 자바 스크립트와 PHPMySQL
- 2. 롤백의 영향을받는 행 수
- 3. 중단과 롤백의 차이점은 무엇입니까?
- 4. Aptana Studio 대체 기능
- 5. 대체 키 기능 교환
- 6. "Simple"텍스트 대체 기능
- 7. Repository.SaveOrUpdate(), 예외시 실행되는 롤백의 확실성?
- 8. 이미지를 업로드하는 동안 phpmysql 업데이트가 설정되었습니다.
- 9. 팝업 창 대체 (동일한 기능)
- 10. Devise : authenticate_user의 사용자 지정 대체 기능
- 11. HTML5의 history.pushState() 및 window.onpopstate에 대한 대체 기능
- 12. C++ 용 부스트 대체 0x 라이브러리 기능
- 13. SQL Server에서 트랜잭션 롤백의 영향을받지 않는 감사 테이블 만들기
- 14. XSLT : XML : 기능/기능
- 15. GCC의 ICE는 - 대체 기능 구문 가변 템플릿 튜플은
- 16. std :: set (다른 세트로 요소를 이동할 수있는 기능)로 대체
- 17. 자바 스크립트 - 캐치 구문 에러 및 실행 대체 기능
- 18. Guard의 기능 사용이 허용되지 않습니다. 대체 구현을위한 제안
- 19. ASP.Net/C# .Net - 템플릿을 사용한 PDF 라이브러리/인보이스 대체 기능
- 20. JPA : DDL 기능 만 @UniqueConstraint를 통한 대체 키?
- 21. Oracle DBA 역할 및 특정 기능 (및 가능한 대체)
- 22. Umbraco에서 대체/대체 5
- 23. 대체 개체 대체 코드 #
- 24. H1 이미지 대체 대체
- 25. 대체
- 26. 대체
- 27. 대체
- 28. 대체
- 29. unix2dos를 기능
- 30. IE6/7의 대체 속성 대체
두 데이터베이스가 모두 InnoDB입니까? – biziclop
지금까지 어떤 코드를 작성 했습니까? 우리가 당신을 도울 수 있도록 코드를 보여주십시오. –
yes.two 데이터베이스는 모두 InnoDB – salma