SQL NULL Vs EMPTY Statement
The major distinction between NULL
refers to nothing, whereas EMPTY
refers to a single string of zero length.
A string is a collection of characters. "Programming," for example, is a String .
Strings are supported by the Java programming language and are considered as objects.
The String
class is unchangeable.
As a result, once an item is created, it cannot be modified. Strings
can be null or empty at times.
When you assign null to a String
variable, it means the variable isn't actually referring to any memory location in the heap.
The assignment of an empty String
to a String
variable, on the other hand, implies that the reference variable is pointing to a memory address of a String
of zero length .
SQL NULL Statement
Nothing is referred to as null. NULL indicates that we have no idea what the value is; it may or may not exist; we just don't know. Take note of the following two statements.
The variable s1
is a String
. It's been given the value of null. As a result, the JVM will s1
will be null.
String s1 = null;
this is also equivalent to String s1
; When printing function does not count the length of a string.
Example: Let's start by looking at an example that shows how to use the IS NULL
condition in a SELECT
statement.
In this example, we have a table called Country
with the following data:
ID | Code | Name | Continent |
---|---|---|---|
1 | TJK | Tajikistan | Asia |
2 | SYR | Syria | Asia |
3 | MYS | Malaysia | Asia |
4 | FRA | France |
Enter the following SQL statement:
//Mysql,Sql-server,Sqlite,Ms accessSELECT *
FROM Country
WHERE Continent IS NULL;
When testing for a NULL value, IS NULL
is the recommended comparison operator to use in SQL. There will be 1 record selected.
Output: This example will return all records from the Country table where the Continent contains a NULL
value.
ID | Code | Name | Continent |
---|---|---|---|
4 | FRA | France |
SQL EMPTY
Empty-String denotes that we are aware of the value and that it is zero. The term "empty" refers to a string that has no length. Please see the code below.
The variable s2
is a String
. It is given the value "" as an empty string.
As a result, the JVM will set aside memory for an empty string. That empty string will be referred to by s2
.
It will print 0 when the length of the String s2
is printed.
This is because s1
refers to a String
, but there are no characters in it to count the length.
SQL DIFFERENCE BETWEEN NULL AND EMPTY FUNCTION
In below table we will discusse the distinguish between null & empty function:-
Column 1 | Column 2 |
---|---|
NULL is a phrase used to denote that an object refers to nothing on the heap. | EMPTY is a word that denotes that an object refers to a single zero-length string in the heap. |
A null pointer exception will be thrown if the length of the null string is printed. | If you print the length of an empty string, you'll get a zero. |
Null can refer to an unknown value or a value that does not exist. | An empty or blank string is a value that is just that: it is empty. |
Null can be used to represent a string, an integer, a date, or any other field in a database. | For string fields, the value empty is used. |
Because NULL is an unknown value, no memory is allocated to a field with NULL. | EMPTY fields have no value and are assigned memory space. |
If you don't allow empty strings but don't require the user to enter a value, NULL is a good option. | If you need a value but it can be empty, NOT NULL is appropriate, and a value of "" is appropriate. |