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