What is the purpose of the DEFAULT keyword in PostgreSQL?
The DEFAULT keyword sets a predefined value for a column when no value is provided during an INSERT operation.
Click to reveal answer
intermediate
How can you use an expression as a DEFAULT value in PostgreSQL?
You can use expressions like functions or calculations as DEFAULT values, for example: DEFAULT CURRENT_TIMESTAMP or DEFAULT (10 * 0.9).
Click to reveal answer
beginner
True or False: DEFAULT values are applied when the INSERT statement omits the column.
True. DEFAULT values are used if the INSERT statement does not specify a value for that column.
Click to reveal answer
beginner
How do you define a DEFAULT value for a column in a CREATE TABLE statement?
Use the syntax: column_name data_type DEFAULT default_value, for example: created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Click to reveal answer
intermediate
Can DEFAULT values be changed after table creation? If yes, how?
Yes, use ALTER TABLE to change DEFAULT values, for example: ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT new_value;
Click to reveal answer
What happens if you insert a row without specifying a value for a column that has a DEFAULT value?
AThe column value becomes NULL.
BThe DEFAULT value is used for that column.
CThe insert fails with an error.
DThe column value is set to zero.
✗ Incorrect
If a column has a DEFAULT value and no value is provided during insert, PostgreSQL uses the DEFAULT value.
Which of the following is a valid DEFAULT expression in PostgreSQL?
ADEFAULT NULL
BDEFAULT 'today'
CDEFAULT NOW() + '5'
DDEFAULT CURRENT_DATE
✗ Incorrect
CURRENT_DATE is a valid function used as a DEFAULT expression. NOW() + '5' is invalid because you cannot add text directly to timestamp without casting.
How do you remove a DEFAULT value from a column?
AALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT;
BALTER TABLE table_name DROP COLUMN column_name;
CUPDATE table_name SET column_name = NULL;
DDELETE DEFAULT FROM column_name;
✗ Incorrect
To remove a DEFAULT value, use ALTER TABLE ... ALTER COLUMN ... DROP DEFAULT.
Can a DEFAULT value be a subquery in PostgreSQL?
AYes, any subquery can be used.
BOnly if the subquery returns a single value.
CNo, DEFAULT cannot be a subquery.
DOnly if the subquery is in a function.
✗ Incorrect
DEFAULT values cannot be subqueries; they must be constant expressions or functions.
Which statement correctly sets a DEFAULT value for a column after table creation?
AALTER TABLE mytable ALTER COLUMN mycol SET DEFAULT 100;
BALTER TABLE mytable SET DEFAULT mycol = 100;
CUPDATE mytable SET DEFAULT mycol = 100;
DALTER TABLE mytable ADD DEFAULT mycol 100;
✗ Incorrect
The correct syntax to set a DEFAULT value after creation is ALTER TABLE ... ALTER COLUMN ... SET DEFAULT.
Explain how DEFAULT values work in PostgreSQL and give an example of using an expression as a DEFAULT.
Think about what happens when you insert a row without specifying all columns.
You got /4 concepts.
Describe how to change or remove a DEFAULT value on an existing table column.
Focus on ALTER TABLE syntax for modifying columns.
You got /3 concepts.
Practice
(1/5)
1. What does the DEFAULT keyword do in a PostgreSQL table column definition?
easy
A. Sets a value automatically when no value is provided during insert
B. Deletes the column if no value is provided
C. Requires the user to always provide a value
D. Creates an index on the column
Solution
Step 1: Understand the purpose of DEFAULT
The DEFAULT keyword assigns a value automatically if no value is given during an insert operation.
Step 2: Compare options with this behavior
Only Sets a value automatically when no value is provided during insert correctly describes this automatic assignment behavior.
Final Answer:
Sets a value automatically when no value is provided during insert -> Option A
Hint: Defaults fill missing columns, provided values override [OK]
Common Mistakes:
Assuming missing columns become NULL instead of default
Expecting error when default exists
Confusing default value with NOT NULL constraint
4. Consider this table:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
Which of the following INSERT statements will cause an error?
medium
A. INSERT INTO users (id, username, created_at) VALUES (DEFAULT, 'alice', DEFAULT);
B. INSERT INTO users (username, created_at) VALUES ('bob', DEFAULT);
C. INSERT INTO users (username) VALUES ('alice');
D. INSERT INTO users (created_at) VALUES (NOW());
Solution
Step 1: Check NOT NULL column requirements
The username column is NOT NULL and must be provided in inserts.
Step 2: Analyze each insert
INSERT INTO users (created_at) VALUES (NOW()); inserts only created_at but omits username, causing a NOT NULL violation error.
Final Answer:
INSERT INTO users (created_at) VALUES (NOW()); -> Option D
Quick Check:
NOT NULL columns must be provided [OK]
Hint: NOT NULL columns require values, even with defaults [OK]
Common Mistakes:
Assuming DEFAULT VALUES inserts NULL for NOT NULL columns
Thinking DEFAULT can replace NOT NULL columns
Ignoring NOT NULL constraints
5. You want to create a table products where the created_at column defaults to the current timestamp, and the price column defaults to 9.99 if no price is given. Which of the following CREATE TABLE statements correctly implements this?
hard
A. CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 9.99,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP()
);
B. CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 9.99,
created_at TIMESTAMP DEFAULT NOW()
);
C. CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT '9.99',
created_at TIMESTAMP DEFAULT NOW
);
D. CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 9.99,
created_at TIMESTAMP DEFAULT CURRENT_DATE
);
Solution
Step 1: Check default for price column
Price should default to numeric 9.99 without quotes (numeric literal).
Step 2: Check default for created_at column
Use NOW() function call for current timestamp, not CURRENT_TIMESTAMP or CURRENT_DATE (date only).
Step 3: Validate syntax correctness
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 9.99,
created_at TIMESTAMP DEFAULT NOW()
); uses price NUMERIC DEFAULT 9.99 and created_at TIMESTAMP DEFAULT NOW(), which is correct syntax and behavior.
Final Answer:
CREATE TABLE products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
price NUMERIC DEFAULT 9.99,
created_at TIMESTAMP DEFAULT NOW()
); -> Option B
Quick Check:
Use NOW() for timestamp, numeric literals without quotes [OK]
Hint: Use NOW() for timestamp defaults, numeric literals without quotes [OK]
Common Mistakes:
Using quotes around numeric default values
Using CURRENT_DATE instead of CURRENT_TIMESTAMP for timestamps
Omitting parentheses in NOW() function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.