J
J
J. Snow2020-06-21 20:13:41
MySQL
J. Snow, 2020-06-21 20:13:41

What value should be stored in the database for empty text fields - NULL or an empty string?

Hi all!

Let's say we have a regular TODO application.
Each body has a title ( title VARCHAR ) and a description ( description TEXT ).
Description is not a required field and will most often be left blank.
Everything is stored in a MySQL-like database.

Question:
What to store in the description field if it is empty?
NULL or empty string?

Accordingly, you need to decide whether to make it nullable or not.

Here are the advantages I see if NOT making the field nullabe, and just storing an empty string:

+ 1 bit less disk space per entry.
(at least in MySQL + InnoDB; I wonder what about other databases?)

+ Less problems in the code. No null checks needed.
(although in modern languages ​​this is almost not a problem anymore)


Cons :
– When transferring data to the client in JSON format, there will be more traffic, since you will also have to transfer:
{..., description: ""}
(of course, you can not transfer empty fields, but these are unnecessary body movements in the code, as on the server , and on the client.)

– MySQL stores BLOBs and TEXTs in a separate object in its storage. This is not necessarily a separate file, but it appears to be a separate space in the table file. Not sure exactly. Correct me. But if this is true, then the fetches will probably be slower.
(It is also interesting how other databases store TEXT - PostgreSQL and AWS Aurora.)

- There will probably be slower fetches if you need to find all the empty ones:
( WHERE description = '' ) vs ( WHERE description IS NULL)


Question 2:
Will there be a difference in terms of organizing a full-text search on the description field?

Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Developer, 2020-06-21
@samodum

Null

L
Lazy @BojackHorseman MySQL, 2020-06-21
Tag

what logic requires.
NULL and empty string are different values

V
Vladimir Korotenko, 2020-06-21
@firedragon

You can always mess up. Act logically. If the field has no value, then its value is null. That is, at any time you can check for zero and display a message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question