Foreign Key foreign key in SQL Server

This tutorial introduces how to use Foreign Key foreign key in SQL Server with syntax and examples.

What is the foreign key in SQL Server?

Foreign keys are used to increase references in SQL Server databases. A foreign key means that the value in this table must appear in another table.

The reference table is called the parent table, while the table containing the foreign key is called a child table. Foreign keys in the child table usually refer to the primary key PRIMARY KEY in the parent table.

Foreign keys can be created with the CREATE TABLE command or the ALTER TABLE command.

Create a foreign key using the CREATE TABLE command

Syntax

 CREATE TABLE bang_con ( cot1 kieudulieu [ NULL | NOT NULL ], cot2 kieudulieu [ NULL | NOT NULL ], … CONSTRAINT fk_ten FOREIGN KEY (cot_con1, cot_con2, … cot_con_n) REFERENCES bang_me (cot_me1, cot_me2, … cot_me_n) [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] ); 

bang_con

The name of the child table you want to create.

cot1, cot2

The column you want to create in the table. Each column has 1 data type, must be specified to contain NULL or NOT NULL values, otherwise default is NULL.

Data types in SQL Server

fk_ten

The name of the foreign key constraint to create.

cot_con1, cot_con2, . cot_con_n

The column in bang_con wants to refer to the primary key in bang_me.

bang_me

The name of the parent table contains the primary key used in bang_con.

cot_me1, cot_me2, . cot_me_n

The column that makes up the primary key in bang_me. Foreign keys will create constraints between data and columns cot_con1, cot_con2, . cot_con_n in bang_con.

ON DELETE

Option. Indicates what to do with the child data when the parent data is deleted. There are options NO ACTION, CASCADE, SET NULL and SET DEFAULT.

ON UPDATE

Option. Indicates what to do with the child data when the parent data is updated. There are options NO ACTION, CASCADE, SET NULL and SET DEFAULT.

NO ACTION

Use with ON DELETE or ON UPDATE, ie do nothing with child data when parent data is deleted or updated.

CASCADE

Use with ON DELETE or ON UPDATE, meaning that the child data is deleted or updated when the parent data is deleted or updated.

SET NULL

Use with ON DELETE or ON UPDATE, meaning that the child data is set to NULL when the parent data is deleted or updated.

SET DEFAULT

Use with ON DELETE or ON UPDATE, meaning that the child data is set to the default value when the parent data is deleted or updated.

  1. Foreign Key (Cascade Delete) in SQL Server
  2. Foreign Key (Set Null) foreign key in SQL Server

For example

 CREATE TABLE sanpham (id_sanpham INT PRIMARY KEY, ten_sanpham VARCHAR(50) NOT NULL, phan_loai VARCHAR(25) ); CREATE TABLE hangtonkho ( id_hangtonkho INT PRIMARY KEY, id_sanpham INT NOT NULL, soluong INT, luong_toithieu INT, luong_toida INT, CONSTRAINT fk_htk_id_sanpham FOREIGN KEY (id_sanpham) REFERENCES sanpham (id_sanpham) ); 

In the above example, we create the parent table, the sanpham table with the primary key consisting of the fields in id_sanpham. Next is the hangtonkho sub table.

The CREATE TABLE statement used to create the foreign key of the hangtonkho table is named fk_htk_id_sanpham. The foreign key forms the link between the id_sanpham column in the hangtonkho table and id_sanpham in the sanpham table.

The example above shows how to create a foreign key consisting of 1 column. Now create a foreign key with more than 1 information field.

For example

 CREATE TABLE sanpham ( ten_sanpham VARCHAR(50) NOT NULL, diadiem VARCHAR(50) NOT NULL, phanloai VARCHAR(25) CONSTRAINT sanpham_pk PRIMARY KEY (ten_sanpham, diadiem) ); 
 CREATE TABLE hangtonkho ( id_hangtonkho INT PRIMARY KEY, ten_sanpham VARCHAR(50) NOT NULL, diadiem VARCHAR(50) NOT NULL, soluong INT, luong_toithieu INT, luong_toida INT, CONSTRAINT fk_htk_sanpham FOREIGN KEY (ten_sanpham, diadiem) REFERENCES sanpham (ten_sanpham, diadiem) ); 

In this example, the sanpham parent table has a two-column primary key ten_sanpham and diadiem. Child tables and foreign keys must refer to these two columns.

Create a foreign key using the ALTER TABLE command

Syntax

 ALTER TABLE bang_con ADD CONSTRAINT fk_ten FOREIGN KEY (cot_con1, cot_con2, … cot_con_n) REFERENCES bang_me (cot_me1, cot_me2, … cot_me_n); 

bang_con

The name of the child table you want to create.

fk_ten

The name of the foreign key constraint to create.

cot_con1, cot_con2, . cot_con_n

The column in bang_con wants to refer to the primary key in bang_me.

bang_me

The name of the parent table contains the primary key used in bang_con.

cot_me1, cot_me2, . cot_me_n

The column that makes up the primary key in bang_me. Foreign keys will create constraints between data and columns cot_con1, cot_con2, . cot_con_n in bang_con.

For example

 ALTER TABLE hangtonkho ADD CONSTRAINT fk_htk_id_sanpham FOREIGN KEY (id_sanpham) REFERENCES sanpham (id_sanpham); 

This example creates the foreign key in the hangtonkho table called fk_htk_id_sanpham, referencing the sanpham table based on the id_sanpham column.

You can create foreign keys with more than 1 field as the example below.

 ALTER TABLE hangtonkho ADD CONSTRAINT fk_htk_sanpham FOREIGN KEY (ten_sanpham, diadiem) REFERENCES sanpham (ten_sanpham, diadiem); 

The example above creates a foreign key named fk_htk_sanpham for the hangtonkho table, referring to the dashboard based on the ten_sanpham and diadiem columns.

Previous lesson: LOCAL TEMPORARY TABLE in SQL Server

The following article: Foreign Key Foreign Key (Cascade Delete) in SQL Server

4.5 ★ | 2 Vote

May be interested

  • Bkav released the W32.WeakPass anti-virus server inspection toolBkav released the W32.WeakPass anti-virus server inspection tool
    on the afternoon of february 19, bkav released a tool to help administrators check the security of the server against a targeted attack on vietnamese public servers from foreign hackers.
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • Good news: Learning a foreign language helps increase brain elasticityGood news: Learning a foreign language helps increase brain elasticity
    new findings show that people who are exposed to foreign languages ​​have better elasticity.
  • How to set up your own Git server on LinuxHow to set up your own Git server on Linux
    while you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
  • Use IIS to set up FTP Server on WindowsUse IIS to set up FTP Server on Windows
    set up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • What is VPS? VPS used to do? What is VPS different from Server?What is VPS? VPS used to do? What is VPS different from Server?
    what is vps? vps used to do? what is vps different from server ?. when you intend to learn about network data or open the website, you will definitely be introduced to many different server and server services. but server hosting has a lot of tricks
  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • Instructions for setting up and managing FTP Server on Windows 10Instructions for setting up and managing FTP Server on Windows 10
    if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.