Horje
MySQL NOT NULL Constraint

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 INTO users (user_id, username, email, age)
VALUES
(1, 'ram_123', '[email protected]', 30),
(2, 'sumitb 17', '[email protected]', NULL),
(3, 'rohit264', '[email protected]', 35),
(4, 'mahi', '[email protected]', NULL),
(5, 'Bob', '[email protected]', 25);

Output:

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 data consistency. After modifying the ‘email‘ column we can insert 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.

FAQs on MySQL NOT NULL Constraint

What is a NOT NULL constraint in MySQL?

A NOT NULL constraint is used in MySQL to ensure that a column cannot contain NULL values. It enforces that every row in the table must have a value for that particular column.

Why are NOT NULL constraints important?

NOT NULL constraints are important for maintaining data integrity. They prevent the insertion of empty or missing data into columns where values are required, thereby ensuring data reliability.

How do you add a NOT NULL constraint to an existing column?

To add a NOT NULL constraint to an existing column in MySQL, you use the ALTER TABLE statement with MODIFY COLUMN, specifying the column name followed by NOT NULL.

Can you remove a NOT NULL constraint from a column?

Yes, you can remove a NOT NULL constraint from a column using the ALTER TABLE statement with MODIFY COLUMN, but it’s essential to consider the impact on existing data and ensure it won’t compromise data integrity.



Reffered: https://www.geeksforgeeks.org


Databases

Related
MySQL RIGHT JOIN MySQL RIGHT JOIN
MySQL CREATE TABLE MySQL CREATE TABLE
Connect MongoDB Atlas Cluster to MongoDB Compass Connect MongoDB Atlas Cluster to MongoDB Compass
View Existing Validation Rules in MongoDB View Existing Validation Rules in MongoDB
Modify Valid or Invalid Documents in MongoDB Modify Valid or Invalid Documents in MongoDB

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14