---
title: SQL Foreign Key
description: "## SQL FOREIGN KEY Constraint – SQL ҮЛ ТАНИХ ТҮЛХҮҮР Хязгаарлалт  The `FOREIGN KEY` constraint is used to prevent actions that would destroy links between tables. `FOREIGN KEY` хязгаарлалт нь хүснэгт хоорондын холбоосуудыг холбоход ашиглагддаг. A `FOREIGN KEY` is a field (or collection of…"
url: "https://apprentice.mn/docs/sql-foreign-key/"
updated: "2024-07-24"
category: SQL
---

# SQL Foreign Key

## SQL FOREIGN KEY Constraint – SQL ҮЛ ТАНИХ ТҮЛХҮҮР Хязгаарлалт 

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

`FOREIGN KEY` хязгаарлалт нь хүснэгт хоорондын холбоосуудыг холбоход ашиглагддаг.

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

`FOREIGN KEY` нь нэг хүснэгтийн талбар (эсвэл талбарын цуглуулга), энэ нь өөр хүснэгтийн  `PRIMARY KEY` ойлгоно.

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.

`FOREIGN KEY`-тэй хүснэгтийг хүүхэд хүснэгт (child table) гэнэ, бас нэгдүгээр түлхүүр (PRIMARY KEY) бүхий хүснэгтийг лавлагаа (referenced) эсвэл эцэг хүснэгт (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.

“Захиалгууд” хүснэгтийн “ХүнID” багана нь “Хүмүүс” хүснэгтийн “ХүнID” баганыг зааж байна.

The “PersonID” column in the “Persons” table is the `PRIMARY KEY` in the “Persons” table.

“Хүмүүс” хүснэгтийн “ХүнID” багана нь “Хүмүүс” хүснэгтийн `PRIMARY KEY` юм.

The “PersonID” column in the “Orders” table is a `FOREIGN KEY` in the “Orders” table.

“Захиалгууд” хүснэгтийн “ХүнID” багана нь “Захиалгууд” хүснэгтийн `FOREIGN KEY` юм.

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` хязгаарлалт нь хүчинтэй бус өгөгдлийг гадны түлхүүр баганад оруулахаас сэргийлдэг, учир нь энэ нь эцэг хүснэгтэд агуулагдах утгуудын нэг байх ёстой.

## SQL FOREIGN KEY on CREATE TABLE – SQL ХҮСНЭГТ ҮҮСГЭХ дээ ҮЛ ТАНИХ ТҮЛХҮҮР байлгах

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

Дараах SQL нь “Захиалгууд” хүснэгтийг үүсгэхэд “ХүнID” баганад `FOREIGN KEY` үүсгэнэ:

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

**SQL Сервер / Oracle / MS хандалт:**

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

`FOREIGN KEY` хязгаарлалтыг нэрлэх ба олон баганад тодорхойлохын тулд дараах SQL дүрмийг ашиглана уу:

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

“Захиалгууд” хүснэгтэд “ХүнID” баганад `FOREIGN KEY` хязгаарлалтыг нэмэхийн тулд дараах 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:

`FOREIGN KEY` хязгаарлалтад нэр өгөх боломжийг олгохын тулд, мөн олон баганад `FOREIGN KEY` хязгаарлалт тодорхойлохын тулд дараах SQL дүрмийг ашиглана:

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

`FOREIGN KEY` хязгаарлалтыг устгах даа, дараах SQL хэрэглэнэ:

**MySQL:**

> ALTER TABLE Orders
> DROP FOREIGN KEY FK_PersonOrder;

**SQL Server / Oracle / MS Access:**

> ALTER TABLE Orders
> DROP CONSTRAINT FK_PersonOrder;

         [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fsql-foreign-key%2F&linkname=SQL%20Foreign%20Key)[https://www.addtoany.com/share](https://www.addtoany.com/share)
