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,
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: