In Mysql, The NOT NULL constraint is used to specify that a column in a table cannot contain NULL values.
This constraint enforces that every row in the table must have a non-null value for that column and it is essential for ensuring data completeness and integrity by preventing the insertion of incomplete or missing data into the table.
The common use case of NOT NULL Constraints is to prevent mandatory fields should not contain null values in tables such as like id, email, age, and DOB etc.
When we declare a column as NOT NULL, it means that the column must have a value during the INSERT operation, and the absence of a value (i.e., NULL) is not allowed.
CREATE TABLE products ( product_code INT PRIMARY KEY, product_name VARCHAR(50) NOT NULL, product_category VARCHAR(100) NOT NULL, product_price INT, );
We are creating the `products` table with columns such as `product_code`, `product_name`, `product_price` and `product_category`.
we have defined some columns with NOT NULL Constraints for ex:- "product_name", "product_price" and "product_category" and these constraints indicate that columns cannot contain "NULL" values.
The `PRIMARY KEY` constraint is also applied to the `product_code` column, ensuring that each `product_code` is unique.
We are attempting to insert a row into the `products` table without providing values for the `product_name`, `product_price` and `product_category` columns will result in an error due to the NOT NULL constraint.
<!-- it will fail due to NOT NULL constraint --> INSERT INTO products (product_code) VALUES (1);
To insert a row into a `products` table with "NOT NULL" columns, we need to provide values for those columns during the INSERT operation in SQL Table.
<!-- This will get succeed --> INSERT INTO products (product_code, product_name, product_category, product_price) VALUES (1, 'iPad', 'GADGETS', 400);
The NOT NULL constraint is useful when we want to ensure that certain columns always contain valid data, and it helps maintain data integrity within our database.