开发者

MySQL Integer 0 vs NULL

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, because I come from a Java world were (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 开发者_C百科the "more correct" option.


Using NULL is preferable, for two reasons:

  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.


Declare columns to be NOT NULL if possible. It makes SQL operations faster, by enabling better use of indexes and eliminating overhead for testing whether each value is NULL. You also save some storage space, one bit per column. If you really need NULL values in your tables, use them. Just avoid the default setting that allows NULL values in every column.

MySQL - optimizing data size


using NULL for "no value" is literally correct. 0 is a value for an integer, therefore it has meaning. NULL otoh literally means there is nothing, so there is no value.

Performance would likely be irrelevant, but using NULL may well be faster somewhat if you learn to code using NULL correctly.


In your parent_id example 0 is perfectly valid, because it stands for 'root'. In most cases, NULL is a better choice for 'no value' logically.

It has no performance impact that I know of, though.


You shouldn't expect to see any real life performance difference from this


UNIQUE( id1, id2 ) won't work with null values because it would allow, for example 1, null twice

on the other hand if you use 0, JOIN atable ON this.extID = atable.ID the join will be executed (resulting in no rows joined) whereas NULL will just be ignored

Anyway I suggest to always use "empty values" (like 0 or empty string) instead of NULL unless the empty value has a different meaning from NULL

and I also modify queries like so: JOIN atable ON this.extID = atable.id AND extID > 0 which prevents to execute the useless join


I think 0 can be used instead of NULL if you don't actually expect 0 to be used as a value.

That is for example your column is a foreign key. Since foreign keys don't normally start with 0 but instead start with 1, it means you wouldn't expect the 0 to be used as a value.

You can then use the 0 to denote the 'No' value state. Using it in joins would not match any columns on the other table. Thus, having the same effect as NULL.

But if you have a column where the 0 actually has a meaning. Like for example a quantity field. And apart from that, you also need to express and empty value. For example, to denote that the quantity hasn't been inputted yet. Then you need a NULL for that.

Hope that makes sense.


0 is still a valid value for an integer column. Hence you have to use NULL and allow null on that column.

Also if you are using integer column for only positive numbers, then you can use -1 for no value.

In your example of parent_id reference to use 0, it is fine until you make sure that there are no reference ids starting with id 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜