Open links in new tab
  1. A foreign key in MySQL establishes a relationship between two tables, ensuring referential integrity. It links a column in the child table to the primary key in the parent table.

    Example: Creating a Foreign Key

    CREATE TABLE categories (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL
    );

    CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    category_id INT,
    name VARCHAR(255) NOT NULL,
    FOREIGN KEY (category_id) REFERENCES categories(id)
    );
    Copied!

    In this example:

    • The categories table is the parent table with id as its primary key.

    • The products table is the child table, where category_id is a foreign key referencing categories(id).

    Adding a Foreign Key to an Existing Table

    If the child table already exists, you can add a foreign key using ALTER TABLE:

    ALTER TABLE products
    ADD CONSTRAINT fk_category
    FOREIGN KEY (category_id) REFERENCES categories(id);
    Copied!

    Important Considerations

    • Both tables must use the same storage engine (e.g., InnoDB).

    • The data types of the foreign key and referenced column must match.

    • Ensure the parent table exists before creating the foreign key.

    • Use ON DELETE CASCADE or ON DELETE SET NULL for cascading delete behavior if needed.

  1. 15.1.20.5 FOREIGN KEY Constraints - MySQL

    MySQL supports foreign keys, which permit cross-referencing related data across tables, and foreign key constraints, which help keep the related data consistent.

  2. MySQL FOREIGN KEY Constraint - W3Schools

    The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in …

    Code sample

    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
    REFERENCES Persons(PersonID)...
  3. MySQL FOREIGN KEY Constraint - GeeksforGeeks

    Jul 23, 2025 · In this article, we will learn about how to use foreign keys in MySQL with examples. The FOREIGN KEY creates a relationship between the columns in the current table or let's say …

  4. MySQL Foreign Key

    This tutorial introduces you to MySQL foreign key constraints and shows you how to manage foreign keys effectively.

  5. How to View All Foreign Key Constraints for an Entire MySQL …

    5 days ago · Foreign key (FK) constraints are the backbone of relational databases, ensuring data integrity by enforcing relationships between tables. They define how columns in one table (the …

  6. MySQL Foreign Keys: How and why with examples | DoltHub Blog

    Mar 5, 2025 · In this tutorial, learn how to use foreign keys in MySQL and what they're good for, with examples

  7. People also ask
  8. What is a proper naming convention for MySQL FKs?

    Feb 10, 2010 · Oracle & SQL Server allow you to disable specific constraints. If you don't have fk in the name, you have to confirm the constraint is a …

  9. Working with FOREIGN KEY in MySQL 8: A Developer’s Guide

    Jan 27, 2024 · In this developer’s guide, we will delve into the use of FOREIGN KEY constraints in MySQL 8, covering everything from the basics to more advanced topics with practical examples …

  10. Mastering Foreign Keys in MySQL: A …

    Jun 4, 2025 · In MySQL, most of us are familiar with primary keys and their main role in uniquely identifying rows within a table. However, foreign keys …

  11. A quick overview of MySQL foreign key with …

    In this article, we will learn how to work with MySQL foreign keys with a few examples. A typical data modeling process starts with conceptual data …