0
0
MySQLquery~20 mins

Column definitions and constraints in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Constraints Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this CREATE TABLE statement?
Consider the following MySQL table creation code:
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)
);
ATable is created successfully with a check constraint on Age.
BSyntax error because CHECK constraints are not supported in MySQL.
CTable is created but the CHECK constraint is ignored.
DError because AUTO_INCREMENT must be on a PRIMARY KEY column.
Attempts:
2 left
💡 Hint
MySQL supports CHECK syntax but does not enforce it before version 8.0.16.
🧠 Conceptual
intermediate
1: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?
Aemail VARCHAR(100) UNIQUE NOT NULL
Bemail VARCHAR(100) NOT NULL
Cemail VARCHAR(100) UNIQUE
Demail VARCHAR(100) PRIMARY KEY
Attempts:
2 left
💡 Hint
Think about what constraints ensure uniqueness and disallow NULLs.
📝 Syntax
advanced
2: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
);
APrice DECIMAL(10, 2) NOT NULL CHECK (Price > 0)
BPrice DECIMAL(10, 2) NOT NULL CHECK Price > 0
CPrice DECIMAL(10, 2) NOT NULL CHECK (Price > 0) COMMENT 'Price must be positive'
DPrice DECIMAL(10, 2) NOT NULL
Attempts:
2 left
💡 Hint
CHECK constraints require parentheses around the condition.
🔧 Debug
advanced
2:00remaining
Why does this table creation fail?
Given this MySQL table creation code:
CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT,
CustomerID INT NOT NULL,
PRIMARY KEY (CustomerID)
);

Why does this code cause an error?
APRIMARY KEY cannot be on CustomerID because it is NOT NULL.
BCustomerID cannot be PRIMARY KEY because it is not AUTO_INCREMENT.
CAUTO_INCREMENT cannot be used without UNIQUE constraint.
DAUTO_INCREMENT column must be part of the PRIMARY KEY.
Attempts:
2 left
💡 Hint
AUTO_INCREMENT columns must be indexed as PRIMARY KEY or UNIQUE.
🧠 Conceptual
expert
2: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?
Acol INT UNSIGNED UNIQUE CHECK (col > 0)
Bcol INT CHECK (col > 0) UNIQUE
Ccol INT PRIMARY KEY CHECK (col > 0)
Dcol INT UNSIGNED PRIMARY KEY
Attempts:
2 left
💡 Hint
UNSIGNED means zero or positive; CHECK enforces greater than zero; UNIQUE enforces uniqueness.