Keys and Constraints in MySQL

Published July 26, 2025 By Sai Sree Ram Gundepudi

Primary Key

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

Foreign Key

CREATE TABLE orders (
  id INT,
  user_id INT,
  FOREIGN KEY (user_id) REFERENCES users(id)
);

UNIQUE

CREATE TABLE students (
  email VARCHAR(255) UNIQUE
);

Was this solution helpful?

Your feedback helps improve our technical knowledge repository.

Share this article