Should data validation be done at the database level?

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

    Generally, I would do validations in multiple places:

    1. Client side using validators on the aspx page
    2. Server side validations in the code behind

    I use database validations as a last resort because database trips are generally more expensive than the two validations discussed above.

    I'm definitely not saying "don't put validations in the database", but I would say, don't let that be the only place you put validations.

    If your data is consumed by multiple applications, then the most appropriate place would be the middle tier that is (should be) consumed by the multiple apps.

    What you are asking in terms of business rules, takes on a completely different dimension when you start thinking of your entire application in terms of business rules. If the question of validations is small enough, do it in individual places rather than build a centralized business rules system. If it is a rather large system, them you can look into a business rules engine for this.

提交回复
热议问题