Should data validation be done at the database level?

后端 未结 12 1500
半阙折子戏
半阙折子戏 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:36

    First ideal: have a "gatekeeper" so that your data's consistency does not depend upon each developer applying the same rules. Simple validation such as range validation may reasonably be implemented in the DB. If it changes at least you have somewhere to put.

    Trouble is the "business rules" tend to get much more complex. It can be useful to offload processing to the application tier where OO languages can be better for managing complex logic.

    The trick then is to structure the app tier so that the gatekeeper is clear and unduplicated.

    In a small organisation (no DBA ergo, small?) I would tend to put the business rules where you have strong development expertise.

    This does not exclude doing initial validation in higher levels, for example you might validate all the way up in the UI to help the user get it right, but you don't depend upon that initial validation - you still have the gatekeeper.

提交回复
热议问题