Scenario:You are working as SQL Server developer, you need to create two tables with Primary -Foreign Key Relationship. You want to create Foreign Key Constraint with setting if record will be deleted from Referenced Table (Primary Key Column Table), it should be deleted from Parent Table ( Foreign Key Constraint Table) as well.
USE YourDatabaseName GO CREATE TABLE dbo.Customer ( Customerid INT PRIMARY KEY ,FName VARCHAR(100) ,LName VARCHAR(100) ,SSN VARCHAR(10) ) CREATE TABLE dbo.Orders ( OrderId INT Identity(1, 1) ,OrderitemName VARCHAR(50) ,OrderItemAmt INT ,Customer_id INT FOREIGN KEY REFERENCES Customer(CustomerId) ON DELETE CASCADE ) --insert sample data insert into dbo.Customer (CustomerId,FName, LName,SSN) values (1,'Aamir','Shahzad','000-000-00')
|How to enable ON Delete CASCADE rule with Foreign Key Constraint in SQL Server|
--Delete the Record from Referenced Table(PK Column Table) Delete from dbo.Customer where CustomerId=1
|How to use ON Delete Cascade to delete records from multiple Tables in SQL Server Table|