Lessons
SQL Constraints
SQL Data Handling
SQL SELECT and WHERE
Aggregate Functions
Nested Queries
Conclusion
SQL NOT NULL
The NOT NULL
constraint in SQL ensures that a column cannot hold NULL
values. This constraint guarantees that every row in the column contains valid data, preventing the insertion of incomplete records into a table. By enforcing the presence of a value, the NOT NULL
constraint helps maintain data integrity and reliability.
Why Use the NOT NULL Constraint?
The NOT NULL
constraint is essential for ensuring that critical fields always contain a value. Here’s why it’s important:
- Prevent Missing Data: Ensures no essential information is left blank in a table.
- Enhance Data Reliability: Guarantees that required fields have meaningful values.
- Avoid Application Errors: Eliminates the need for handling
NULL
values in calculations or operations.
Apply NOT NULL Constraint
The NOT NULL
constraint can be applied to a column during table creation or later by altering an existing table.
1. Add NOT NULL on Table Creation
When creating a table, you can specify the NOT NULL
constraint for one or more columns to ensure those columns always have values.
Example:
mysql
1 2 3 4 5 6
CREATE TABLE Employees ( EmployeeID INT NOT NULL, LastName VARCHAR(100) NOT NULL, FirstName VARCHAR(100) NOT NULL, Salary DECIMAL(10, 2) );
EmployeeID
,LastName
, andFirstName
columns cannot haveNULL
values.- The
Salary
column can acceptNULL
values because it does not have theNOT NULL
constraint.
2. Adding NOT NULL to an Existing Table
You can add a NOT NULL
constraint to an existing column in a table using the ALTER TABLE
statement.
SQL Server / MS Access Example:
mysql
1 2
ALTER TABLE Employees ALTER COLUMN Salary DECIMAL(10, 2) NOT NULL;
MySQL / Oracle Example:
mysql
1 2
ALTER TABLE Employees MODIFY COLUMN Salary DECIMAL(10, 2) NOT NULL;
This enforces the NOT NULL
constraint on the Salary
column, ensuring it cannot contain NULL
values.
Key Characteristics of NOT NULL Constraint
- Mandatory Data Entry: Columns with the
NOT NULL
constraint must have values during insertion or update. - Error Prevention: Attempting to insert or update a
NULL
value in aNOT NULL
column results in an error. - Default Values: Combine
NOT NULL
with theDEFAULT
constraint to automatically populate a column if no value is provided.
Benefits of Using NOT NULL
- Data Integrity: Ensures essential fields like IDs, names, or timestamps always have valid entries.
- Simplified Queries: Eliminates the need to check for
NULL
values in queries or operations. - Consistent Data: Avoids unexpected issues caused by missing or incomplete information.
Common Use Cases for NOT NULL
- Primary Key Columns: Always combine
NOT NULL
with primary keys to uniquely identify rows. - Mandatory Fields: Enforce non-null constraints on fields like names, email addresses, or timestamps to ensure completeness.
- Critical Data Points: Use
NOT NULL
for essential numeric or text fields that should never be left empty.
Key Points to Remember
- The
NOT NULL
constraint prevents columns from storingNULL
values. - It can be applied during table creation or later by modifying the table structure.
- Use the
NOT NULL
constraint for mandatory fields to maintain data reliability. - Attempting to insert or update a
NULL
value in aNOT NULL
column will result in an error. - Combine
NOT NULL
withDEFAULT
values or constraints likeUNIQUE
andPRIMARY KEY
for robust database design.