Should data validation be done at the database level?

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

    I think basic data validation like you described makes sure that the data entered is correct. The applications should be validating data, but it doesn't hurt to have the data validated again on the database. Especially if there is more than one way to access the database.

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

    If you have a good data access tier, it almost doesn't matter which approach you take.

    That said, a database constraint is a lot harder to bypass (intentionally or accidentally) than an application-layer constraint.

    In my work, I keep the business logic and constraints as close to the database as I can, ensuring that there are fewer potential points of failure. Different constraints are enforced at different layers, depending on the nature of the constraint, but everything that can be in the database, is in the database.

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

    I worked for a government agency, and we had a -ton- of business rules. I was one of the DBA's, and we implemented a large number of the business rules in the database; however, we had to keep them pretty simple to avoid Oracle's dreaded 'mutating table' error. Things get complicated very quickly if you want to use triggers to implement business rules which span several tables.

    Our decision was to implement business rules in the database where we could because data was coming in through the application -and- through data migration scripts. Keeping the business rules only in the application wouldn't do much good when data needed to be migrated in to the new database.

    I'd suggest implementing business rules in the application for the most part, unless you have data being modified elsewhere than in the application. It can be easier to maintain and modify your business rules that way.

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

    Richard is right: the question is subjective the way it has been asked here.

    Another take is: what are the schools of thought on this? Do they vary by sector or technology?

    I've been doing Ruby on Rails for a bit now, and there, even relationships between records (one-to-many etc.) are NOT respected on the DB level, not to mention cascade deleting and all that stuff. Neither are any kind of limits aside from basic data types, which allow the DB to do its work. Your percentage thing is not handled on the DB level but rather at the Data Model level.

    So I think that one of the trends that we're seeing lately is to give more power to the app level. You MUST check the data coming in to your server (so somewhere in the presentation level) and you MIGHT check it on the client and you MIGHT check again in the business layer of your app. Why would you want to check it again at the database level?

    However: the darndest things do happen and sometimes the DB gets values that are "impossible" reading the business-layer's code. So if you're managing, say, financial data, I'd say to put in every single constraint possible at every level. What do people from different sectors do?

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

    In general, I would think that the closer the validation is to the data, the better.

    This way, if you ever need to rewrite a top level application or you have a second application doing data access, you don't have two copies of the (potentially different) code operating on the same data.

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

    It would depend on how you are interacting with the database, IMO. For example, if the only way to the database is through your application, then just do the validation there.

    If you are going to allow other applications to update the database, then you may want to put the validation in the database, so that no matter how the data gets in there it gets validated at the lowest level.

    But, validation should go on at various levels, to give the user the quickest opportunity possible to know that there is a problem.

    You didn't mention which version of SQL Server, but you can look at user defined datatypes and see if that would help you out, as you can just centralize the validation.

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