đĢ The NOT NULL constraint in SQL is used to ensure that a column cannot store NULL values. It guarantees that every row must contain a valid value for that column, helping maintain data integrity and preventing incomplete records.
đ What is the NOT NULL Constraint?
By default, many SQL databases allow columns to contain NULL values, which represent missing or unknown data. Applying the NOT NULL constraint makes a column mandatory, meaning every new record must provide a value for that column.
Information
đ¯ Why Use NOT NULL?
The NOT NULL constraint ensures that important information is always provided.
- đ Prevent incomplete records.
- đ Improve data quality and consistency.
- đ Enforce mandatory fields.
- đ Reduce unexpected NULL-related errors.
- đ Support reliable querying and reporting.
đ Basic Syntax
NOT NULL Syntax
CREATE TABLE table_name
(
column_name data_type NOT NULL
);đĄ Create a Table with NOT NULL
Create a Students table where the student's name and department are required.
NOT NULL Example
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Department VARCHAR(100) NOT NULL,
Age INT
);In this table:
- StudentID must contain a value because it is the primary key.
- Name cannot be NULL.
- Department cannot be NULL.
- Age may contain NULL.
â Valid INSERT
This statement succeeds because all required columns have values.
Valid INSERT
INSERT INTO Students
(StudentID, Name, Department, Age)
VALUES
(101, 'Alice', 'Computer Science', 20);â Invalid INSERT
This statement fails because the Name column is defined as NOT NULL.
Invalid INSERT
INSERT INTO Students
(StudentID, Department, Age)
VALUES
(102, 'Mathematics', 21);Error
đ Add NOT NULL to an Existing Column
If a table already exists, you can modify a column to make it mandatory. Ensure that existing rows do not contain NULL values before applying the constraint.
MySQL Example
ALTER TABLE Students
MODIFY Name VARCHAR(100) NOT NULL;SQL Server Example
ALTER TABLE Students
ALTER COLUMN Name VARCHAR(100) NOT NULL;Warning
đ Example Table
| StudentID | Name | Department | Age |
|---|---|---|---|
| 101 | Alice | Computer Science | 20 |
| 102 | Bob | Mathematics | 21 |
âī¸ NULL vs NOT NULL
| Feature | NULL | NOT NULL |
|---|---|---|
| Allows Missing Values | â Yes | â No |
| Requires a Value | â No | â Yes |
| Typical Use | Optional information. | Mandatory information. |
đŧ Real-World Example
In an employee management system, every employee must have an employee ID, full name, and date of joining. These fields should never be empty.
Employee Table
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
FullName VARCHAR(100) NOT NULL,
JoinDate DATE NOT NULL,
PhoneNumber VARCHAR(20)
);Here, PhoneNumber is optional, but the employee's identity and joining date are mandatory.
â ī¸ Common Mistakes
- â Applying NOT NULL to columns that should remain optional.
- â Adding the constraint before cleaning existing NULL values.
- â Confusing an empty string with NULL.
- â Forgetting to provide values during INSERT operations.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ NOT NULL prevents a column from storing NULL values.
- đ It helps maintain complete and reliable data.
- đ Required values must be provided during INSERT and UPDATE operations.
- đ Existing NULL values must usually be removed before adding the constraint.
- đ The syntax for modifying existing columns varies across database systems.
- đ Use NOT NULL only for fields that are truly required.