2. • In SQL, you can disallow null values in a
column by using the NOT NULL constraint
when creating or altering a table. This
constraint ensures that every row in the table
must have a value for that column and that
value cannot be NULL.
3. Here's an example of creating a table
with the NOT NULL constraint:
• CREATE TABLE Employees (
• EmployeeID INT PRIMARY KEY,
• FirstName VARCHAR(50) NOT NULL,
• LastName VARCHAR(50) NOT NULL,
• Department VARCHAR(50)
• );
• In this example, both FirstName and LastName
columns are specified as NOT NULL, meaning that
whenever a new record is inserted into the
Employees table, these columns must have a
value, and NULL values are not allowed.
4. • If you try to insert a record without providing
a value for a column with the NOT NULL
constraint, you'll receive an error. For
instance:
• INSERT INTO Employees (EmployeeID,
FirstName, LastName) VALUES (1, 'John',
NULL);
• This would result in an error because
LastName is declared as NOT NULL, and a
NULL value is being attempted to be inserted.
5. • To allow NULL values again, you would need to
alter the table and remove the NOT NULL
constraint:
• ALTER TABLE Employees
• ALTER COLUMN LastName VARCHAR(50) NULL;
• Now, LastName column will allow NULL values.
• In summary, using the NOT NULL constraint
ensures data integrity by disallowing NULL values
in the specified column, thereby requiring that
each row has a valid value for that column.
6. Comparisons Using Null Values
• When performing comparisons involving null
values in SQL, it's essential to understand how
null values behave. Null represents a missing
or unknown value in SQL. Comparisons
involving null values often yield results that
may not be immediately intuitive due to the
unknown nature of null.
7. • Here are some common behaviors when comparing
null values in SQL:
1. Equality Comparisons: Comparing a null value to
another value, including another null value, using the
equality operator (=) results in an unknown or null
outcome. This is because the value of the null is
unknown.
SELECT * FROM Employees WHERE FirstName =
NULL;
This query doesn't return any rows, even if there are
records with a null value for the FirstName column.
Instead, you should use the IS NULL or IS NOT NULL
operators to check for null values:
SELECT * FROM Employees WHERE FirstName IS
NULL;
8. 2. Inequality Comparisons: Similarly, comparing
a null value to another value using inequality
operators (<>, !=, >, <, >=, <=) also results in
an unknown outcome.
• SELECT * FROM Employees WHERE FirstName
<> NULL;
• This query also doesn't return any rows.
Instead, you should use the IS NULL or IS NOT
NULL operators to check for null values.
9. 3. Aggregate Functions: Aggregate functions
such as COUNT, SUM, AVG, etc., generally
ignore null values unless explicitly specified
otherwise.
SELECT COUNT(FirstName) FROM Employees;
This query counts the non-null values in the
FirstName column. To count all values,
including nulls, you could use:
SELECT COUNT(*) FROM Employees;
10. • Sorting: Null values are often treated differently when
sorting data. Depending on the database system, null
values might be sorted either at the beginning or end
of a result set. You can use the ORDER BY clause with
the NULLS FIRST or NULLS LAST option to specify how
nulls should be ordered.
• SELECT * FROM Employees ORDER BY FirstName
NULLS FIRST;
• his query sorts the result set by the FirstName column,
with null values appearing first.
• Overall, handling null values properly is crucial for
obtaining accurate query results in SQL. Understanding
how nulls behave in comparisons and utilizing
appropriate operators like IS NULL or IS NOT NULL
helps ensure correct query logic.