数据库表是否具有默认值? [英] Should a database table have default values?

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

问题描述

我正在与开发人员讨论一个表使用默认值的问题。

I was having a discussion with a developer at work on the issue of should a table use default values. Is there a hard and fast rule on this or is it a gray area in best practices?

推荐答案

我的规则:如果有很多记录将使用该默认值(至少在最初),然后我喜欢使用它作为默认值。例如,在线商店中的产品的图像表可以具有 images / NoPictureYet.png 的默认路径。最终,这些将被替换,但对于批量加载的数据,其中图片根本不存在(也许大多数他们永远不会!),默认是有意义的(至少对我来说)。

My rule: if many records will use that default (at least initially) then I like to use it as a default. For example, an image table for products in an online store might have a default path of images/NoPictureYet.png. Eventually, those will get replaced, but for batch loads of data where the pictures simply don't exist yet (and maybe most of them never will!), a default makes sense (to me, at least).

如果没有合理的默认值(例如客户数据库中的名字 - 我不希望MY名称默认为FirstName),那么我使它不可为空,没有默认 - 这是应用程序的责任,以确保输入正确的值。

If there is no sensible default (such as "first name" in a customer database - I don't want MY name defaulted to "FirstName"), then I make it non-nullable and no default - it's the application's responsibility to ensure that a correct value gets entered.

但没有严格和快速的规则。这一切都有所不同);

But no hard and fast rules on this. It all varies a little ;)

这篇关于数据库表是否具有默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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