How to add foreign key in MySQL

MySQL is an open-source relational database management system that allows users to store and manage their data efficiently. One of the essential components of MySQL is the ability to establish relationships between tables. This relationship is achieved through the use of foreign keys. In this article, we will explore what foreign keys are, why they are essential, and how to add foreign keys in MySQL.

What is a Foreign Key in MySQL?

A foreign key is a column or a set of columns that refer to the primary key of another table. It is used to establish a relationship between two tables. In other words, a foreign key is a way to ensure that the data in one table is linked to the data in another table. The foreign key is used to maintain data integrity and consistency across tables.

Why are Foreign Keys Important in MySQL?

Foreign keys are essential in MySQL for several reasons. foreign keys help to maintain data integrity. They ensure that the data in one table is linked to the data in another table. This ensures that the data is consistent and accurate across tables.

Foreign keys help to enforce referential integrity. Referential integrity is a set of rules that ensure that the data in one table is consistent with the data in another table. Foreign keys are used to enforce these rules and ensure that the data is accurate and consistent across tables.

Lastly, foreign keys help to improve the performance of the database. When foreign keys are used, the database can perform joins more efficiently. This is because the database can use the foreign key to quickly look up data in another table.

How to Add Foreign Key in MySQL?

Now that we understand what foreign keys are and why they are essential, let’s explore how to add foreign keys in MySQL. To add a foreign key to a table, we need to follow these steps:

Step 1: Create the Parent Table

The parent table is the table that contains the primary key that the foreign key will reference. To create the parent table, we need to use the CREATE TABLE statement. Here is an example:

“`

CREATE TABLE parent_table (

Id INT NOT NULL PRIMARY KEY,

Name VARCHAR(50) NOT NULL

);

“`

In the above example, we have created a parent table with two columns: id and name. The id column is the primary key.

Step 2: Create the Child Table

The child table is the table that will contain the foreign key. To create the child table, we need to use the CREATE TABLE statement. Here is an example:

“`

CREATE TABLE child_table (

Id INT NOT NULL PRIMARY KEY,

Parent_id INT NOT NULL,

Name VARCHAR(50) NOT NULL,

FOREIGN KEY (parent_id) REFERENCES parent_table(id)

);

“`

In the above example, we have created a child table with three columns: id, parent_id, and name. The id column is the primary key, and the parent_id column is the foreign key. The FOREIGN KEY constraint specifies that the parent_id column references the id column in the parent_table.

Step 3: Insert Data into the Parent Table

Before we can insert data into the child table, we need to insert data into the parent table. Here is an example:

“`

INSERT INTO parent_table (id, name)

VALUES (1, ‘John’),

(2, ‘Mary’),

(3, ‘Jane’);

“`

In the above example, we have inserted three rows into the parent table.

Step 4: Insert Data into the Child Table

Now that we have inserted data into the parent table, we can insert data into the child table. Here is an example:

“`

INSERT INTO child_table (id, parent_id, name)

VALUES (1, 1, ‘John Jr.’),

(2, 2, ‘Mary Jr.’),

(3, 3, ‘Jane Jr.’);

“`

In the above example, we have inserted three rows into the child table. Note that the parent_id column references the id column in the parent table.

Step 5: Query the Data

Now that we have inserted data into both tables, we can query the data. Here is an example:

“`

SELECT child_table.id, child_table.name, parent_table.name

FROM child_table

INNER JOIN parent_table ON child_table.parent_id = parent_table.id;

“`

In the above example, we have used an INNER JOIN to join the child table and the parent table. The ON clause specifies that the parent_id column in the child table matches the id column in the parent table. The SELECT statement retrieves the id and name columns from the child table and the name column from the parent table.

Foreign keys are essential in MySQL as they help to maintain data integrity, enforce referential integrity, and improve database performance. Adding foreign keys to tables is a straightforward process that involves creating the parent table, creating the child table, inserting data into the parent table, inserting data into the child table, and querying the data. By following these steps, we can ensure that our data is accurate and consistent across tables.