📖 Scenario: You are creating a simple database table to store information about employees in a company. You want to make sure the data is clean and follows some rules.
🎯 Goal: Create a table called employees with columns that have constraints: id cannot be null, email must be unique, and age must be at least 18.
📋 What You'll Learn
Create a table named employees
Add a column id that cannot be NULL
Add a column email that must be UNIQUE
Add a column age with a CHECK constraint to ensure age is 18 or older
💡 Why This Matters
🌍 Real World
Companies use column constraints to keep their data accurate and reliable, like making sure emails are unique or ages are valid.
💼 Career
Database administrators and developers use constraints to enforce rules and prevent bad data from entering the database.
Progress0 / 4 steps
1
Create the employees table with basic columns
Write a SQL statement to create a table called employees with columns id as integer, email as text, and age as integer.
PostgreSQL
Hint
Use CREATE TABLE employees and list the columns with their data types inside parentheses.
2
Add NOT NULL constraint to id
Modify the employees table creation to add a NOT NULL constraint to the id column so it cannot be empty.
PostgreSQL
Hint
Add NOT NULL right after the INTEGER type for the id column.
3
Add UNIQUE constraint to email
Modify the employees table creation to add a UNIQUE constraint to the email column so no two employees can have the same email.
PostgreSQL
Hint
Add UNIQUE right after the TEXT type for the email column.
4
Add CHECK constraint to age to ensure minimum age
Modify the employees table creation to add a CHECK constraint on the age column so that age must be 18 or older.
PostgreSQL
Hint
Add CHECK (age >= 18) right after the INTEGER type for the age column.
Practice
(1/5)
1. Which column constraint in PostgreSQL ensures that a column cannot have empty or missing values?
easy
A. PRIMARY KEY
B. NOT NULL
C. CHECK
D. UNIQUE
Solution
Step 1: Understand the NOT NULL constraint
NOT NULL means the column must always have a value; it cannot be left empty.
Step 2: Compare with other constraints
UNIQUE prevents duplicates but allows NULLs; CHECK enforces custom rules; PRIMARY KEY implies NOT NULL and UNIQUE but is more specific.
Final Answer:
NOT NULL -> Option B
Quick Check:
NOT NULL = no empty values [OK]
Hint: NOT NULL means no empty values allowed in the column [OK]
Common Mistakes:
Confusing UNIQUE with NOT NULL
Thinking CHECK prevents NULLs
Assuming PRIMARY KEY is the only way to disallow NULL
2. Which of the following is the correct syntax to add a UNIQUE constraint to the column email in a table users?
easy
A. ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);
B. ALTER TABLE users ADD UNIQUE email;
C. ALTER TABLE users ADD UNIQUE (email) CONSTRAINT unique_email;
D. ALTER TABLE users ADD CONSTRAINT email UNIQUE;
Solution
Step 1: Recall the syntax for adding UNIQUE constraint
The correct syntax is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
Step 2: Match the syntax with options
ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email); matches the correct syntax exactly, naming the constraint and specifying the column.
Final Answer:
ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email); -> Option A
Quick Check:
ADD CONSTRAINT name UNIQUE (column) [OK]
Hint: Use ADD CONSTRAINT name UNIQUE (column) to add UNIQUE [OK]
Common Mistakes:
Omitting CONSTRAINT keyword
Placing UNIQUE before CONSTRAINT name incorrectly
Not specifying column in parentheses
3. Given the table definition:
CREATE TABLE products ( id SERIAL PRIMARY KEY, price NUMERIC CHECK (price > 0), sku TEXT UNIQUE );
What happens if you try to insert price = -5 and sku = 'A123'?
medium
A. Insert fails due to UNIQUE constraint violation on sku
B. Insert succeeds because CHECK allows negative values
C. Insert fails due to CHECK constraint violation on price
D. Insert succeeds because price can be negative
Solution
Step 1: Analyze the CHECK constraint on price
The CHECK constraint requires price > 0, so negative values like -5 violate this rule.
Step 2: Consider the UNIQUE constraint on sku
Since 'A123' is new, UNIQUE is not violated here.
Final Answer:
Insert fails due to CHECK constraint violation on price -> Option C
CREATE TABLE employees ( id SERIAL PRIMARY KEY, email TEXT UNIQUE NOT NULL, age INT CHECK (age >= 18) );
Which statement will cause an error when inserting data?
medium
A. INSERT INTO employees (email, age) VALUES ('john@example.com', 25);
B. INSERT INTO employees (email, age) VALUES ('jane@example.com', 20);
C. INSERT INTO employees (email, age) VALUES ('jane@example.com', 18);
D. INSERT INTO employees (email, age) VALUES (NULL, 30);
Solution
Step 1: Check NOT NULL constraint on email
Email cannot be NULL because of NOT NULL constraint.
Step 2: Check CHECK constraint on age
Age must be 18 or older; the ages in A(25), B(20), and D(18) all satisfy this.
Final Answer:
INSERT with NULL email causes error due to NOT NULL constraint -> Option D
Quick Check:
NOT NULL rejects NULL email [OK]
Hint: NOT NULL disallows NULL values, CHECK enforces conditions [OK]
Common Mistakes:
Focusing on CHECK constraint instead of NOT NULL
Ignoring NOT NULL on email
Assuming UNIQUE causes error on first insert
5. You want to create a table orders where the quantity column must be a positive integer and the order_code must be unique and never empty. Which is the best column definition for quantity and order_code?
hard
A. quantity INT CHECK (quantity > 0), order_code TEXT UNIQUE NOT NULL
B. quantity INT NOT NULL, order_code TEXT CHECK (order_code != '')
C. quantity INT CHECK (quantity >= 0), order_code TEXT UNIQUE
D. quantity INT, order_code TEXT NOT NULL UNIQUE CHECK (order_code IS NOT NULL)
Solution
Step 1: Ensure quantity is positive integer
CHECK (quantity > 0) enforces positive values only, better than >= 0 which allows zero.
Step 2: Ensure order_code is unique and not empty
UNIQUE prevents duplicates; NOT NULL disallows empty; this combination ensures order_code is unique and present.
Final Answer:
quantity INT CHECK (quantity > 0), order_code TEXT UNIQUE NOT NULL -> Option A
Quick Check:
CHECK positive + UNIQUE NOT NULL = correct constraints [OK]
Hint: Use CHECK for positive, UNIQUE + NOT NULL for unique non-empty [OK]
Common Mistakes:
Allowing zero quantity
Missing NOT NULL on unique column
Using CHECK without NOT NULL for order_code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.