MySQL NOT NULL Constraint (original) (raw)
Last Updated : 20 Jun, 2024
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', 'ram123@gmail.com', 30),
(2, 'sumitb 17', 'sumit@gmail.com', NULL),
(3, 'rohit264', 'rohits@yopmail.com', 35),
(4, 'mahi', 'msd@gmail.com', NULL),
(5, 'Bob', 'bobzy@gmail.com', 25);
**Output:
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:
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.
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.