Keys and Constraints in MySQL

Published July 26, 2025 By Sai Sree Ram Gundepudi

Primary Key

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

Foreign Key

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Other Constraints

  • NOT NULL
  • UNIQUE
  • DEFAULT

Was this solution helpful?

Your feedback helps improve our technical knowledge repository.

Share this article