Skip to content
Learn Netverks

Lesson

Step 14/36 39% through track

constraints-mysql

Constraints in MySQL

Last reviewed Jun 1, 2026 Content v20260601
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Constraints in MySQL: the SQL patterns, schema habits, and query reasoning you need before advancing in MySQL.

Teams query Constraints in MySQL on every MySQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply Constraints in MySQL in contexts like: Web apps on shared hosting, ecommerce, and many startups’ first production DB.

Copy MySQL SQL into the mysql client, local MySQL/MariaDB, or DB Fiddle (MySQL dialect)—use DESCRIBE and EXPLAIN where lessons show them. The in-browser lab ships later; mysql client is the practice path now.

When you can explain the previous lesson's ideas without copying example queries verbatim.

PRIMARY KEY, UNIQUE, NOT NULL, CHECK (8.0.16+), and FOREIGN KEY on InnoDB enforce data integrity beyond application code.

Constraints example

CREATE TABLE invoices (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  email VARCHAR(255) NOT NULL,
  total DECIMAL(12,2) NOT NULL CHECK (total >= 0),
  UNIQUE KEY uq_invoices_email (email)
) ENGINE=InnoDB;

Practice: Run on practice.

CHECK enforcement

Older MySQL ignored CHECK—verify version; still validate in app for portability.

Important interview questions and answers

  1. Q: UNIQUE on email?
    A: Prevents duplicate accounts at storage layer.
  2. Q: NOT NULL?
    A: SQL rejects missing values—pair with sensible DEFAULT where optional.

Self-check

  1. What does CHECK total >= 0 do?
  2. Why UNIQUE on email?

Tip: CHECK constraints need MySQL 8.0.16+—still validate in app.

Interview prep

UNIQUE email?

Prevents duplicate emails at database layer.

CHECK MySQL 8?

Enforced from 8.0.16+—verify version.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • CHECK version?
  • UNIQUE email?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump