Should data validation be done at the database level?

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

    In a perfect world the only thing talking (updating, deleting, inserting) to your database would be your business api. In the perfect world databae level constraints are a waste of time, your data would already have been validated and cross checked in your business api. In the real world we get cowboys taking shortcuts and other people writing directly to the database. In this case some constraints on the database are well worth the effort. However if you have people not using your api to read/write you have to consider where you went wrong in your api design.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 03:37

    You can reasonable restrict the database so that the data always makes sense. A database will support multiple applications using the same data so some restrictions make sense.

    I think the only real cost in doing so would be time. I think such restrictions aren't a big deal unless you are doing something crazy. And, you can change the rules later if needed (although some changes are obviously harder than others)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 03:38

    One can make a case for:

    • In the database implement enough to ensure overall data integrity (e.g. in SO this could be every question/answer has at least one revision).

    • In the boundary between presentation and business logic layer ensure the data makes sense for the business logic (e.g. in SO ensuring markup doesn't contain dangerous tags)

    But one can easily make a case for different places in the application layers for every case. Overall philosophy of what the database is there for can affect this (e.g. is the database part of the application as a whole, or is it a shared data repository for many clients).

    The only thing I try to avoid is using Triggers in the database, while they can solve legacy problems (if you cannot change the clients...) they are a case of the Action at a Distance anti-pattern.

    0 讨论(0)
提交回复
热议问题