Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (55.6k points)

Can anyone explain the foreign key in MySQL?

1 Answer

0 votes
by (119k points)

In MySQL, a FOREIGN KEY is used to connect two tables together.

A FOREIGN KEY is a field or a set of fields in one table that refers to the PRIMARY KEY of another table.

The table having a foreign key is called the referring or child table, and the table has the candidate key is called the referenced or parent table.

Here is the syntax to use the foreign key in a table:

CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

If you want to learn MySQL from top professionals, then sign up for this SQL Certification program by Intellipaat.

Also, watch this video on MySQL:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...