Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store different version of data in a relational database?

Here is the example, for example, I have a table called Profile, and have different columns like:

id, firstName, secondName, address

typically, I create a profile, full in the information, and the database will become something like this:

 1| Ted | WONG | Hong Kong |

after that, I may update the data, like this

 1| Ted | WONG | US |

the data Hong Kong will be changed by an UPDATE SQL command, and I lose track of previous data. So, is there any way to let the database keep track of previous data and maintain the current information? Thanks.

like image 463
DNB5brims Avatar asked Sep 16 '25 10:09

DNB5brims


1 Answers

Add a version number column that you increase with each update but retain the same id. Then when retrieving the latest row for a given id you need to do a

where versionNo = (select max(versionNo) from table where id = <outerTableAliasOrVariable>.id)
like image 196
kaj Avatar answered Sep 18 '25 05:09

kaj