The NOT NULL constraint makes sure a column always has a value. It stops empty or missing data in that column.
NOT NULL constraint in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
CREATE TABLE table_name ( column_name data_type NOT NULL );
CREATE TABLE Users ( id INT NOT NULL, name VARCHAR(50) NOT NULL );
CREATE TABLE Products ( product_id INT NOT NULL, price DECIMAL(10,2) NOT NULL );
CREATE TABLE Events ( event_id INT NOT NULL, event_date DATE NOT NULL );
This example creates an Employees table where none of the columns can be empty. The first insert works fine. The second insert fails because email is NULL, which is not allowed. The SELECT shows the rows that were successfully added.
CREATE TABLE Employees ( employee_id INT NOT NULL, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, email VARCHAR(50) NOT NULL ); INSERT INTO Employees (employee_id, first_name, last_name, email) VALUES (1, 'Alice', 'Smith', 'alice@example.com'); -- This will fail because email is NOT NULL INSERT INTO Employees (employee_id, first_name, last_name, email) VALUES (2, 'Bob', 'Jones', NULL); SELECT * FROM Employees;
NOT NULL helps keep your data complete and reliable.
If you try to insert NULL into a NOT NULL column, the database will give an error.
You can add NOT NULL when creating a table or alter an existing column to add it.
NOT NULL means a column must always have a value.
It prevents empty or missing data in important columns.
Use NOT NULL to keep your data clean and trustworthy.
Practice
What does the NOT NULL constraint do in a database table?
Solution
Step 1: Understand the purpose of NOT NULL
The NOT NULL constraint prevents a column from having NULL (empty) values, ensuring data is always present.Step 2: Compare options with the definition
Only It ensures a column cannot have empty or missing values. correctly describes this behavior; others describe different constraints or actions.Final Answer:
It ensures a column cannot have empty or missing values. -> Option CQuick Check:
NOT NULL means no empty values allowed [OK]
- Confusing NOT NULL with UNIQUE constraint
- Thinking NOT NULL deletes rows
- Assuming NOT NULL auto-generates values
Which of the following is the correct syntax to add a NOT NULL constraint to a column named email when creating a table?
CREATE TABLE users (
id INT PRIMARY KEY,
email ???
);
Solution
Step 1: Recall correct order of column definition
The data type comes first, then constraints like NOT NULL.Step 2: Check each option's order
VARCHAR(255) NOT NULL correctly places VARCHAR(255) before NOT NULL; others have wrong order or use NULL instead.Final Answer:
VARCHAR(255) NOT NULL -> Option DQuick Check:
Data type first, then NOT NULL [OK]
- Writing NOT NULL before data type
- Using NULL instead of NOT NULL
- Omitting data type
Given the table employees with columns id INT PRIMARY KEY and name VARCHAR(100) NOT NULL, what happens when you run this query?
INSERT INTO employees (id, name) VALUES (1, NULL);
Solution
Step 1: Understand NOT NULL behavior on insert
NOT NULL means the column cannot accept NULL values during insert or update.Step 2: Analyze the insert statement
The query tries to insert NULL into the name column, violating the NOT NULL constraint, causing an error.Final Answer:
The query fails with an error due to NOT NULL constraint. -> Option AQuick Check:
NOT NULL rejects NULL inserts [OK]
- Assuming NULL is converted to empty string
- Thinking the row inserts ignoring NULL
- Confusing NOT NULL with default values
Consider this table creation statement:
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
price DECIMAL(5,2)
);
Which of the following statements will cause an error?
Solution
Step 1: Identify columns with NOT NULL constraint
Only product_name has NOT NULL, so it must have a value on insert.Step 2: Check each insert statement
INSERT INTO products (product_id, price) VALUES (2, 2.00); omits product_name, so it tries to insert NULL, causing an error. Others provide product_name.Final Answer:
INSERT missing NOT NULL column product_name causes error. -> Option BQuick Check:
NOT NULL columns must be included in insert [OK]
- Ignoring NOT NULL columns in insert
- Assuming NULL allowed if column omitted
- Confusing NULL and empty string
You have a table orders with columns order_id INT PRIMARY KEY, customer_id INT NOT NULL, and order_date DATE NOT NULL. You want to add a new column status that must always have a value and defaults to 'pending'. Which is the correct way to add this column?
Solution
Step 1: Understand NOT NULL with default value
Adding a NOT NULL column requires a default value to avoid errors on existing rows.Step 2: Analyze each ALTER statement
ALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending'; adds status with NOT NULL and default 'pending', so existing rows get this value. Others miss NOT NULL or default.Final Answer:
ALTER with NOT NULL and DEFAULT 'pending' is correct. -> Option AQuick Check:
NOT NULL columns need default when added to existing table [OK]
- Adding NOT NULL column without default
- Assuming default alone enforces NOT NULL
- Omitting NOT NULL when required
