0
0
PostgreSQLquery~5 mins

DEFAULT values and expressions in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
Which of the following is a valid DEFAULT expression in PostgreSQL?
ADEFAULT NULL
BDEFAULT 'today'
CDEFAULT NOW() + '5'
DDEFAULT CURRENT_DATE
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;
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.
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;
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.