Open In App

MySQL NOT NULL Constraint

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In the database management system maintaining data reliability and data accuracy is very important. MySQL is a popular relational database management system, which offers various constraints to provide security and ensure the integrity of the stored data. There are various key constraints present in the table among these, the NOT NULL Constraint enforces the presence of values within specific columns. In this article, we will see the MySQL NOT NULL constraints, covering their implementation, addition, removal, and significance in database design.

MySQL NOT NULL Constraint

The NOT NULL constraint in MySQL serves as a validation rule for the columns. In NOT NULL constraints the column cannot contain the NULL values. This constraint guarantees that each row in the table must have a value for that particular column. NOT NULL constraint prevents the insertion of the empty or missing data into the column of that table. Due to this restriction, data accuracy and reliability increase, and the database becomes more robust.

Example:

Creating a ‘users‘ table without a NOT NULL constraint initially.

CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100),
age INT
);

Insert some data in the users table:

UserTable

User Table

Adding a NOT NULL Constraint to an Existing Column

To add the NOT NULL constraint to an existing column, a straightforward alteration to the table structure is required. Using SQL commands, this constraint can be added to ensure data consistency. Here’s an example of adding a NOT NULL constraint to the ‘email‘ column in a ‘users‘ table:

ALTER TABLE users
MODIFY COLUMN email VARCHAR(100) NOT NULL;

Displaying the altered table structure:

UserTableAddNull

After running above Query

Explanation: This command modifies the ‘email‘ column in the ‘users‘ table, specifying that it cannot contain NULL values. Attempting to insert NULL into ‘email‘ (will result in an error due to NOT NULL constraint). The insert statement will fail because it violates the NOT NULL constraint added to the ‘email‘ column, ensuring that NULL values cannot be inserted.

Removing a NOT NULL Constraint

Removing a NOT NULL Constraint from the column can affect existing data and integrity. To remove the NOT NULL constraint from a column, the following SQL query can be used.

ALTER TABLE users
MODIFY COLUMN email VARCHAR(100);

Displaying the altered table structure after removing the NOT NULL constraint.

UserTableRemoveNull

After running above Query

Explanation: By removing the ‘NOT NULL‘ constraint from the column, NULL values are allowed to be inserted and it impacts on data consistency. After modifying ‘email‘ column we can the inserting NULL values into the ‘email‘ column.

Conclusion

MySQL NOT NULL constraints are important tools for ensuring data integrity in databases. They ensure that the required columns contain valid information, contributing to the overall reliability of the database system. When adding or removing these restrictions, it is important to carefully consider existing information and potential impact. By acknowledging and applying NOT NULL constraints, database administrators and developers can create robust, reliable databases that consistently deliver accurate information.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads