Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check whether a row has been updated in SQL

I have an update statement which updates a table. And there is a column that records the last modified time. If data in a specific row has not been changed, I don't want to change the last modified date time.

What is the best way to check whether an update statement will change the row of data or not.

Thanks,

like image 866
J.W. Avatar asked Mar 30 '09 17:03

J.W.


2 Answers

Check the old vs. new data in your code instead of doing it in a query.

No need to bother the DB layer unnecessarily if data didn't change at all.

In short, if data didn't change, don't send the UPDATE statement.

like image 133
Seb Avatar answered Sep 21 '22 20:09

Seb


One way is to start a transaction, select the contents of the row and compare it to what you're going to update it to. If they don't match, then do the update and end the transaction. If they match, rollback the transaction.

like image 43
brian-brazil Avatar answered Sep 24 '22 20:09

brian-brazil