SQL Server perform a delete on all child records when deleting from a parent

ecribs

I have 3 tables:

Create TABLE Subjects
(
    SubjectID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
    SubjectName VARCHAR(20) NOT NULL,
    ClassID VARCHAR(10) FOREIGN KEY REFERENCES Classes(ClassID) NOT NULL
);

Create TABLE Topic
( 
    TopicID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
    TopicName VARCHAR(100),
    SubjectID INT FOREIGN KEY REFERENCES Subjects(SubjectID)
); 

Create Table Worksheet
(
    WorksheetName varchar(100) PRIMARY KEY,
    TopicID INT Foreign KEY References Topic(TopicID),
    Num_Q INT NOT NULL,
    W_Type varchar(30)
);

Each one is a one to many relationship. When I try to delete from Subjects I get a foreign key constraint which is fine. What I want to know is how to get around this and perform a query to delete all relating aspects in a cascading style. I looked it up and there's but I am not sure how it works there seems to be multiple queries. Would it be better to create a trigger or is there a basic cascading function to do it all? I'm using visual studio to perform queries but not sure where the options to perform tasks like this are?

Gordon Linoff

You can add the ON DELETE CASCADE right after the foreign key definition:

Create TABLE Subjects (
    SubjectID INT PRIMARY KEY NOT NULL IDENTITY(1, 1),
    SubjectName VARCHAR(20) NOT NULL,
    ClassID VARCHAR(10) NOT NULL
        FOREIGN KEY REFERENCES Classes(ClassID) ON DELETE CASCADE
);

You can also define it as a separate constraint, if you like, either within the CREATE TABLE statement or using ALTER TABLE ADD CONSTRAINT.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete all child records from record

From Dev

Linq to SQL Delete child and its relationships while Deleting Parent

From Dev

How to prevent from Grails not to delete child while deleting parent?

From Dev

All records being deleted when deleting records from a temp table

From Dev

Purge by deleting all records older than 180 days from the current date in SQL Server

From Dev

Deleting particular named directories from parent and all child folders

From Dev

Delete Child Records with Missing Parent

From Dev

How to delete all duplicate records from sql

From Dev

Retrieve all parent/child records from database on Laravel (hierarchical data)

From Dev

Best practices deleting many records from several tables in SQL Server

From Dev

Best practices deleting many records from several tables in SQL Server

From Dev

How to soft delete related records when soft deleting a parent record in Laravel?

From Dev

how delete parent row with all child row from other table

From Dev

Delete parent and child records depending on search text

From Dev

Saving a child when deleting a parent node

From Dev

Saving a child when deleting a parent node

From Dev

SQL Server - Concatenate all child IDs into delimited string in parent record

From Dev

Delete orphans when deleting parent manyToOne annotaion

From Dev

"On Delete Cascade" if deleting a row from the child table

From Dev

Deleting Records in SQL server table prior to insert

From Dev

Delete parent table data when deleted all related child rows form child table in laravel

From Dev

How to Delete Duplicate records from Table in SQL Server?

From Dev

Delete duplicate records from SQL Server 2012 table with identity

From Dev

Delete records from both SQL Server tables in a JOIN using a CTE

From Dev

Getting Oracle SQL top level parent record from a number of parent/child records

From Dev

delete subfolders and files without deleting parent folder and child folder?

From Dev

Query parent relation when querying for child records

From Dev

Parent Child relation in SQL Server

From Dev

Parent Child relation in SQL Server

Related Related

  1. 1

    Delete all child records from record

  2. 2

    Linq to SQL Delete child and its relationships while Deleting Parent

  3. 3

    How to prevent from Grails not to delete child while deleting parent?

  4. 4

    All records being deleted when deleting records from a temp table

  5. 5

    Purge by deleting all records older than 180 days from the current date in SQL Server

  6. 6

    Deleting particular named directories from parent and all child folders

  7. 7

    Delete Child Records with Missing Parent

  8. 8

    How to delete all duplicate records from sql

  9. 9

    Retrieve all parent/child records from database on Laravel (hierarchical data)

  10. 10

    Best practices deleting many records from several tables in SQL Server

  11. 11

    Best practices deleting many records from several tables in SQL Server

  12. 12

    How to soft delete related records when soft deleting a parent record in Laravel?

  13. 13

    how delete parent row with all child row from other table

  14. 14

    Delete parent and child records depending on search text

  15. 15

    Saving a child when deleting a parent node

  16. 16

    Saving a child when deleting a parent node

  17. 17

    SQL Server - Concatenate all child IDs into delimited string in parent record

  18. 18

    Delete orphans when deleting parent manyToOne annotaion

  19. 19

    "On Delete Cascade" if deleting a row from the child table

  20. 20

    Deleting Records in SQL server table prior to insert

  21. 21

    Delete parent table data when deleted all related child rows form child table in laravel

  22. 22

    How to Delete Duplicate records from Table in SQL Server?

  23. 23

    Delete duplicate records from SQL Server 2012 table with identity

  24. 24

    Delete records from both SQL Server tables in a JOIN using a CTE

  25. 25

    Getting Oracle SQL top level parent record from a number of parent/child records

  26. 26

    delete subfolders and files without deleting parent folder and child folder?

  27. 27

    Query parent relation when querying for child records

  28. 28

    Parent Child relation in SQL Server

  29. 29

    Parent Child relation in SQL Server

HotTag

Archive