SQL Database Constraint Rules Quiz
A 50-question quiz covering NOT NULL, UNIQUE, CHECK, DEFAULT, primary keys, foreign keys, and constraint logic.
Question 1
A signup page requires every user to provide an email. The database column storing emails must never allow empty or missing entries. Which constraint ensures this rule?
Question 2
A company ensures that no two employees share the same work email. Which constraint enforces this uniqueness across all rows?
Question 3
A salary column must always hold a positive number. Which constraint ensures only valid values are stored?
Question 4
A new row should automatically receive today’s date when no creation date is provided. Which constraint supplies this automatic value?
Question 5
A product table uses an ID column that uniquely identifies each product. It may not be NULL or duplicated. Which concept describes this requirement?
Question 6
A foreign key prevents an order from referencing a non-existing customer. What concept describes this protection?
Question 7
A system demands that age values be at least 18 for certain accounts. What type of rule enforces this?
Question 8
A table definition requires every product code to be unique so that scanning devices identify items correctly. What constraint supports this?
Question 9
A system wants to prevent negative stock values. Which constraint is appropriate?
Question 10
A reminders app must always store a timestamp for each reminder. Which constraint ensures the timestamp cannot be omitted?
Question 11
A price value should always be non-negative and not null. Which combination correctly applies both rules?
Question 12
A DEFAULT value for a status field sets new entries to 'pending' unless overridden. What advantage does this bring?
Question 13
A foreign key constraint is added to ensure product reviews always refer to a valid product. This prevents what?
Question 14
A UNIQUE constraint on a username field ensures what important behavior?
Question 15
A CHECK constraint validates that a temperature reading stays within realistic limits. What role does this play?
Question 16
A NOT NULL column is added to an existing table. What challenge can arise when applying this constraint to existing data?
Question 17
CHECK constraints help enforce rules like 'quantity must be positive.' What kind of data issue does this prevent?
Question 18
A user table includes a UNIQUE constraint on phone numbers. What does this allow the system to do reliably?
Question 19
A DEFAULT of true is added to an 'is_active' flag so newly created accounts begin active. What advantage does this provide?
Question 20
A uniqueness rule on (first_name, last_name, birthday) is applied to ID verification. What type of key does this combination form?
Question 21
If a column has a DEFAULT value but also allows NULL, what happens when a user explicitly inserts NULL?
Question 22
A database uses a CHECK constraint requiring delivery dates to be on or after order dates. What does this prevent?
Question 23
A NOT NULL column paired with DEFAULT ensures what behavior for new rows?
Question 24
A UNIQUE constraint still allows NULL for a nullable column. Why?
Question 25
A CHECK constraint on a role-level column ensures only allowed values such as 'admin', 'viewer', or 'editor' appear. What does this enforce?
Question 26
This definition enforces that names cannot be empty. Which rule applies?
CREATE TABLE people (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);Question 27
This rule ensures no two users share the same email. What does it enforce?
CREATE TABLE users (
email TEXT UNIQUE
);Question 28
This constraint enforces that an item's price cannot be zero. What rule is applied?
CREATE TABLE items (
price DECIMAL CHECK (price > 0)
);Question 29
This column receives a fallback value. What effect does this DEFAULT create?
CREATE TABLE tasks (
status TEXT DEFAULT 'pending'
);Question 30
This definition adds a NOT NULL rule to a birthdate column. What must inserts do?
CREATE TABLE employees (
birthdate DATE NOT NULL
);Question 31
This constraint prevents invalid rating values. What does the CHECK enforce?
CREATE TABLE ratings (
score INT CHECK (score BETWEEN 1 AND 5)
);Question 32
This column enforces uniqueness on user names. What happens if a duplicate is inserted?
CREATE TABLE accounts (
username TEXT UNIQUE
);Question 33
This rule ensures stock values cannot become negative. What logic is used?
CREATE TABLE inventory (
stock INT CHECK (stock >= 0)
);Question 34
This DEFAULT assigns a current timestamp to new rows. What outcome results?
CREATE TABLE logs (
created_at TIMESTAMP DEFAULT NOW()
);Question 35
This line defines a foreign key. What requirement does it enforce?
project_id INT REFERENCES projects(id)Question 36
This check ensures email addresses contain an '@'. What logic ensures this?
CHECK (email LIKE '%@%')Question 37
This constraint requires start_date to be before end_date. What effect does it produce?
CHECK (start_date < end_date)Question 38
This rule ensures that two values together must remain unique. What constraint is used?
UNIQUE (student_id, course_id)Question 39
This rule sets a default quantity. What happens on new inserts?
quantity INT DEFAULT 1Question 40
This definition blocks deleting referenced customers. What behavior occurs?
customer_id INT REFERENCES customers(id) ON DELETE RESTRICTQuestion 41
This command inserts a NULL into a NOT NULL field. What occurs?
INSERT INTO people (name) VALUES (NULL);Question 42
This CHECK rule ensures numeric values stay below a limit. What does it guarantee?
CHECK (score <= 100)Question 43
This DEFAULT uses NOW(). What behavior does it introduce?
created_at TIMESTAMP DEFAULT NOW()Question 44
This constraint ties a contact to a valid user. What rule does it define?
user_id INT REFERENCES users(id)Question 45
This CHECK ensures discount percentages stay within 0–50. What rule is applied?
CHECK (discount >= 0 AND discount <= 50)Question 46
This rule stops duplicate category names. What happens if two inserts attempt the same name?
category_name TEXT UNIQUEQuestion 47
This rule blocks invalid email formats. What does the CHECK accomplish?
CHECK (email LIKE '%@%')Question 48
This default assigns the value 'draft' to new posts. What result does this have?
status TEXT DEFAULT 'draft'Question 49
This CHECK ensures ages are realistic. What is being enforced?
CHECK (age BETWEEN 0 AND 120)Question 50
This rule allows a NULL foreign key when a project is removed. Which behavior applies?
project_id INT REFERENCES projects(id) ON DELETE SET NULL