Introduction
This article will explore the basics of foreign key constraints in MySQL, including what they are, why they are essential, and how to create and manage them. We'll also cover some common issues that occur when working with foreign keys and how to handle them.
What is MySQL?
MySQL is an acronym for My Structured Query Language and a database management system. In this MySQL, SQL is a programming language used to work with data in relational databases. SQL is the most common standardized language used to access databases. To better understand MySQL, you can read this article- Introduction To MySQL. And to learn how to work in MySQL Workbench- How to Use MySQL Workbench.
What is Foreign Key in MySQL?
A foreign key in MySQL is a column or a group of columns in a table that refers to the primary key of another table. It establishes a relationship between two tables in a database, which helps maintain data integrity and consistency.
Example.
- If you have two tables in your database i.e. Employee and Department, and you want to link them, you can create a foreign key in the Department table that refers to the primary key in the Employee table. This will allow you to track which Employee is placed in a particular Department and ensure that the data remains accurate and consistent.
- In MySQL, you can create a foreign key constraint using the FOREIGN KEY keyword when defining a table. This constraint ensures that the data in the foreign key column matches the data in the primary key column of the referenced table.
Unlocking the Power of Foreign Keys in MySQL
Foreign keys play an important role in creating a well-structured database that can maintain data integrity and provide efficient access to data across related tables.
- A foreign key establishes a relationship between two tables in a MySQL database.
- The foreign key is a column or a set of columns in one table that references the primary key of another table.
- The foreign key helps ensure data integrity by enforcing referential integrity between related tables.
- With a foreign key, you can create a link between the data in two tables, which allows you to retrieve and combine data from both tables with a single query.
- Foreign keys can be created using the ALTER TABLE statement, either when creating a table or by modifying an existing table.
Understanding Foreign Key Constraints in MySQL
In MySQL, a foreign key constraint stops you from deleting the records of the parent row. Simply it means parent records cannot be deleted if the child records are present. A foreign key is to ensure data consistency across linked tables. It contains five different referential options, which specify what type of action should be taken when a user tries to delete or update a row related to another table. These referential options are,
- CASCADE- When a row in the Master table is deleted or updated, the corresponding rows in the child table are also deleted or updated, respectively.
- SET NULL- When a row in the Master table is deleted or updated, the corresponding values in the child table are set to NULL.
- RESTRICT- This prevents the deletion or updation of a row in the Master table if there are corresponding rows in the child table.
- SET DEFAULT- When a row in the Master table is deleted or updated, then the corresponding values in the child table are set to their default values.
- NO ACTION- This option is similar to RESTRICT but does not generate an error message. Instead, the action is silently ignored.
Note
- Master table- The data of this table is not changed frequently.
- Child table- The data of this table changes frequently.
Let's understand this better by taking an example with the help of MySQL.
create database school;
Created a database and named it a school.
use school;
Here, use is a keyword in MySQL to use a database.
create table student (studentId int not null primary key auto_increment,
studentName varchar(50), studentAddress varchar(100));
create table teacher (teacherId int not null primary key auto_increment,
teacherName varchar(50), studentId int,
foreign key (studentId) references student (studentId));
We then created a Master table named student and a child table named teacher. This student table will create a table with columns studentId, studentName, and studentAddress, where studentId is an integer that can not be null and will increment automatically and be designated as the primary key for this student table. And studentName and studentAddress are strings that can be null.






YuvPosted May 10, 2023, 2:54 AM
Praiseworthy!