What is a NOT NULL Constraint in MySQL?
A NOT NULL Constraint in MySQL is used to prevent a column in a table from storing NULL values, and every record in that column must have a valid value.
Syntax:
CREATE TABLE table_name (
column_name data_type NOT NULL
);
Example:
Example: Let the Student table contain three columns: Student_ID, Name, and Age, with three rows. The column 'Name' stores the student's name and cannot contain NULL values because it is set with a NOT NULL Constraint.
Output:
+------------+---------+-----+ | Student_ID | Name | Age | +------------+---------+-----+ | 101 | Rahul | 20 | | 102 | Amit | 21 | | 103 | Neha | 19 | +------------+---------+-----+
Explanation:
The column 'Name' cannot store NULL values because the NOT NULL Constraint is set on the column.Because a NOT NULL Constraint is set on this column, MySQL does not allow NULL values to be inserted into this field.
How Do You Set a NOT NULL Constraint on a Table in MySQL?
You can set a NOT NULL Constraint on a table by using the following two methods:
-
By using the CREATE TABLE statement while creating a new table.
Example:
CREATE TABLE Student ( Student_ID INT, Name VARCHAR(50) NOT NULL, Age INT );
Steps:
- First, you write the CREATE TABLE statement, and then you write the table name.
- Open the parentheses (.
- Inside the parentheses, write the column names and their data types.
- For the column that you want to make NOT NULL, write the NOT NULL keyword after the data type of that column.
- Write the remaining column names with their data types, separating each field with a comma.
- Close the parentheses ) and end the query with a semicolon ;.
- Finally, you run the query to create the table with the NOT NULL Constraint.
-
By adding a NOT NULL Constraint to an existing table using the ALTER TABLE statement.
Example:
ALTER TABLE Student MODIFY Name VARCHAR(50) NOT NULL;
Steps:
- First, write the ALTER TABLE statement.
- Next, write the name of the existing table.
- Write the MODIFY clause with the column name and data type.
- Write the NOT NULL keyword after the data type of that column.
- Close the query and end it with a semicolon ;.
- Finally, execute the query to add the NOT NULL Constraint to the existing table.
Advantages of a NOT NULL Constraint
-
Prevents NULL Values
It does not allow NULL values in the column. So, every record must have the required value.
-
Maintains Data Accuracy
If you set a NOT NULL Constraint on a column, it helps keep accurate data by making sure the column always has a valid value.
-
Ensures Required Data
It does not permit NULL values when users insert records into a table. If a user does not enter a value for the column, MySQL shows an error message.
-
Improves Data Consistency
When you define a NOT NULL Constraint on a column, it helps keep the data consistent by preventing missing values in that column when users add records to a table.
Common Mistakes When Setting a NOT NULL Constraint in MySQL
The following are common mistakes that you should avoid:
-
To Insert NULL Values
You should always enter a value in the column.
-
To Apply NOT NULL on the Wrong Column
You should choose the correct column for the NOT NULL Constraint. That means you should set it only on a column that must always have a value.
-
To Forget Required Values
You should always enter a value in every NOT NULL column.
-
To Confuse NOT NULL with UNIQUE Constraint
You should not confuse a NOT NULL Constraint with a UNIQUE Constraint. A NOT NULL Constraint does not allow NULL values, while a UNIQUE Constraint does not allow duplicate values.
-
Choose the Incorrect Data Type
You should choose the correct data type for the NOT NULL column.
-
To Apply NOT NULL Without Checking Existing Data
Before checking and applying a NOT NULL Constraint to a column, you should check the current data.












