Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created February 23, 2015 06:51
Show Gist options
  • Save AlexArchive/b9382b66f8e2492c0228 to your computer and use it in GitHub Desktop.
Save AlexArchive/b9382b66f8e2492c0228 to your computer and use it in GitHub Desktop.
Cascade Delete T-Sql
USE master;
DROP DATABASE Experiment;
CREATE DATABASE Experiment;
USE Experiment;
CREATE TABLE dbo.Departments
(
departmentid INT PRIMARY KEY IDENTITY,
name NVARCHAR(100) NOT NULL
);
CREATE TABLE dbo.Employees
(
employeeid INT PRIMARY KEY IDENTITY,
name NVARCHAR(200) NOT NULL,
departmentid INT
CONSTRAINT Fk_Departments FOREIGN KEY (departmentid)
REFERENCES dbo.Departments (departmentid)
ON DELETE SET NULL
);
INSERT INTO dbo.Departments
VALUES ('Human Resources');
INSERT INTO dbo.Employees
VALUES ('Toby Flenderson', 1);
DELETE
FROM dbo.Departments
WHERE departmentid = 1
SELECT *
FROM dbo.Employees
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment