SQL NOT NULL Constraint
In SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and integrity of the data stored in a database. One of the most commonly used constraints is the NOT NULL constraint, which ensures that a column cannot have NULL values.
This is important for maintaining data integrity, especially when specific data entries are mandatory. In this article, we will explore the SQL NOT NULL constraint in detail, its syntax, and how it can be applied to your tables.
What is the SQL NOT NULL Constraint?
The NOT NULL constraint is used to enforce that a column in a table must always contain a value; it cannot contain a NULL value. By default, columns in SQL can hold NULL values, meaning they can have no data. However, for certain columns—such as IDs, names, or any required fields—you may want to enforce the rule that no NULL values can be inserted.
This constraint is similar to a primary key constraint in that both prevent NULL values. However, they are different in their purpose and application. A primary key uniquely identifies each record in a table, while the NOT NULL constraint simply ensures that a column cannot have empty or undefined values.
Key Points:
- NOT NULL is used to enforce mandatory fields.
- It prevents NULL values from being inserted or updated.
- It is applicable at the column level.
- It can be used during table creation or modification (with the ALTER command).
Syntax:
CREATE TABLE table_Name
(
column1 data_type(size) NOT NULL,
column2 data_type(size) NOT NULL,
….
);
SQL NOT NULL on CREATE a Table
In SQL, we can add NOT NULL constraints while creating a table.
For example, the “EMPID” will not accept NULL values when the EMPLOYEES table is created because NOT NULL constraints are used with these columns.
Query:
CREATE TABLE Emp(
EmpID INT NOT NULL PRIMARY KEY,
Name VARCHAR (50),
Country VARCHAR(50),
Age int(2),
Salary int(10));
Output:

SQL NOT NULL on ALTER Table
We can also add a NOT NULL constraint in the existing table using the ALTER statement. For example, if the EMPLOYEES table has already been created then add NOT NULL constraints to the “Name” column using ALTER statements in SQL as follows:
Query:
ALTER TABLE Emp modify Name Varchar(50) NOT NULL;
This command ensures that the Name column will no longer accept NULL values, enforcing a requirement for all employee records to have a valid name.
Advantages of Using the NOT NULL Constraint
- Prevents Data Gaps: With NOT NULL, you avoid incomplete or missing data, which is crucial for accurate data analysis.
- Enforces Business Logic: Often, business rules require certain fields to be mandatory. For example, an employee record must have an employee ID, name, and department.
- Improves Data Integrity: By preventing null entries, you ensure that critical data is always present, improving overall database reliability.
Conclusion
The SQL NOT NULL constraint is a powerful tool that ensures the integrity and completeness of your database by preventing NULL values in critical columns. Whether you’re creating a new table or modifying an existing one, applying the NOT NULL constraint guarantees that important data is always present, which is essential for accurate reporting, analysis, and consistency across your database. Always consider which fields are required before inserting records into your tables to enforce data integrity with this constraint.