W3Schools.com (2024)

SQL FOREIGN KEY Constraint

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.

Look at the following two tables:

Persons Table

PersonID LastName FirstName Age
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20

Orders Table

OrderID OrderNumber PersonID
1 77895 3
2 44678 3
3 22456 2
4 24562 1

Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons" table.

The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.

The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.

The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column,because it has to be one of the values contained in the parent table.

SQL FOREIGN KEY on CREATE TABLE

The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:

MySQL:

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

SQL Server / Oracle / MS Access:

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

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

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

SQL FOREIGN KEY on ALTER TABLE

To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD FOREIGN KEY (PersonID)REFERENCES Persons(PersonID);

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID)REFERENCES Persons(PersonID);

DROP a FOREIGN KEY Constraint

To drop a FOREIGN KEY constraint, use the following SQL:

MySQL:

ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;

SQL Server / Oracle / MS Access:

ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder;


W3schools Pathfinder

Track your progress - it's free!

W3Schools.com (2024)
Top Articles
Transformational Change Leadership
U.S. insurance agents, brokers and service industry ad spend 2023 | Statista
Ffxiv Act Plugin
Average Jonas Wife
Uca Cheerleading Nationals 2023
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Star Sessions Imx
Jesus Calling December 1 2022
Devotion Showtimes Near Mjr Universal Grand Cinema 16
Miss Carramello
The Best Classes in WoW War Within - Best Class in 11.0.2 | Dving Guides
Lesson 1 Homework 5.5 Answer Key
Vocabulario A Level 2 Pp 36 40 Answers Key
Becky Hudson Free
Jackson Stevens Global
Crossword Nexus Solver
Viprow Golf
Paychex Pricing And Fees (2024 Guide)
Pay Boot Barn Credit Card
Best Boston Pizza Places
Defending The Broken Isles
Kirsten Hatfield Crime Junkie
WRMJ.COM
Toonkor211
What is Software Defined Networking (SDN)? - GeeksforGeeks
950 Sqft 2 BHK Villa for sale in Devi Redhills Sirinium | Red Hills, Chennai | Property ID - 15334774
Craftsman Yt3000 Oil Capacity
Google Flights To Orlando
Salemhex ticket show3
Rock Salt Font Free by Sideshow » Font Squirrel
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
Austin Automotive Buda
Ticket To Paradise Showtimes Near Marshall 6 Theatre
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Google Flights Orlando
Cygenoth
Prior Authorization Requirements for Health Insurance Marketplace
Reese Witherspoon Wiki
Lbl A-Z
Lima Crime Stoppers
Janaki Kalaganaledu Serial Today Episode Written Update
No Boundaries Pants For Men
11 Best Hotels in Cologne (Köln), Germany in 2024 - My Germany Vacation
Blow Dry Bar Boynton Beach
Dicks Mear Me
Scott Surratt Salary
Ewwwww Gif
Where and How to Watch Sound of Freedom | Angel Studios
Hy-Vee, Inc. hiring Market Grille Express Assistant Department Manager in New Hope, MN | LinkedIn
91 East Freeway Accident Today 2022
7 National Titles Forum
Qvc Com Blogs
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6161

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.