Tutorials Logic, IN info@tutorialslogic.com

MySQL Constraints PRIMARY KEY, FOREIGN KEY, UNIQUE

Database Invariants

MySQL constraints enforce rules for every writer, including scripts and future services. PRIMARY KEY identifies a row, UNIQUE prevents duplicate key values, NOT NULL requires a value, CHECK validates a condition in supported versions, and FOREIGN KEY protects referenced relationships under the chosen storage engine.

Database Integrity Contract

Applications can have bugs, imports can be messy, and multiple services may write to one database. Constraints keep essential rules close to the data.

  • Primary keys identify rows.
  • Foreign keys protect relationships.
  • Unique constraints prevent duplicates.
  • CHECK and NOT NULL protect valid state.

Foreign Keys and Cascades

Foreign keys prevent orphan records. Cascades define what happens to children when parents change, so the choice must match the business rule.

  • Use RESTRICT when history must remain.
  • Use CASCADE only when child data has no independent value.
  • Index foreign key columns.

Migration Safety

Adding a constraint to an existing table can fail if old rows violate the rule. Production migrations should audit, backfill, then enforce.

  • Find duplicate values before UNIQUE.
  • Backfill before NOT NULL.
  • Test rollback plans for large tables.

Constraint Design

Choose constraint names that identify the table and rule so production errors are diagnosable. A UNIQUE constraint involving nullable columns follows MySQL null semantics and may allow multiple rows with NULL; use NOT NULL when absence is not valid.

Define foreign-key update and delete actions from domain meaning. CASCADE is useful for owned dependent rows but dangerous when relationships are not true ownership. Add constraints after cleaning existing data, and handle violations as expected conflicts rather than generic server failures.

Constraint Semantics

Constraint Protects Important Boundary
PRIMARY KEY One stable row identity Implies uniqueness and NOT NULL; one per table.
UNIQUE Candidate-key uniqueness Nullable columns can allow multiple NULL values.
NOT NULL Required presence Does not reject an empty string or zero.
CHECK A row-level Boolean rule A check that evaluates to UNKNOWN is not the same as FALSE.
FOREIGN KEY Referenced-row existence Actions must match ownership and both sides need compatible definitions.
DEFAULT Value when a column is omitted It is not validation and does not replace an explicit NULL.

Constraint Rollout

Before enforcing a new rule, query violations, decide whether to repair or quarantine them, and test the DDL against production-like volume. Large table changes can lock work or rebuild storage depending on version and operation, so inspect the execution algorithm and maintenance window.

Applications should map duplicate, null, check, and referential violations to stable domain responses. Do not parse localized error prose when a driver exposes an error code or SQLSTATE, and do not reveal internal table or constraint details to an untrusted client.

Order Integrity Rules

Order Integrity Rules
CREATE TABLE customers (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE orders (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  customer_id BIGINT NOT NULL,
  total DECIMAL(10,2) NOT NULL CHECK (total >= 0),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Add a Business Rule as a Check Constraint

The database rejects invalid date ranges regardless of which application performs the write.

Add a Business Rule as a Check Constraint
ALTER TABLE subscriptions
  ADD CONSTRAINT chk_subscription_dates
  CHECK (ends_at IS NULL OR ends_at >= starts_at);

INSERT INTO subscriptions (starts_at, ends_at)
VALUES ('2026-07-14', '2026-07-01');
Output
The INSERT is rejected because ends_at is before starts_at.
  • Validate existing rows before adding the constraint to a populated table.
Before you move on

Integrity Rule Review

5 checks
  • Explain what each constraint prevents.
  • Choose cascade behavior carefully.
  • Audit existing data before adding constraints.
  • Name constraints for diagnosable production errors.
  • Test concurrent writers against uniqueness and relationships.

Constraint Design Failures

  • UNIQUE nullable email

    Add NOT NULL when every row must have one distinct email.
  • CASCADE chosen by convenience

    Derive delete behavior from whether the child is truly owned.
  • Dirty data ignored before DDL

    Audit and backfill violations before enforcing the rule.
  • Application validation treated as enough

    Keep the invariant in the database to protect every writer.

MySQL Constraints Questions Learners Ask

Yes. Constraints protect data from bugs, scripts, imports, and other applications.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.