应该在数据库级别进行数据验证吗

应该在数据库级别进行数据验证吗

本文介绍了应该在数据库级别进行数据验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些存储过程来创建表和添加数据。其中一个字段是指示百分比的列。该值应为0-100。我开始思考,数据验证应该在哪里完成?一般来说,数据验证应该在哪里完成?

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 thinking, "where should the data validation for this be done? Where should data validation be done in general? Is it a case by case situation?"

对我来说,虽然今天我已经决定0-100是一个有效的百分比值,明天,我可能会决定任何正值是有效的。所以这可能是一个业务规则,不是吗?是否应该在数据库级实现业务规则?

It occurs to me that although today I've decided that 0-100 is a valid value for percentage, tomorrow, I might decide that any positive value is valid. So this could be a business rule, couldn't it? Should a business rule be implemented at the database level?

只是寻找指导,我们这里没有dba了。

Just looking for guidance, we don't have a dba here anymore.

推荐答案

一般来说,我会在多个地方进行验证:

Generally, I would do validations in multiple places:


  1. 在aspx页面上使用验证器

  2. 后面代码中的服务器端验证

验证作为最后的手段,因为数据库访问通常比上面讨论的两个验证更昂贵。

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.

这篇关于应该在数据库级别进行数据验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:51