A NOT NULL constraint prevents a column from storing NULL values. It is used when a value is required for every row.
Required business information should not be missing. NOT NULL helps enforce this requirement at the database level.
An EmployeeName column should normally contain a value for every employee, so it can be defined as NOT NULL.
CREATE TABLE Employees (
EmployeeID INT NOT NULL,
EmployeeName VARCHAR(100) NOT NULL,
Salary DECIMAL(12,2)
);
NOT NULL is commonly used for names, identifiers, transaction dates, amounts, statuses, and other mandatory fields.
- NOT NULL prevents NULL values.
- It is used for required fields.
- It helps maintain data quality.
- It is defined at the column level.
- It is commonly used with other constraints.
What does NOT NULL do?
NOT NULL prevents a column from containing NULL values.
Why use NOT NULL?
It ensures that required information is provided for every applicable record.