Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is better use an empty value as a '' or as NULL?

Database schema

I have this fields:

  • title (string)
  • subtitle
  • description (string)

Is better set the default value as an empty string '' or a NULL?

For better reading/writing and size-storage performance

like image 376
sparkle Avatar asked Nov 24 '12 14:11

sparkle


People also ask

Is an empty value the same as NULL?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

Why would you use a NULL as a data value?

NULL values are used to indicate that you could have a value, but you don't know what that value should be yet. They are placeholders until you finally collect the data needed to fill the table field with a real value. You should never confuse NULL values for zeros or blank strings.

Why should NULL values be avoided?

They should be avoided to avoid the complexity in select & update queries and also because columns which have constraints like primary or foreign key constraints cannot contain a NULL value.

When should NOT NULL be used?

A NOT NULL constraint in SQL is used to prevent inserting NULL values into the specified column, considering it as a not accepted value for that column. This means that you should provide a valid SQL NOT NULL value to that column in the INSERT or UPDATE statements, as the column will always contain data.


1 Answers

The usual contract is:

  • NULL means "no information available".
  • '' means "there is information available. It's just empty."

Beyond this point there is much philosophical discussion since the invention of NULL in any language, not just SQL.

The only technical point here is: In PostgreSQL NULL can be stored more efficiently than a string of length zero. If that really matters in your case ... we cannot know.

like image 194
A.H. Avatar answered Nov 03 '22 20:11

A.H.