Foreign Key foreign key in SQL Server
This tutorial introduces how to use Foreign Key foreign key in SQL Server with syntax and examples.
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.
- Foreign Key (Cascade Delete) in SQL Server
- 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
You should read it
- Activate foreign keys in SQL Server
- Foreign Key with Set Null in SQL Server
- Foreign Key with Cascade Delete in SQL Server
- Delete the foreign key in SQL Server
- Disable foreign key in SQL Server
- Need to be wary of foreign objects in the ear
- The 10 best 'jobs' are available only to those who know a foreign language
- 7 compelling reasons for you to definitely learn a foreign language
- 7 simple tips to help you learn new languages in just one week
- Good news: Learning a foreign language helps increase brain elasticity
- Visiting foreign websites - how to stay safe?
- Check constraints in SQL Server
Maybe you are interested
Why do you 'fart' have a foul smell and others don't? 6 simple ways to minimize the harmful effects of sitting for a long time The new computer model clearly explains the strange trajectory of the Moon 20 pictures of super beautiful splendor around the world on November 14 'Aliens will arrive on Earth in September 2017' - Nostradamus' prediction How hot is the planet of Mercury?