MySQL Integer 0 vs NULL [英] MySQL Integer 0 vs NULL

查看:1204
本文介绍了MySQL Integer 0 vs NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用整数列时,最好使用0或NULL表示无值。例如,如果表具有parent_id字段,并且特定条目没有父,则您将使用0或NULL。我在过去总是使用0,这是因为我来自一个Java世界,其中(之前的1.5)整数总是必须有一个值。我主要是关于性能问题,我不太担心哪个是更正确选项。

When using integer columns is it better to have 0 or NULL to indicate no value. For example, if a table had a parent_id field and a particular entry had no parent, would you use 0 or NULL. I have in the past always used 0, this is because I come from a Java world where (prior to 1.5) integers always had to have a value. I am asking mainly in relation to performance, I am not too worried about which is the "more correct" option.

推荐答案

NULL 是更好的,因为两个原因:

Using NULL is preferable, for two reasons:


  1. NULL 用于表示该字段没有值,这正是您想要建模的。

  2. 如果您决定添加一些参照完整性约束未来,您将不得不使用 NULL

  1. NULL is used to mean that the field has no value, which is exactly what you're trying to model.
  2. If you decide to add some referential integrity constraints in the future, you will have to use NULL.

这篇关于MySQL Integer 0 vs NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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