CodeBlanks Logo Icon CodeBlanks

DEFAULT

Give columns a default value.

DEFAULT

With DEFAULT you set a fallback value used when no value is given for a column.

CREATE TABLE products (
  id INT PRIMARY KEY,
  stock INT DEFAULT 0
);

If stock is omitted it becomes 0.

The table products is created.

DEFAULT — details

With DEFAULT CURRENT_TIMESTAMP you can auto-fill the insert time. DEFAULT can be a constant, function or expression.

Practice DEFAULT

Create products with id INT PRIMARY KEY and stock INT DEFAULT 0.

Starter code

query.sql
-- create table
CREATE TABLE products

Sample solution

query.sql
CREATE TABLE products (
  id INT PRIMARY KEY,
  stock INT DEFAULT 0
)

Expected output

Tablo oluşturuldu

Questions and answers

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

1 What does DEFAULT do?

  • Drops a column
  • It adds a column
  • Sets a default value Correct answer
  • Creates an index

Explanation: DEFAULT assigns a fallback value.

2 Default

Question
CREATE TABLE products (
  stock INT ____ 0
);
Answer
CREATE TABLE products (
  stock INT DEFAULT 0
);

Explanation: DEFAULT is the right answer.

3 DEFAULT: match the terms

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

All lessons in Beginner