mySQL: get hash value for each row?

前端 未结 5 1831
长发绾君心
长发绾君心 2021-01-04 07:00

Currently I\'m manually creating a string where I concatenate all the values in each row in my table. I\'m hashing this string for each row to get a hash value for the curre

5条回答
  •  爱一瞬间的悲伤
    2021-01-04 07:43

    Late to the party, but...

    You could add an updated field that gets updated whenever the row changes.
    Then you only have to keep track of the timestamps, that show like this

    updated: 2017-12-30 17:51:19 
    

    This way not only you know if a row changed, but you also know when was the last update.
    MySQL command:

    ALTER TABLE mytable ADD `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    ON UPDATE CURRENT_TIMESTAMP;
    

    The default precision is a second (that should be enough, if you only check from time to time if a row changed). But you could set a fraction of a second precision, like microseconds, if necessary

    ALTER TABLE mytable ADD `updated` timestamp(6) NOT NULL
    DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6);
    

    The timestamp will then show like

    updated: 2017-12-30 17:51:19.123456 
    

    Link to the documentation, this is for 8.0 but I've been using that on 5.7 successfully.

提交回复
热议问题