How to check if foreign key exists in MySQL table?

Summary: in this tutorial, you will learn about MySQL foreign key and how to create, drop, and disable a foreign key constraint.

Show

Introduction to MySQL foreign key

A foreign key is a column or group of columns in a table that links to a column or group of columns in another table. The foreign key places constraints on data in the related tables, which allows MySQL to maintain referential integrity.

Let’s take a look at the following

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
5 and

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 tables from the sample database.

How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

In this diagram, each customer can have zero or many orders and each order belongs to one customer.

The relationship between

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
5 table and

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table is one-to-many. And this relationship is established by the foreign key in the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table specified by the

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0 column.

The

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0 column in the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table links to the

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0 primary key column in the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
5 table.

The

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
5 table is called the parent table or referenced table, and the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table is known as the child table or referencing table.

Typically, the foreign key columns of the child table often refer to the primary key columns of the parent table.

A table can have more than one foreign key where each foreign key references to a primary key of the different parent tables.

Once a foreign key constraint is in place, the foreign key columns from the child table must have the corresponding row in the parent key columns of the parent table or values in these foreign key column must be

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
7 (see the

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
8 action example below).

For example, each row in the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table has a

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0 that exists in the

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0 column of the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
5 table. Multiple rows in the

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
6 table can have the same

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
0.

Self-referencing foreign key

Sometimes, the child and parent tables may refer to the same table. In this case, the foreign key references back to the primary key within the same table.

See the following

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
5 table from the sample database.

How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

The

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
6 column is a foreign key that refers to the

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
7 column which is the primary key of the

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
5 table.

This relationship allows the

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
5 table to store the reporting structure between employees and managers. Each employee reports to zero or one employee and an employee can have zero or many subordinates.

The foreign key on the column

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)
6 is known as a recursive or self-referencing foreign key.

MySQL INSERT INTO products(productName, categoryId) VALUES('iPad',3);Code language: SQL (Structured Query Language) (sql)1 syntax

Here is the basic syntax of defining a foreign key constraint in the

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
2 or

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
3 statement:

[CONSTRAINT constraint_name] FOREIGN KEY [foreign_key_name] (column_name, ...) REFERENCES parent_table(colunm_name,...) [ON DELETE reference_option] [ON UPDATE reference_option]

Code language: SQL (Structured Query Language) (sql)

In this syntax:

First, specify the name of foreign key constraint that you want to create after the

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
4 keyword. If you omit the constraint name, MySQL automatically generates a name for the foreign key constraint.

Second, specify a list of comma-separated foreign key columns after the

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
1 keywords. The foreign key name is also optional and is generated automatically if you skip it.

Third, specify the parent table followed by a list of comma-separated columns to which the foreign key columns reference.

Finally, specify how foreign key maintains the referential integrity between the child and parent tables by using the

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
6 and

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
7 clauses.  The

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
8 determines action which MySQL will take when values in the parent key columns are deleted (

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
6) or updated (

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
7).

MySQL has five reference options:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
1,

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
8,

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
3,

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4, and

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5.

  • Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

    Code language: JavaScript (javascript)
    1: if a row from the parent table is deleted or updated, the values of the matching rows in the child table automatically deleted or updated.
  • SELECT * FROM categories;

    Code language: SQL (Structured Query Language) (sql)
    8:  if a row from the parent table is deleted or updated, the values of the foreign key column (or columns) in the child table are set to

    SELECT * FROM categories;

    Code language: SQL (Structured Query Language) (sql)
    7.
  • Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

    Code language: JavaScript (javascript)
    4:  if a row from the parent table has a matching row in the child table, MySQL rejects deleting or updating rows in the parent table.
  • Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

    Code language: JavaScript (javascript)
    3: is the same as

    Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

    Code language: JavaScript (javascript)
    4.
  • Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

    Code language: JavaScript (javascript)
    5: is recognized by the MySQL parser. However, this action is rejected by both InnoDB and NDB tables.

In fact, MySQL fully supports three actions:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4,

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
1 and

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
8.

If you don’t specify the

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
6 and

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
7 clause, the default action is

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4.

MySQL INSERT INTO products(productName, categoryId) VALUES('iPad',3);Code language: SQL (Structured Query Language) (sql)1 examples

Let’s create a new database called

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
0 for the demonstration.

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)Code language: JavaScript (javascript)4 & Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)Code language: JavaScript (javascript)3 actions

Inside the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
0 database, create two tables

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 and

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)

The

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table is the foreign key column that refers to the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 column in the 

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table.

Because we don’t specify any

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
7 and

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)
6 clauses, the default action is

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 for both update and delete operation.

The following steps illustrate the

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 action.

1) Insert two rows into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)

2) Select data from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

3) Insert a new row into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

INSERT INTO products(productName, categoryId) VALUES('iPhone',1);

Code language: SQL (Structured Query Language) (sql)

It works because the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 1 exists in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table.

4) Attempt to insert a new row into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table with a

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6  value does not exist in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

INSERT INTO products(productName, categoryId) VALUES('iPad',3);

Code language: SQL (Structured Query Language) (sql)

MySQL issued the following error:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)

5) Update the value in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 column in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table to

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
14:

UPDATE categories SET categoryId = 100 WHERE categoryId = 1;

Code language: SQL (Structured Query Language) (sql)

MySQL issued this error:

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)

Because of the

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 option, you cannot delete or update

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
16 since it is referenced by the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
17

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
18 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table.

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)Code language: JavaScript (javascript)1 action

These steps illustrate how

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
21 and

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
22 actions work.

1) Drop the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
0

2) Create the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table with the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
21 and

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
26 options for the foreign key:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
1

3) Insert four rows into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
2

4) Select data from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
3
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

5) Update

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 1 to 100 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
4

6) Verify the update:

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

7) Get data from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
3
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

As you can see, two rows with value

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
18 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 column of the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table were automatically updated to

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
14 because of the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
21 action.

8) Delete

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 2 from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
7

9) Verify the deletion:

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

10) Check the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
3
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

All products with

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 2 from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table were automatically deleted because of the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
26 action.

SELECT * FROM categories;Code language: SQL (Structured Query Language) (sql)8 action

These steps illustrate how the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
44 and

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
45 actions work.

1) Drop both

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 and

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 tables:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
0

2) Create the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 and

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 tables:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
1

The foreign key in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table changed to

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
44 and

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
45 options.

3) Insert rows into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
2

4) Insert rows into the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
2

5) Update

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 from 1 to 100 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
4

6) Verify the update:

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

7) Select data from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

The rows with the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 1 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table were automatically set to

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
7 due to the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
44 action.

8) Delete the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 2 from the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
4 table:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
6

9) Check the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
3
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

The values in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 column of the rows with

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
6 2 in the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table were automatically set to

SELECT * FROM categories;

Code language: SQL (Structured Query Language) (sql)
7 due to the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
45 action.

Drop MySQL foreign key constraints

To drop a foreign key constraint, you use the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
70 statement:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
8

In this syntax:

  • First, specify the name of the table from which you want to drop the foreign key after the

    CREATE DATABASE fkdemo; USE fkdemo;

    Code language: SQL (Structured Query Language) (sql)
    70 keywords.
  • Second, specify  the constraint name after the

    CREATE DATABASE fkdemo; USE fkdemo;

    Code language: SQL (Structured Query Language) (sql)
    72 keywords.

Notice that

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
73 is the name of the foreign key constraint specified when you created or added the foreign key constraint to the table.

To obtain the generated constraint name of a table, you use the

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
74 statement:

CREATE TABLE categories( categoryId INT AUTO_INCREMENT PRIMARY KEY, categoryName VARCHAR(100) NOT NULL ) ENGINE=INNODB; CREATE TABLE products( productId INT AUTO_INCREMENT PRIMARY KEY, productName varchar(100) not null, categoryId INT, CONSTRAINT fk_category FOREIGN KEY (categoryId) REFERENCES categories(categoryId) ) ENGINE=INNODB;

Code language: SQL (Structured Query Language) (sql)
9

For example, to see the foreign keys of the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table, you use the following statement:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
0

The following is the output of the statement:

How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

As you can see clearly from the output, the table

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table has one foreign key constraint:

CREATE DATABASE fkdemo; USE fkdemo;

Code language: SQL (Structured Query Language) (sql)
77

And this statement drops the foreign key constraint of the

Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)

Code language: JavaScript (javascript)
5 table:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
1

To ensure that the foreign key constraint has been dropped, you can view the structure of the products table:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
0
How to check if foreign key exists in MySQL table?
How to check if foreign key exists in MySQL table?

Disabling foreign key checks

Sometimes, it is very useful to disable foreign key checks e.g., when you import data from a CSV file into a table. If you don’t disable foreign key checks, you have to load data into a proper order i.e., you have to load data into parent tables first and then child tables, which can be tedious. However, if you disable the foreign key checks, you can load data into tables in any order.

To disable foreign key checks, you use the following statement:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
3

And you can enable it by using the following statement:

INSERT INTO categories(categoryName) VALUES ('Smartphone'), ('Smartwatch');

Code language: SQL (Structured Query Language) (sql)
4

In this tutorial, you have learned about the MySQL foreign key and how to create a foreign key constraint with various reference options.

How to check if foreign key exists in table MySQL?

To see foreign key relationships of a table: SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA. KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'db_name' AND REFERENCED_TABLE_NAME = 'table_name';

How do I know if a foreign key exists in a table?

Using SQL Server Management Studio.
Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu..
In the Foreign Key Relationships dialog box, select the relationship with properties you want to view..

How do I check if a foreign key exists in SQL?

You can use the OBJECTPROPERTY() function in SQL Server to check whether or not a table has one or more foreign key constraints.

How do you check if a value exists in a table in MySQL?

The syntax of the PostgreSQL EXISTS condition is as follows: WHERE EXISTS (subquery);.
SELECT column1, column2… columnN..
FROM table1..
WHERE [NOT] EXISTS (.
SELECT 1..
FROM table2..
WHERE column2= table1. column1..