Zero in sql

In SQL Null is both a value and a keyword. Let's look at the NULL value first −
Null as value
Simply put, NULL is just a placeholder for non-existent data. When performing insert operations on tables, these will be times when some field values ​​will not be available.
To meet the requirements of true relational database management systems, MySQL uses NULL as a placeholder for values ​​that have not been provided. The screenshot below shows what NULL values ​​look like in the database.
Let's now look at some of the basics of NULL before we delve into the discussion.
- NULL is not a data type , which means that it is not recognized as "int", "date", or any other defined data type.
- Arithmetic operations involving NULL always return NULL, for example, 69 + NULL = NULL.
- All aggregate functions only affect rows that do not have NULL values .
Let's now demonstrate how the count function handles null values. Let's see the current contents of the member table
SELECT * FROM `members`;
Executing the above script gives us the following results
membership number | full_names | gender | date_of_birth | physical address | postal address | contact_number | |
---|---|---|---|---|---|---|---|
one | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | janetjones@yagoo.cm |
2 | Janet Smith Jones | Female | 23-06-1980 | Melrose 123 | NULL | NULL | <a href='mailto:jj@fstreet.com'>jj@fstre et.com</a> |
3 | Robert Phil | Male | 12-07-1989 | 3rd Street 34 | NULL | 12345 | <a href='mailto:rm@tstreet.com'>rm@tstre et.com</a> |
4 | Gloria Williams | Female | 14-02-1984 | 2nd Street 23 | NULL | NULL | NULL |
5 | Leonard Hofstadter | Male | NULL | Woodcrest | NULL | 845738767 | NULL |
6 | Sheldon Cooper | Male | NULL | Woodcrest | NULL | 976736763 | NULL |
7 | Rajesh Koothrappali | Male | NULL | Woodcrest | NULL | 938867763 | NULL |
eight | Leslie Winkle | Male | 14-02-1984 | Woodcrest | NULL | 987636553 | NULL |
9 | Howard Wolowitz | Male | 24-08-1981 | south park | PO Box 4563 | 987786553 | lwolowitz[at]email.me |
Let's count all members who have updated their contact_number
SELECT COUNT(contact_number) FROM `members`;
Running the above query gives us the following results.
COUNT(contact_number) |
---|
7 |
Note: values ​​that are NULL were not included
What is not?
The logical NOT operator is used to test logical conditions and returns true if the condition is false. The NOT operator returns false if the tested condition is true
State |
NOT Operator Result |
Truth |
Lie |
Lie |
Truth |
There will be times when we will need to perform calculations on the result set of a query and return values. Performing any arithmetic operations on columns that are NULL returns null results. To avoid situations like this, we can use the NOT NULL clause to limit the results our data works with.
NOT NULL values
Let's assume that we want to create a table with certain fields that must always be provided with values ​​when new rows are inserted into the table. We can use the NOT NULL clause for a given field when creating a table.
The following example creates a new table containing employee data. Employee number must always be specified
CREATE TABLE `employees`( employee_number int NOT NULL, full_names varchar(255) , gender varchar(6) );
Let's now try inserting a new record without specifying the employee's name and see what happens.
INSERT INTO `employees` (full_names,gender) VALUES ('Steve Jobs', 'Male');
Running the above script in MySQL Workbench gives the following error -
NULL keywords
NULL can also be used as a keyword when performing boolean operations on values ​​that include NULL. The keyword "IS / NOT" is used in conjunction with the word NULL for such purposes. The basic syntax when null is used as a keyword is as follows
`comlumn_name' IS NULL `comlumn_name' NOT NULL
HERE
- "IS NULL" is a keyword that performs a boolean comparison. It returns true if the provided value is NULL and false if the provided value is not NULL.
- "NOT NULL" is a keyword that performs a boolean comparison. It returns true if the provided value is not NULL and false if the provided value is null.
Now let's look at a practical example that uses the NOT NULL keyword to exclude all column values ​​that have null values.
Continuing with the above example, let's say we want data on members whose contact number is not null. We can query like
SELECT * FROM `members` WHERE contact_number IS NOT NULL;
Running the above query only returns records where the contact number is not null.
Let's say we want member records where the contact number is null. We can use the following query
SELECT * FROM `members` WHERE contact_number IS NULL;
Running the above query gives the details of the member whose contact number is INVALID
membership number | full_names | gender | date_of_birth | physical_address | postal_address | contact_number | |
---|---|---|---|---|---|---|---|
one | Janet Jones | Female | 21-07-1980 | First Street Plot No 4 | Private Bag | 0759 253 542 | <a href='mailto:janetjones@yagoo ;.cm'>janetjones@yagoo.cm</a> |
3 | Robert Phil | Male | 12-07-1989 | 3rd Street 34 | NULL | 12345 | <a href='mailto:rm@tstreet.com'>rm@tstre et.com</a> |
5 | Leonard Hofstadter | Male | NULL | Woodcrest | NULL | 845738767 | NULL |
6 | Sheldon Cooper | Male | NULL | Woodcrest | NULL | 976736763 | NULL |
7 | Rajesh Koothrappali | Male | NULL | Woodcrest | NULL | 938867763 | NULL |
eight | Leslie Winkle | Male | 14-02-1984 | Woodcrest | NULL | 987636553 | NULL |
9 | Howard Wolowitz | Male | 24-08-1981 | south park | PO Box 4563 | 987786553 | lwolowitz[at]email.me |
Zero value comparison s
Three-valued logic - performing logical operations in conditions that use NULL can return "Unknown", "True", or "False".
For example, using the keyword "IS NULL" when performing comparison operations using NULL can return true or false . Using other comparison operators returns "Unknown" (NULL).
Suppose you are comparing number five with 5
SELECT5=5;
Query result is 1 which means TRUE
5 = 5 |
---|
one |
Let's do the same operation with NULL
SELECT NULL = NULL;
NULL = NULL |
---|
NULL |
Let's look at another example
SELECT 5 > 5;
5 > 5 |
---|
0 |
Query result is 0, which means FALSE
Let's look at the same example using NULL
SELECT NULL > NULL;
NULL > NULL |
---|
NULL |
Allows the use of the IS NULL keyword
SELECT 5 IS NULL;
5 NULL |
---|
0 |
Query result is 0 which is FALSE
SELECT NULL IS NULL;
NULL IS NULL |
---|
one |
Query result - 1, TRUE
Summary
- NULL is a value place holder for optional table fields.
- MySQL treats the NULL value differently from other data types. The NULL values ​​when used in a condition evaluates to the false Boolean value.
- The NOT logical operate is used to test for Boolean values ​​and evaluates to true if the Boolean value is false and false if the Boolean value is true.
- The NOT NULL clause is used to eliminate NULL values ​​from a result set
- Performing arithmetic operations on NULL values ​​always returns NULL results.
- The comparison operators such as [, =, etc.] cannot be used to compare NULL values.