简体   繁体   中英

Updating the MySQL database with PHP only if the value changed

I have a big MySQL database and I'm extracting the important bits into a smaller MySQL database. The values in the big database are constantly changing. The smaller database should also be changing dynamically.

I am able to populate the smaller database only when it's empty, using this code:

$SQL_INSERT="INSERT LOW_PRIORITY IGNORE INTO oddsnavi_baby.calc (one , two, three)
VALUES ('$one', '$two' , '$three')";

mysql_db_query($database_baby, $SQL_INSERT) or die("Failed Query of " . $SQL_INSERT);

I'd like the values which are changed to be updated. For example, if $three is different than the existing value in 'three' in the small database, then 'three' is the only updated value in the row. How to do that?

EDIT: I'm getting an error with the following code. What's wrong with my syntax?

$SQL_INSERT="INSERT LOW_PRIORITY IGNORE INTO oddsnavi_baby.calc (one , two, three)
VALUES ('$one', '$two' , '$three')
ON DUPLICATE KEY UPDATE oddsnavi_baby.calc SET two = '$two' , three = '$three'
WHERE one = '$one'";

If both databases are hosted on the same physical hardware, you might want to consider implementing an UPDATE trigger on one of more tables in your "big" database which would update the values in "small" database.

Here is an article with brief overview on using triggers in MySQL: http://www.roseindia.net/mysql/mysql5/triggers.shtml

I can think of two ways you can do this:

  1. the software that changes the "big" database, alerts the software that manages the small database, and then the managing software changes the appropriate fields.

  2. I think you can create a view between the two databases, never done something like that though, no idea even if its possible, but why not, you can make foreign keys between two (or more) databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM