Should I store a calculate value in my database along with the variables?

前端 未结 5 802
囚心锁ツ
囚心锁ツ 2020-12-31 07:00

In all the applications I have made where a database is used I typically store the calculated value along with the variables needed to calculate that value. For example, if

5条回答
  •  傲寒
    傲寒 (楼主)
    2020-12-31 07:36

    In my experience, the most common thing to do is to a) store the calculated value, b) without any CHECK constraints in the database that would guarantee that the value is correct.

    The right thing to do is either

    • don't store the result of the calculation
    • store the calculated value in a column that's validated with a CHECK constraint.

    MySQL doesn't support CHECK constraints. So your options are

    • don't store the result of the calculation
    • switch to a dbms that supports CHECK constraints, such as PostgreSQL.

提交回复
热议问题