How to Insert Foreign Key Value into Table in MySQL

MySQL is a widely used open-source relational database management system that provides a flexible and scalable solution for storing and managing data. One of the key features of MySQL is its ability to create relationships between tables using foreign keys. In this article, we will explore how to insert foreign key values into tables in MySQL.

Understanding Foreign Keys

Before we dive into the process of inserting foreign key values into a table in MySQL, let’s have a quick refresher on what foreign keys are and how they work. A foreign key is a column or a set of columns in a table that refers to the primary key of another table. The primary key of the referenced table is called the parent key, and the foreign key in the referring table is called the child key.

The purpose of foreign keys is to create relationships between tables, which enforce referential integrity. Referential integrity ensures that the data in the child table is consistent with the data in the parent table. In other words, foreign keys prevent the insertion of invalid data into the child table, which could cause data inconsistencies and errors.

Inserting Foreign Key Values into a Table

To insert foreign key values into a table in MySQL, we need to follow a few simple steps. Let’s assume that we have two tables, “orders” and “customers,” and we want to insert a foreign key value into the “orders” table that refers to the “customers” table.

Step 1: Create the Tables

First, we need to create the two tables. Here is the SQL code to create the “customers” table:

CREATE TABLE customers (

Id INT PRIMARY KEY,

Name VARCHAR(50),

Email VARCHAR(50)

);

And here is the SQL code to create the “orders” table:

CREATE TABLE orders (

Id INT PRIMARY KEY,

Customer_id INT,

Order_date DATE,

FOREIGN KEY (customer_id) REFERENCES customers(id)

);

In the “customers” table, we have three columns: “id,” “name,” and “email.” The “id” column is the primary key, which is a unique identifier for each customer. In the “orders” table, we have three columns as well: “id,” “customer_id,” and “order_date.” The “id” column is the primary key for the “orders” table, and the “customer_id” column is the foreign key that refers to the “id” column in the “customers” table.

Notice that we have included a FOREIGN KEY constraint on the “customer_id” column in the “orders” table. This constraint ensures that the values inserted into the “customer_id” column are valid references to the “id” column in the “customers” table.

Step 2: Insert Data into the Tables

Next, we need to insert some data into the two tables. Here is the SQL code to insert a row into the “customers” table:

INSERT INTO customers (id, name, email) VALUES (1, ‘John Doe’, ‘johndoe@email.com’);

And here is the SQL code to insert a row into the “orders” table:

INSERT INTO orders (id, customer_id, order_date) VALUES (1, 1, ‘2022-01-01’);

Notice that we have inserted the value “1” into the “customer_id” column in the “orders” table. This value refers to the “id” column in the “customers” table, which has a value of “1” for the same customer.

Step 3: Verify the Data

We need to verify that the data has been inserted correctly into both tables. Here is the SQL code to select all the rows from the “customers” table:

SELECT * FROM customers;

And here is the SQL code to select all the rows from the “orders” table:

SELECT * FROM orders;

When we run these two queries, we should see the following output:

Customers table:

| id | name | email |

+—-+———–+———————–+

| 1 | John Doe | johndoe@email.com |

Orders table:

| id | customer_id | order_date |

+—-+————-+—————-+

| 1 | 1 | 2022-01-01 |

As we can see, the data has been inserted correctly into both tables, and the foreign key constraint has been enforced to ensure that the value in the “customer_id” column in the “orders” table refers to a valid customer in the “customers” table.

Inserting foreign key values into a table in MySQL is a straightforward process that involves creating the tables, inserting data into the tables, and verifying the data. By creating relationships between tables using foreign keys, we can ensure referential integrity and avoid data inconsistencies and errors. With the steps outlined in this article, you should be able to insert foreign key values into tables in MySQL with ease.