0
0
PostgreSQLquery~10 mins

Column constraints (NOT NULL, UNIQUE, CHECK) in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the email column not accept empty values.

PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255) [1]);
Drag options to blanks, or click blank then click option'
ANOT NULL
BUNIQUE
CCHECK
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Using UNIQUE instead of NOT NULL, which allows NULL values but prevents duplicates.
Using CHECK without a proper condition.
2fill in blank
medium

Complete the code to ensure the username column has unique values.

PostgreSQL
CREATE TABLE accounts (id SERIAL PRIMARY KEY, username VARCHAR(50) [1]);
Drag options to blanks, or click blank then click option'
ANOT NULL
BCHECK
CUNIQUE
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Using NOT NULL only, which does not prevent duplicates.
Using CHECK without a condition.
3fill in blank
hard

Fix the error in the age column to allow only ages 18 or older.

PostgreSQL
CREATE TABLE members (id SERIAL PRIMARY KEY, age INT [1] (age >= 18));
Drag options to blanks, or click blank then click option'
ACHECK
BUNIQUE
CNOT NULL
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Using NOT NULL which only prevents empty values.
Using UNIQUE which prevents duplicates but not value ranges.
4fill in blank
hard

Fill both blanks to create a price column that cannot be empty and must be positive.

PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, price NUMERIC [1] [2] (price > 0));
Drag options to blanks, or click blank then click option'
ANOT NULL
BUNIQUE
CCHECK
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Using UNIQUE instead of NOT NULL.
Omitting the CHECK condition for positive values.
5fill in blank
hard

Fill all three blanks to create a user_id column that is unique, not empty, and must be greater than zero.

PostgreSQL
CREATE TABLE orders (order_id SERIAL PRIMARY KEY, user_id INT [1] [2] [3] (user_id > 0));
Drag options to blanks, or click blank then click option'
ANOT NULL
BUNIQUE
CCHECK
DDEFAULT
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing order of constraints incorrectly.
Forgetting NOT NULL or CHECK constraints.