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?
✗ Incorrect
NOT NULL means the column must have a value. Inserting NULL causes an error.
Which constraint ensures all values in a column are different?
✗ Incorrect
UNIQUE constraint makes sure no two rows have the same value in that column.
What does this SQL do?
CHECK (salary > 0)✗ Incorrect
CHECK ensures salary must be greater than zero.
Can a UNIQUE column contain NULL values in PostgreSQL?
✗ Incorrect
In PostgreSQL, UNIQUE allows multiple NULLs because NULL is not equal to NULL.
Which constraint would you use to prevent empty values in a column?
✗ Incorrect
NOT NULL prevents empty (NULL) values in a column.
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.