Null Values in DBMS
Null values in DBMS
- Special value that is supported by SQL is called as
nullwhich is used to represent values of attributes that are unknown or do not apply for that particular row
- For example age of a particular student is not available in the age column of student table then it is represented as null but not as zero
- It is important to know that null values is always different from zero value
- A null value is used to represent the following different interpretations
- Value unknown (value exists but is not known)
- Value not available (exists but is purposely hidden)
- Attribute not applicable (undefined for that row)
- SQL provides special operators and functions to deal with data involving null values
Consider the sample table ‘emp’
IS NULL operator
All operations upon null values present in the table must be done using this ‘is null’ operator .we cannot compare null value using the assignment operator
select * from emp where comm is null
4 rows selected.
The details of those employees whose commission value is Null are displayed
IS NOT NULL
select * from emp where comm is not null;
3 rows selected.
Details of all those employees whose Commission value is not null value are displayed
NOT NULL Constraint
- Not all constraints prevents a column to contain null values
- Once not null is applied to a particular column, you cannot enter null values to that column and restricted to maintain only some proper value other than null
- A not-null constraint cannot be applied at table level
CREATE TABLE STUDENT
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
- In the above example, we have applied not null on three columns ID, name and age which means whenever a record is entered using insert statement all three columns should contain a value other than null
- We have two other columns address and salary, where not null is not applied which means that you can leave the row as empty or use null value while inserting the record into the table
NVL() NULL Function
- Using NVL function you can substitute a value in the place of NULL values.
- The substituted value then temporarily replaces the NULL values in your calculations or expression. Remember that the substituted value only replaces the NULL value temporarily for the session and does not affect the value stored in the table.
- Here is the syntax of NVL function.
NVL (exp, replacement-exp)
- As you can see NVL function takes two parameters exp and replacement exp. First parameter exp can be a column name of a table or an arithmetic expression and the second parameter replacement expression will be the value which you want to substitute when a NULL value is encountered.
- Always remember the data type of both the parameters must match otherwise the compiler will raise an error.
SELECT NVL (comm, 500) FROM employees
- On execution all the null values in the result set will get replaced by 500.
- Similarly we can use NVL null function while performing arithmetic expression.
- Again let’s take the same arithmetic expression which we used in the previous query where we added 100 to the values of commission column.
SELECT NVL(comm,100), NVL(comm,100)+100 FROM employees WHERE salary>1000;