Lesson 36 of 60 – NOT NULL Constraint
60%

NOT NULL Constraint in SQL

The NOT NULL constraint is used to make sure that a column cannot contain NULL values. It is useful when a particular field must always have a value.

Note: NOT NULL means that a column must contain a value. It does not mean that the value must be unique.

1. What is NOT NULL?

The NOT NULL constraint prevents a column from storing NULL values.

CREATE TABLE students (
    student_id INT NOT NULL,
    name VARCHAR(100) NOT NULL
);

Here, both student_id and name must have values.

2. Why Use NOT NULL?

NOT NULL is used when a field is required for every record.

For example, a student record may require:

  • Student name
  • Mobile number
  • Course name

These fields should not be left NULL.

3. Basic NOT NULL Syntax

The basic syntax is:

column_name data_type NOT NULL

Example:

name VARCHAR(100) NOT NULL

4. NOT NULL During CREATE TABLE

You can define NOT NULL while creating a table.

CREATE TABLE students (
    id INT,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL
);

The name and mobile columns cannot contain NULL.

5. Insert a Valid Record

A valid record provides values for NOT NULL columns.

INSERT INTO students (id, name, mobile)
VALUES (1, 'Rahul', '9876543210');

This record satisfies the NOT NULL requirement.

6. Insert NULL into NOT NULL Column

If you try to insert NULL into a NOT NULL column, the database rejects the operation.

INSERT INTO students (id, name, mobile)
VALUES (2, NULL, '9876543211');

This causes an error because name is defined as NOT NULL.

7. NULL vs Empty String

NULL means that a value is missing or unknown.

An empty string '' is a string containing zero characters.

INSERT INTO students (name)
VALUES ('');

Depending on the database and column definition, an empty string is not the same as NULL.

8. NULL vs Zero

NULL and zero are different values.

marks = NULL
marks = 0

NULL means no value is stored, while 0 is an actual numeric value.

9. NOT NULL with INT

NOT NULL can be applied to numeric columns.

CREATE TABLE courses (
    course_id INT NOT NULL,
    fee INT NOT NULL
);

Both columns must contain values.

10. NOT NULL with VARCHAR

NOT NULL is commonly used with text columns.

CREATE TABLE students (
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL
);

Both name and email are required.

11. NOT NULL with DATE

Date columns can also use NOT NULL.

CREATE TABLE admissions (
    student_id INT NOT NULL,
    admission_date DATE NOT NULL
);

Every admission record must contain an admission date.

12. NOT NULL with DEFAULT

NOT NULL can be combined with a DEFAULT value.

CREATE TABLE students (
    name VARCHAR(100) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'Active'
);

If no status is supplied, the default value can be used.

13. NOT NULL and PRIMARY KEY

A PRIMARY KEY identifies each record uniquely and cannot contain NULL values.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

The primary key is inherently non-null.

14. NOT NULL and UNIQUE

UNIQUE and NOT NULL serve different purposes.

  • UNIQUE prevents duplicate values.
  • NOT NULL prevents NULL values.
email VARCHAR(150) NOT NULL UNIQUE

This requires an email value and prevents duplicate email values.

15. Multiple NOT NULL Columns

A table can contain many NOT NULL columns.

CREATE TABLE employees (
    id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    salary DECIMAL(10,2) NOT NULL
);

All four columns require values.

16. Checking Existing NULL Values

Before changing an existing column to NOT NULL, check whether NULL values already exist.

SELECT *
FROM students
WHERE mobile IS NULL;

If rows are returned, those NULL values should be handled before adding NOT NULL.

17. Add NOT NULL to an Existing Column

In MySQL, you can modify an existing column using ALTER TABLE.

ALTER TABLE students
MODIFY mobile VARCHAR(15) NOT NULL;

The existing data must satisfy the new NOT NULL requirement.

18. ALTER TABLE with NOT NULL and DEFAULT

You can define both NOT NULL and DEFAULT while modifying a column.

ALTER TABLE students
MODIFY status VARCHAR(20) NOT NULL DEFAULT 'Active';

This makes the column required and provides a default value.

19. Updating a NOT NULL Column

You cannot update a NOT NULL column to NULL.

UPDATE students
SET name = NULL
WHERE id = 1;

This violates the NOT NULL constraint.

20. Selecting NOT NULL Records

Use IS NOT NULL to find records where a column contains a value.

SELECT *
FROM students
WHERE mobile IS NOT NULL;

This returns records where mobile is not NULL.

21. IS NULL vs IS NOT NULL

Use these operators to check NULL values.

SELECT * FROM students
WHERE mobile IS NULL;
SELECT * FROM students
WHERE mobile IS NOT NULL;

Do not use = NULL to test for NULL.

22. NOT NULL in Student Table

A practical student table can use NOT NULL for required information.

CREATE TABLE students (
    student_id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    course VARCHAR(100) NOT NULL
);

Each student must have these basic details.

23. NOT NULL in Employee Table

Employee tables can also require important information.

CREATE TABLE employees (
    employee_id INT NOT NULL,
    employee_name VARCHAR(100) NOT NULL,
    department VARCHAR(100) NOT NULL
);

These fields cannot be NULL.

24. NOT NULL in Course Table

Course information can use NOT NULL for required fields.

CREATE TABLE courses (
    course_id INT NOT NULL,
    course_name VARCHAR(100) NOT NULL,
    fee DECIMAL(10,2) NOT NULL
);

A course must have an ID, name, and fee.

25. Dropping NOT NULL in MySQL

In MySQL, you can modify a column without NOT NULL to allow NULL values.

ALTER TABLE students
MODIFY mobile VARCHAR(15);

The column is no longer explicitly defined as NOT NULL.

26. NOT NULL with INSERT Columns

If a NOT NULL column does not have a default value, an INSERT must provide an appropriate value for it.

INSERT INTO students (student_id)
VALUES (101);

If other required columns have no default values, this statement can fail because their values are missing.

27. Common NOT NULL Mistake

A common mistake is trying to store NULL in a required column.

INSERT INTO students (student_id, name)
VALUES (101, NULL);

If name is NOT NULL, the database rejects the operation.

28. NOT NULL and Data Integrity

NOT NULL helps maintain data integrity by ensuring that required information is always present.

For example, if every student must have a name, defining name as NOT NULL helps prevent incomplete records.

29. Practical Example

Let's create a simple student table using NOT NULL.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    course VARCHAR(100) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'Active'
);

Here, the student's ID, name, mobile, course, and status are required.

30. Complete NOT NULL Example

Here is a complete example using NOT NULL and inserting valid data.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15) NOT NULL,
    course VARCHAR(100) NOT NULL
);

INSERT INTO students
(student_id, name, mobile, course)
VALUES
(1, 'Rahul', '9876543210', 'Python'),
(2, 'Priya', '9876543211', 'SQL');

All required columns contain values, so these records satisfy the NOT NULL constraint.

📌 Key Points

  • NOT NULL prevents a column from storing NULL values.
  • It is used for fields that are required.
  • NOT NULL can be used with INT, VARCHAR, DATE, DECIMAL, and other data types.
  • PRIMARY KEY columns cannot contain NULL values.
  • UNIQUE and NOT NULL have different purposes.
  • Use IS NULL and IS NOT NULL to check NULL values.
  • Before adding NOT NULL to an existing column, check for existing NULL values.
  • In MySQL, ALTER TABLE ... MODIFY can be used to change a column definition.
  • NOT NULL can be combined with DEFAULT.
  • NOT NULL helps maintain data integrity.

🧠 Quick Quiz

Question: Which SQL constraint prevents a column from storing NULL values?