0
0
SQLquery~20 mins

Why constraints matter in SQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constraint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of UNIQUE constraint on duplicate inserts

Consider a table users with a username column that has a UNIQUE constraint. What happens when you try to insert a duplicate username?

SQL
CREATE TABLE users (id INT PRIMARY KEY, username VARCHAR(50) UNIQUE);
INSERT INTO users VALUES (1, 'alice');
INSERT INTO users VALUES (2, 'alice');
AThe second insert is ignored silently without error.
BThe second insert fails with a UNIQUE constraint violation error.
CThe second insert succeeds and creates a duplicate username.
DThe second insert succeeds and overwrites the first row.
Attempts:
2 left
💡 Hint

Think about what UNIQUE means for a column.

🧠 Conceptual
intermediate
1:30remaining
Purpose of NOT NULL constraint

Why do we use the NOT NULL constraint on a database column?

ATo speed up queries on that column.
BTo allow the column to store multiple values in one cell.
CTo automatically generate unique values for the column.
DTo ensure the column always has a value and never stores NULL.
Attempts:
2 left
💡 Hint

Think about what NULL means in a database.

📝 Syntax
advanced
2:00remaining
Identify the correct FOREIGN KEY constraint syntax

Which option correctly adds a FOREIGN KEY constraint to the orders table referencing customers(id)?

SQL
ALTER TABLE orders ADD CONSTRAINT fk_customer_id ... ;
AFOREIGN KEY (customer_id) REFERENCES customers(id);
BFOREIGN KEY customer_id REFERENCES customers(id);
CFOREIGN KEY (customer_id) REF customers(id);
DFOREIGN KEY customer_id REF customers(id);
Attempts:
2 left
💡 Hint

Check the correct syntax for FOREIGN KEY in SQL.

query_result
advanced
2:00remaining
Result of violating CHECK constraint

Given a table products with a CHECK constraint price > 0, what happens if you try to insert a product with price = -5?

SQL
CREATE TABLE products (id INT PRIMARY KEY, price DECIMAL CHECK (price > 0));
INSERT INTO products VALUES (1, -5);
AThe insert succeeds but sets price to 0 automatically.
BThe insert succeeds and stores the negative price.
CThe insert fails with a CHECK constraint violation error.
DThe insert is ignored silently without error.
Attempts:
2 left
💡 Hint

What does a CHECK constraint do?

🔧 Debug
expert
2:30remaining
Why does this PRIMARY KEY constraint fail?

Consider this table creation:

CREATE TABLE employees (
  id INT,
  name VARCHAR(100),
  PRIMARY KEY (name)
);

Why might this PRIMARY KEY constraint cause problems?

ABecause names may not be unique, violating the PRIMARY KEY uniqueness requirement.
BBecause PRIMARY KEY cannot be on a VARCHAR column.
CBecause the table must have an explicit id as PRIMARY KEY.
DBecause PRIMARY KEY columns cannot allow NULL values.
Attempts:
2 left
💡 Hint

Think about what PRIMARY KEY means for data uniqueness.