Created
February 23, 2015 06:51
-
-
Save AlexArchive/b9382b66f8e2492c0228 to your computer and use it in GitHub Desktop.
Cascade Delete T-Sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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