CodeBlanks Logo Icon CodeBlanks

NOT NULL

Mark required columns with NOT NULL.

NOT NULL

With NOT NULL you mark a column as required. It is used for mandatory fields.

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

Name cannot be null.

The table products is created.

NOT NULL — details

NOT NULL prevents inserting NULL into a column. It matters for data integrity; critical fields should always be NOT NULL.

Practice NOT NULL

Create products with id INT PRIMARY KEY and name VARCHAR(100) NOT NULL.

Starter code

query.sql
-- create table
CREATE TABLE products

Sample solution

query.sql
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 NOT NULL: match the terms

Term Meaning
NOT NULL not null
PRIMARY KEY primary
UNIQUE unique
DEFAULT default

2 Required field

Question
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100) ____
);
Answer
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

Explanation: NOT NULL is the right answer.

3 What does NOT NULL prevent?

  • Unique values
  • Inserting empty (NULL) values Correct answer
  • Deletes
  • Updates

Explanation: NOT NULL prevents NULL input.

All lessons in Beginner