0
0
PostgreSQLquery~5 mins

Column constraints (NOT NULL, UNIQUE, CHECK) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the NOT NULL constraint do in a database column?
The NOT NULL constraint ensures that a column cannot have a NULL value. Every row must have a value for this column.
Click to reveal answer
beginner
Explain the purpose of the UNIQUE constraint in a table column.
The UNIQUE constraint makes sure that all values in a column are different from each other. No two rows can have the same value in that column.
Click to reveal answer
intermediate
What is a CHECK constraint and how is it used?
A CHECK constraint limits the values that can be stored in a column by specifying a condition. Only values that meet the condition are allowed.
Click to reveal answer
intermediate
Can a column have both NOT NULL and UNIQUE constraints? What does that mean?
Yes, a column can have both. It means every row must have a value (not null) and all values must be different (unique).
Click to reveal answer
beginner
Write a simple SQL example adding a CHECK constraint to ensure age is 18 or older.
Example: <br>CREATE TABLE users (id SERIAL PRIMARY KEY, age INT CHECK (age >= 18));<br>This means age must be 18 or more.
Click to reveal answer
What happens if you try to insert a NULL value into a column with a NOT NULL constraint?
AThe insert fails with an error.
BThe NULL value is accepted.
CThe NULL is converted to zero.
DThe row is ignored.
Which constraint ensures all values in a column are different?
APRIMARY KEY
BCHECK
CNOT NULL
DUNIQUE
What does this SQL do? CHECK (salary > 0)
AAllows salary to be zero or negative.
BAllows only positive salary values.
CMakes salary optional.
DNo effect on salary.
Can a UNIQUE column contain NULL values in PostgreSQL?
AYes, but only one NULL is allowed.
BNo, NULLs are not allowed.
CYes, multiple NULLs are allowed.
DUNIQUE constraint ignores NULLs.
Which constraint would you use to prevent empty values in a column?
ANOT NULL
BFOREIGN KEY
CCHECK
DUNIQUE
Describe the differences and purposes of NOT NULL, UNIQUE, and CHECK constraints in a database column.
Think about what each constraint controls about the data.
You got /3 concepts.
    Write an example SQL statement to create a table with columns using NOT NULL, UNIQUE, and CHECK constraints.
    Use simple column names and conditions like age >= 18.
    You got /4 concepts.