Challenge - 5 Problems
Column Constraints Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this CREATE TABLE statement?
Consider the following MySQL table creation code:
What will happen when this code runs in MySQL 8.0?
CREATE TABLE Employees (
ID INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 18),
PRIMARY KEY (ID)
);
What will happen when this code runs in MySQL 8.0?
MySQL
CREATE TABLE Employees ( ID INT NOT NULL AUTO_INCREMENT, Name VARCHAR(50) NOT NULL, Age INT CHECK (Age >= 18), PRIMARY KEY (ID) );
Attempts:
2 left
💡 Hint
MySQL supports CHECK syntax but does not enforce it before version 8.0.16.
✗ Incorrect
In MySQL versions before 8.0.16, CHECK constraints are parsed but ignored. The table will be created, but the Age check will not be enforced.
🧠 Conceptual
intermediate1:30remaining
Which column definition enforces uniqueness and non-null values?
You want to create a column in MySQL that must always have a unique value and cannot be NULL. Which of the following column definitions achieves this?
Attempts:
2 left
💡 Hint
Think about what constraints ensure uniqueness and disallow NULLs.
✗ Incorrect
UNIQUE ensures values are unique, NOT NULL disallows NULLs. PRIMARY KEY also enforces both but is usually used for main identifiers.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this column definition
Which option contains a syntax error in the column definition for a MySQL table?
MySQL
CREATE TABLE Products ( ProductID INT PRIMARY KEY, Price DECIMAL(10, 2) NOT NULL CHECK Price > 0, Name VARCHAR(100) NOT NULL );
Attempts:
2 left
💡 Hint
CHECK constraints require parentheses around the condition.
✗ Incorrect
In MySQL, the CHECK constraint condition must be enclosed in parentheses. Option B misses parentheses causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this table creation fail?
Given this MySQL table creation code:
Why does this code cause an error?
CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT,
CustomerID INT NOT NULL,
PRIMARY KEY (CustomerID)
);
Why does this code cause an error?
Attempts:
2 left
💡 Hint
AUTO_INCREMENT columns must be indexed as PRIMARY KEY or UNIQUE.
✗ Incorrect
In MySQL, an AUTO_INCREMENT column must be defined as a PRIMARY KEY or have a UNIQUE index. Here, OrderID is AUTO_INCREMENT but not a key, causing an error.
🧠 Conceptual
expert2:30remaining
Which constraint combination ensures a column stores only positive integers and is unique?
You want a column in MySQL that stores only positive integers (greater than zero) and each value must be unique. Which combination of column definition and constraints achieves this?
Attempts:
2 left
💡 Hint
UNSIGNED means zero or positive; CHECK enforces greater than zero; UNIQUE enforces uniqueness.
✗ Incorrect
INT UNSIGNED allows zero and positive numbers; CHECK (col > 0) restricts to strictly positive; UNIQUE enforces no duplicates. PRIMARY KEY also enforces uniqueness and NOT NULL but does not guarantee positive values without CHECK.