In this post explains how to use Foreign Keys with cascade delete in SQL Server with syntax and examples.
A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
A foreign key with cascade delete can be created using either a CREATE TABLE statement or an ALTER TABLE statement.
The syntax for creating a foreign key with cascade delete using a CREATE TABLE statement in SQL Server (Transact-SQL) is:
The name of the child table that you wish to create.
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 assumes NULL as the default.
The name of the foreign key constraint that you wish to create.
The columns in child_table that will reference a primary key in the parent_table.
The name of the parent table whose primary key will be used in the child_table.
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.
It specifies that the child data is deleted when the parent data is deleted.
Optional. It specifies what to do with the child data when the parent data is updated. You have the options of NO ACTION, CASCADE, SET NULL, or SET DEFAULT.
It is used in conjunction with ON DELETE or ON UPDATE. It means that no action is performed with the child data when the parent data is deleted or updated.
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.
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.
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.
Let's look at an example of how to create a foreign key with cascade delete in SQL Server (Transact-SQL) using the CREATE TABLE statement.
For example:
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 with cascade delete 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.
For this foreign key, we have specified the ON DELETE CASCADE clause which tells SQL Server to delete the corresponding records in the child table when the data in the parent table is deleted. So in this example, if a product_id value is deleted from the products table, the corresponding records in the inventory table that use this product_id will also be deleted.
The syntax for creating a foreign key with cascade delete using an ALTER TABLE statement in SQL Server (Transact-SQL) is:
The name of the child table that you wish to modify.
The name of the foreign key constraint that you wish to create.
The columns in child_table that will reference a primary key in the parent_table.
The name of the parent table whose primary key will be used in the child_table.
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.
It specifies that the child data is deleted when the parent data is deleted.
Let's look at an example of how to create a foreign key with cascade delete 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.
For this foreign key, we have specified the ON DELETE CASCADE clause which tells SQL Server to delete the corresponding records in the child table when the data in the parent table is deleted. So in this example, if a product_id value is deleted from the products table, the corresponding records in the inventory table that use this product_id will also be deleted.