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.