How to Use the MySQL Foreign Key Constraint?


How to Use the MySQL Foreign Key Constraint

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

Databases are foundational to many technologies; understanding their basic operations is critical for succeeding in an information technology interview. This article will discuss foreign keys, specifically the MySQL foreign key constraint, and how they are used when mapping a database.

What Is the MySQL Foreign Key Constraint?

  • The MySQL foreign key constraint is a field which allows the programmer to link two tables.
  • The foreign key must reference the?primary key?of another table.
  • Adding a foreign key to a table creates a?parent-child relationship?between the two; the table containing the foreign key is the child and the table with the primary key is the parent.

When to Use the MySQL Foreign Key Constraint

A foreign key should be used when there is a?parent-child relationship?between two tables in the database. The foreign key allows you to reference one table by using a field in another. Consequently, using a foreign key ensures referential integrity within the database.

How to Implement the MySQL Foreign Key Constraint

The syntax for creating a foreign key in MySQL is: ?Foreign Key (field_name) references parent_table_name(primary_key_name).? When creating tables, the foreign key is listed at the end of the field declarations. The table containing the primary key must be defined before the child table. The default MySQL database engine does not support foreign keys; however, another engine, InnoDB, provides this functionality.

What Happens If the Parent Row Is Updated/Deleted?

MySQL gives programmers some options on how to handle updates to/deletions from the parent table. There are four options for ON DELETE:

  • RESTRICT: This is the default. If the user attempts to delete the parent row’s primary key and a child row exists, the delete operation will no be allowed.
  • NO ACTION: Same as the above.
  • CASCADE: Deleting the parent row’s primary key will also delete the child row.
  • SET NULL: Deleting the parent row’s primary key sets the foreign key on the child to NULL.

ON UPDATE has a nearly identical set of options:

  • RESTRICT: This is the default. If the user attempts to update the parent row’s primary key and a child row exists, the update operation will no be allowed.
  • NO ACTION: Same as the above.
  • CASCADE: Updating the parent row’s primary key updates the foreign key on the child.
  • SET NULL: Updating the parent row’s primary key sets the foreign key on the child to NULL.

Conclusion

The MySQL foreign key constraint allows programmers to define parent-child relationships between tables in a MySQL database. Programmers can also define various constraints on the behavior of the child if the parent is modified.

If you found this article helpful or if you have anything to add, please share your thoughts in the comment section.

Recent Posts