Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views3 pages

Foreign Key

The document outlines the structure of two tables: 'Persons' and 'Orders', highlighting the relationship between them through the 'PersonID' column, which serves as a primary key in 'Persons' and a foreign key in 'Orders'. It explains the importance of the foreign key constraint in maintaining data integrity by ensuring that only valid 'PersonID' values from the 'Persons' table can be inserted into the 'Orders' table. Additionally, it provides an example SQL statement for creating the 'Orders' table with the foreign key constraint.

Uploaded by

ophylia henry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Foreign Key

The document outlines the structure of two tables: 'Persons' and 'Orders', highlighting the relationship between them through the 'PersonID' column, which serves as a primary key in 'Persons' and a foreign key in 'Orders'. It explains the importance of the foreign key constraint in maintaining data integrity by ensuring that only valid 'PersonID' values from the 'Persons' table can be inserted into the 'Orders' table. Additionally, it provides an example SQL statement for creating the 'Orders' table with the foreign key constraint.

Uploaded by

ophylia henry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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
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.
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.

FOREIGN KEY on CREATE TABLE

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

CREATE TABLE Orders (


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

You might also like