Introduction
The NOT NULL constraint makes sure a column always has a value. It stops empty or missing data in that column.
CREATE TABLE table_name ( column_name datatype NOT NULL );
CREATE TABLE Users ( id INT NOT NULL, name VARCHAR(100) NOT NULL );
ALTER TABLE Employees MODIFY email VARCHAR(255) NOT NULL;
CREATE TABLE Products ( product_id INT NOT NULL, price DECIMAL(10,2) NOT NULL );
CREATE TABLE Customers ( customer_id INT NOT NULL, customer_name VARCHAR(50) NOT NULL ); INSERT INTO Customers (customer_id, customer_name) VALUES (1, 'Alice'); -- This will fail because customer_name is NOT NULL INSERT INTO Customers (customer_id, customer_name) VALUES (2, NULL);
NOT NULL constraint do in a database table?email in a table users?products with a price column defined as DECIMAL NOT NULL, what happens when you run this query?INSERT INTO products (name, price) VALUES ('Pen', NULL);CREATE TABLE employees (id INT, name VARCHAR(50) NOT NULL);
INSERT INTO employees (id, name) VALUES (1, NULL);
username column in your accounts table always has a value and cannot be empty. Which of these combined approaches is best to enforce this?CREATE TABLE accounts (
id INT PRIMARY KEY,
username VARCHAR(30) NOT NULL CHECK (username <> '')
);
Pass the test and this pattern moves unknown