在数据库中使用默认值是不好的做法吗?

编程入门 行业动态 更新时间:2024-10-26 00:24:41
本文介绍了在数据库中使用默认值是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

解决方案

数据库中可以做的任何事情都是通常更健壮。如果处理默认值,如果没有在您的应用程序中指定值,并且有人设法通过您的应用程序连接到数据库(并相信我 - 用户将尝试使用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.

更多推荐

在数据库中使用默认值是不好的做法吗?

本文发布于:2023-10-18 12:20:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   默认值   做法   不好

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!