Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The referenced table is called the parent table while the table with the foreign key is called the child
table. The foreign key in the child table will generally reference a primary key in the parent table.
A foreign key can be created using either a CREATE TABLE statement or an ALTER TABLE
statement.
Syntax
The syntax for creating a foreign key using a CREATE TABLE statement in SQL Server (Transact-
SQL) is:
CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n)
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
);
child_table
The name of the child table that you wish to create.
column1, column2
The columns that you wish to create in the table. Each column must have a datatype. The column
should either be defined as NULL or NOT NULL and if this value is left blank, the database
https://www.techonthenet.com/sql_server/foreign_keys/foreign_keys.php 1/5
9/20/2018 SQL Server: Foreign Keys
CASCADE
It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is either
deleted or updated when the parent data is deleted or updated.
SET NULL
It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to
NULL when the parent data is deleted or updated.
SET DEFAULT
It is used in conjunction with ON DELETE or ON UPDATE. It means that the child data is set to
their default values when the parent data is deleted or updated.
Example
Let's look at an example of how to create a foreign key in SQL Server (Transact-SQL) using the
CREATE TABLE statement.
For example:
https://www.techonthenet.com/sql_server/foreign_keys/foreign_keys.php 2/5
9/20/2018 SQL Server: Foreign Keys
In this foreign key example, we've created our parent table as the products table. The products table
has a primary key that consists of the product_id field.
Next, we've created a second table called inventory that will be the child table in this foreign key
example. We have used the CREATE TABLE statement to create a foreign key on the inventory table
called fk_inv_product_id. The foreign key establishes a relationship between the product_id column in
the inventory table and the product_id column in the products table.
This first example shows how to create a foreign key involving one column. Let's look at how to create
a foreign key with one than one field.
For example:
https://www.techonthenet.com/sql_server/foreign_keys/foreign_keys.php 3/5
9/20/2018 SQL Server: Foreign Keys
In this foreign key example, our parent table called products has a primary key that consists of both
the product_name and location columns. Therefore, our child table and foreign key must also
reference these two columns.
So in this example, our foreign key called fk_inv_product references the products table based on two
fields - the product_name and location fields.
Syntax
The syntax for creating a foreign key using an ALTER TABLE statement in SQL Server (Transact-
SQL) is:
child_table
The name of the child table that you wish to modify.
fk_name
The name of the foreign key constraint that you wish to create.
parent_table
The name of the parent table whose primary key will be used in the child_table.
parent_col1, parent_col2, ... parent_col3
The columns that make up the primary key in the parent_table. The foreign key will enforce a link
between this data and the child_col1, child_col2, ... child_col_n columns in the child_table.
Example
Let's look at an example of how to create a foreign key in SQL Server (Transact-SQL) using the
ALTER TABLE statement.
For example:
In this foreign key example, we've created a foreign key on the inventory table called
fk_inv_product_id that references the products table based on the product_id field.
We could also create a foreign key with more than one field as in the example below:
In this SQL Server example, we have created a foreign key on the inventory table called
fk_inv_product that references the products table based on the product_name and location columns.
Copyright TechOnTheNet.com
https://www.techonthenet.com/sql_server/foreign_keys/foreign_keys.php 5/5