We use cookies to enhance your experience on the site
CodeWorlds

Emptiness in the catalog: IS NULL

Not every scroll has all its fields filled in. Sometimes an author's year of birth is unknown, and a borrowed book has not yet been returned. This emptiness - the absence of any value - is called NULL in a database.

NULL is not zero!

Important, @name:

NULL
is not the same as the number 0 nor the empty text
''
.
NULL
means "there is nothing here, the value is unknown". It is like an empty field on a scroll that nobody has filled in yet.

Why = NULL does not work

Because of this "unknown" nature, we cannot compare

NULL
with the ordinary
=
sign. This query will not work correctly:

1SELECT * FROM wypozyczenia
2WHERE data_zwrotu = NULL;

The Library does not know whether "unknown" equals "unknown", so it will always return nothing. That is why MySQL has special operators.

IS NULL and IS NOT NULL

To check whether a value is empty, we use IS NULL:

1SELECT * FROM wypozyczenia
2WHERE data_zwrotu IS NULL;

This means: "Loans that have not yet been returned" - because as long as the book is with the reader,

data_zwrotu
stays empty.

To find rows that do have a value, we use IS NOT NULL:

1SELECT * FROM wypozyczenia
2WHERE data_zwrotu IS NOT NULL;

This means: "Loans already returned" - because

data_zwrotu
has been filled in.

Remember a simple rule: with NULL we never use

=
, only
IS NULL
or
IS NOT NULL
. This is one of the most common mistakes of beginning scribes - you will already avoid it!

Go to CodeWorlds