简体   繁体   English

在MySQL数据库中存储空字符串与NULL?

[英]Storing empty strings vs NULLs in MySQL database?

I have a PHP/MySQL application that stores "blank" values in some cases as '' (empty strings) and in other cases as NULL s. 我有一个PHP / MySQL应用程序,在某些情况下将“空白”值存储为'' (空字符串),在其他情况下存储为NULL

Having this mixed format certainly causes a problem when comparing, so I'm wondering which one is the better storage mechanism? 拥有这种混合格式肯定会在比较时出现问题,所以我想知道哪一个是更好的存储机制? '' or NULL ? ''还是NULL

While this is confusing, actually you should store null for a number of reasons : 虽然这很令人困惑,但实际上你应该存储null有很多原因:

  1. Checking against null is usually faster than checking an empty string in most databases 检查null通常比检查大多数数据库中的空字符串更快
  2. Null commonly means "i don't know", empty string means "I know : it's empty". Null通常表示“我不知道”,空字符串表示“我知道:它是空的”。 It gives you better semantics. 它为您提供更好的语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM