在数据库中使用默认值是不好的做法吗? [英] Is it bad practice to use default values in a database?

查看:70
本文介绍了在数据库中使用默认值是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

数据库中可以做的任何事情都是通常更健壮。如果处理默认值,如果没有在您的应用程序中指定值,并且有人设法通过您的应用程序连接到数据库(并相信我 - 用户尝试使用Excel或其他工具进行连接) - 然后数据库是广泛的,用户可能会插入拙劣的数据。



同样适用于引用完整性和检查约束。如果您尝试在数据库中有这么多的约束,那么相信你会更好,那么无论用户如何连接,如果他没有发送任何内容,您可以放置​​合理的默认值。



不要让应用程序处理你的支票 - 把它留给数据库!



加上它使你的SQL insert语句很多如果您不必指定所有明显的默认值,例如LastChangedOn日期列等的getdate()等,则更精简和更简单。


Shouldn't the code deal with default values instead of the database?

解决方案

Anything you can do in the database is typically more robust. If you handle default values which will be used if no value has been specified only in your app, and someone manages to connect to your database some other way than via your app (and believe me - the users WILL try to connect using Excel or other tools) - then the database is wide open and the user could potentially insert crappy data.

Same goes for referential integrity and check constraints. I believe you're better off if you try to have as many of those constraints on the database - then no matter how the user connects, if he doesn't send you anything, you can put in sensible defaults.

Don't let the application handle your checks - leave that to the database!

Plus it makes your SQL insert statement a lot leaner and meaner if you don't have to specify all the "obvious" defaults, like "getdate()" for a "LastChangedOn" date column etc.

这篇关于在数据库中使用默认值是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆