DEFAULT
- SQL course
- Beginner
- Lesson 92/115
- 4 min
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
-- create table
CREATE TABLE productsSample solution
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
CREATE TABLE products (
stock INT ____ 0
);
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 |