0 votes
1 view
in SQL by (36k points)

Can anyone explain the foreign key in MySQL?

1 Answer

0 votes
by (86.8k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...