Should data validation be done at the database level?

后端 未结 12 1506
半阙折子戏
半阙折子戏 2021-02-07 02:45

I am writing some stored procedures to create tables and add data. One of the fields is a column that indicates percentage. The value there should be 0-100. I started thinkin

12条回答
  •  爱一瞬间的悲伤
    2021-02-07 03:33

    If you percentage is always 'part divided by whole' (and you don't save part and whole values elsewhere), then checking its value against [0-100] is appropriate at db level. Additional constraints should be applied at other levels.

    If your percentage means some kind of growth, then it may have any kind of values and should not be checked at db level.

    It is case by case situation. Usually you should check at db level only constraints, which can never change or have natural limits (like first example).

提交回复
热议问题